Domanda

I have seen many answers regarding this, but nothing seem to solve my problem, I have a jPanel1 and jPanel3 in a tabbed pane (same tab) and when I click a button from jPanel1 then I go to load an image in jPanel3. Here is how I am trying to do the same

jButton1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("In the action");
            try {
                jPanel3.add(new JPanel(){
                    java.net.URL imgURL = this.getClass().getResource( "/resource/images/cd-dvd-icon.png");
                    BufferedImage image = ImageIO.read(imgURL);
                });
                jPanel3.revalidate();
                jPanel3.repaint();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

But I am not seeing any error or the image also. Please help

È stato utile?

Soluzione 2

Finally figured out the problem, have added the layout again as like it is added in the generated code and it worked

jButton3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           System.out.println("In the action");
           JLabel imgLabel = new JLabel(new ImageIcon(this.getClass().getResource( "/resource/images/cd-dvd-icon.png")));
            jPanel2.add(imgLabel);
            jPanel2.revalidate();
            jPanel2.repaint();
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(jButton3)
                    .addGap(59, 59, 59)
                    .addComponent(jToggleButton1)
                    .addGap(50, 50, 50)
                    .addComponent(imgLabel)
                    .addContainerGap(235, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jToggleButton1)
                        .addComponent(jButton3))
                        .addGap(59, 59, 59)
                        .addComponent(imgLabel)
                    .addContainerGap(368, Short.MAX_VALUE))
            );
        }

    });

Altri suggerimenti

You're not adding the image to the panel. Create a JLabel with the image inside and then add the label to jPanel3:

        try 
        {
            JLabel imgLabel = new JLabel(new ImageIcon(this.getClass().getResource( "/resource/images/cd-dvd-icon.png")));
            jPanel3.add(imgLabel);
            jPanel3.revalidate();
            jPanel3.repaint();
        }catch (Exception ex){
            ex.printStackTrace();
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top