JFrame & JWindow Conflicts

161 Views Asked by At

I have a faulty code and I know where the problem lies, but I don't know how to implement the solution. I want to make a program, which on click of a button opens a PDF in a intern PDF Reader. This works great so far but since it loads a few seconds I wanted to implement a splash screen showing a logo using JWindow. But since my PDF Reader also uses GUI, they seem to conflict since the JWindow doesn't appear. I know that the official SplashScreen class is recommended as a better solution but I can't find a simple tutorial on how to use this so I went with the JWindow Version. Can someone help me implement a solution to this code?

Help would be much appreciated

Loading Screen Class

package pdf;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

public class loadingScreen extends JWindow {
BorderLayout bl = new BorderLayout();
JLabel imageLabel = new JLabel();
JPanel barPanel = new JPanel();
FlowLayout barPanelLayout = new FlowLayout();
JProgressBar progress = new JProgressBar();
ImageIcon icon;
public loadingScreen(ImageIcon icon) {

this.icon = icon;
try {
  lsInit();
}

catch(Exception e) {
  e.printStackTrace();
}



}

public void lsInit() throws Exception{

this.imageLabel.setIcon(this.icon);
this.getContentPane().setLayout(bl);
barPanel.setLayout(this.barPanelLayout);
barPanel.setBackground(Color.BLACK);
this.getContentPane().add(this.imageLabel,BorderLayout.CENTER);
//this.getContentPane().add(this.barPanel, BorderLayout.SOUTH);
//barPanel.add(this.progress,null);
this.setSize(new Dimension(500,500));
this.setVisible(true);
this.pack();

}



public void setScreenVisible(boolean b) {
setVisible(b);
 }

}

PDF Reader with Usage of Loading Screen

      package pdf;

  import java.awt.SplashScreen;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  import java.io.File;
  import java.io.IOException;
  import java.util.List;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import org.apache.pdfbox.pdfviewer.PDFPagePanel;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;

  public class PDFViewer {
    int currentPageNo = 0;
    loadingScreen screen;

    public PDFViewer(String path) {

      File pdf_path = new File(path);
      try {
        splashScreenInit();
       PDDocument inputPdf = PDDocument.load(pdf_path);
        List<PDPage> allPgs = inputPdf.getDocumentCatalog().getAllPages();
        PDPage testPage = (PDPage)allPgs.get(0);

        JFrame testFrame = new JFrame();
        testFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        testFrame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            try {
              inputPdf.close();
              testFrame.setVisible(false);
            } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }

          }
        });
        PDFPagePanel pdfPanel = new PDFPagePanel();
        pdfPanel.setPage(testPage);
        testFrame.add(pdfPanel);
        testFrame.setBounds(0, 0, pdfPanel.getWidth(), pdfPanel.getHeight()+50);
        testFrame.addKeyListener(new KeyAdapter() {

          @Override
          public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

          }

          @Override
          public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_DOWN ) {
              if(currentPageNo < allPgs.size()-1) {
                currentPageNo++;
                PDPage currentPage = (PDPage)allPgs.get(currentPageNo);
                pdfPanel.setPage(currentPage);
                testFrame.add(pdfPanel);
                testFrame.invalidate();
                testFrame.validate();
                testFrame.repaint();

              }
            }

            if(e.getKeyCode() == KeyEvent.VK_UP) {
              if(currentPageNo>0) {
                currentPageNo--;
                PDPage currentPage = (PDPage)allPgs.get(currentPageNo);
                pdfPanel.setPage(currentPage);
                testFrame.add(pdfPanel);
                testFrame.invalidate();
                testFrame.validate();
                testFrame.repaint();
              }
            }

          }

          @Override
          public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

          }

        });
        splashScreenDestruct();
        testFrame.setVisible(true);

      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    public void splashScreenInit() {
      ImageIcon logo = new ImageIcon("resources/BASF_logo_logotype.png");//Splashscrreen
      screen = new loadingScreen(logo);
      //screen.setLocationRelativeTo(null);
      //screen.setScreenVisible(true);
      //screen.setProgressMax(100);


    }

    public void splashScreenDestruct() {
      screen.setScreenVisible(false);
      screen = null;
    }

  }
0

There are 0 best solutions below