Hi

I am not sure if this problem is due to my beginners level java skills or if there is another issue.

Either way any help you can give would be much appreciated.

I have a JFrame with a box layout and I want to add JPanels to it.

The problem is that if there is more than one JPanel the last one covers the first one and sets it to the second position.

Any help greatly appreciated

Best Regards

Gerry
public class TheMainFrame extends javax.swing.JFrame {
String filePath;
ArrayList<BufferedImage>  docList;
    /**
     * Creates new form TheMainFrame
     */
    public TheMainFrame() {
        initComponents();
        this.docList = new ArrayList<BufferedImage>();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">             
             
    private void initComponents() {

        findFileChooser = new javax.swing.JFileChooser();
        picture = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        open = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        findFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findFileChooserActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout pictureLayout = new 
javax.swing.GroupLayout(picture);
        picture.setLayout(pictureLayout);
        pictureLayout.setHorizontalGroup(
            
pictureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        pictureLayout.setVerticalGroup(
            
pictureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        jMenu1.setText("File");

        open.setText("Open");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });
        jMenu1.add(open);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picture, javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picture, javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void openActionPerformed(java.awt.event.ActionEvent evt) {          
                           
        // TODO add your handling code here:
        int returnVal = findFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION){
            File file = findFileChooser.getSelectedFile();
            this.filePath = file.getAbsolutePath();
            //System.out.println("filePath = " + filePath);
        }
    }                                    

    private void findFileChooserActionPerformed(java.awt.event.ActionEvent evt) 
{                                                
        // TODO add your handling code here:
        
        if (findFileChooser.isAcceptAllFileFilterUsed()) {
             File filePath = findFileChooser.getSelectedFile();
             SecondaryProcessPanel secondary = new 
SecondaryProcessPanel(filePath.getAbsolutePath());
            try {
                this.docList = secondary.makeImageArray();
            } catch (IOException ex) {
                
Logger.getLogger(TheMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            processView(secondary);
            
        }else{
            System.out.println("File access cancelled by user");
        }
    }                                               
    public void processView(SecondaryProcessPanel secondary){
        this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
        for(int i = 0; i < this.docList.size(); i++){
            
            JPanel panelBack = secondary.getPanel(this.docList.get(i));
            panelBack.setPreferredSize(new Dimension(200,200));
            this.add(panelBack, BorderLayout.CENTER);
            }
           
        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(600, 800);
        //this.setVisible(true);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting 
code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the 
default look and feel.
         * For details see 
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : 
javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            
java.util.logging.Logger.getLogger(TheMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
        } catch (InstantiationException ex) {
            
java.util.logging.Logger.getLogger(TheMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
        } catch (IllegalAccessException ex) {
            
java.util.logging.Logger.getLogger(TheMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            
java.util.logging.Logger.getLogger(TheMainFrame.class.getName()).log(java.util.logging.Level.SEVERE,
 null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TheMainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JFileChooser findFileChooser;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem open;
    private javax.swing.JPanel picture;
    // End of variables declaration                   
public class SecondaryProcessPanel extends javax.swing.JPanel {
    String filePath;
    ArrayList<BufferedImage> docList;
    BufferedImage image;
    int y = 0;
    /**
     * Creates new form SecondaryProcessPanel
     */
    public SecondaryProcessPanel(String filePath) {
        initComponents();
        this.filePath = filePath;
        this.docList = new ArrayList<BufferedImage>();
    }
    public ArrayList makeImageArray() throws IOException{
        File target = new File(filePath);
        PDDocument pddocument = new PDDocument();
        pddocument = pddocument.load(target);
        PDFRenderer pdfrend = new PDFRenderer(pddocument);
        for (int i = 0; i < pddocument.getNumberOfPages(); i ++){
            BufferedImage pageImage = pdfrend.renderImage(i);
            this.docList.add(pageImage);
        }
        pddocument.close();
        return this.docList;
    }
    public JPanel getPanel(BufferedImage image){
        this.image = image;
        return this;
    }
    @Override
    public void paint(Graphics g){
          
            boolean torf = g.drawImage(image, 0,0, null);
              
            
           
           
            
    }
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">             
             
    private void initComponents() {

        ScrollPane = new javax.swing.JScrollPane();

        setAutoscrolls(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 
400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 
300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane ScrollPane;
    // End of variables declaration                   
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to