bld
bld

Reputation: 70

Display image chosen from Jfilechooser on Jpanel

I'm trying to make a JPanel that has a button that you click to upload an image. It brings up a dialog box with a JFilechooser that stores the file path name in an ArrayList when you click open. I want the chosen image to appear on the JPanel after it is chosen. I know lots of people have asked this question before, but I've tried all the solutions I can find and I can't get the image to appear. Here's my code for the actionperformed when I click the upload button (AddImages is the Jpanel):

private void pic1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        JFileChooser fc = new JFileChooser();
        int result = fc.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String sname = file.getAbsolutePath();
            ImageIcon icon = new ImageIcon(sname);
            JLabel label = new JLabel(icon, JLabel.CENTER);
            AddImages.add(label);
        }
    }                                    

When I try to upload an image, the image gets stored in the ArrayList (tested this by adding a line that prints the size of the ArrayList after I add the image), but it doesn't show up on the JPanel. Does anyone know what I'm doing wrong? Thanks in advance.

EDIT: Here's the full code for the panel (which is visible). The actionperformed for pic1 and pic2 is the same code, which I've shown above:

addImagePrompt.setText("Add some pics");

    pic2.setText("add pic");
    pic2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            pic2ActionPerformed(evt);
        }
    });

    pic1.setText("add pic");
    pic1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            pic1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout AddImagesLayout = new javax.swing.GroupLayout(AddImages);
    AddImages.setLayout(AddImagesLayout);
    AddImagesLayout.setHorizontalGroup(
        AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(AddImagesLayout.createSequentialGroup()
            .addGap(50, 50, 50)
            .addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(AddImagesLayout.createSequentialGroup()
                    .addComponent(pic2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(pic2name, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(pic1)
                .addComponent(addImagePrompt))
            .addContainerGap(161, Short.MAX_VALUE))
    );
    AddImagesLayout.setVerticalGroup(
        AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(AddImagesLayout.createSequentialGroup()
            .addGap(22, 22, 22)
            .addComponent(addImagePrompt)
            .addGap(18, 18, 18)
            .addComponent(pic1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(AddImagesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(pic2)
                .addComponent(pic2name))
            .addContainerGap(377, Short.MAX_VALUE))
    );

    mainPanel.add(AddImages, "card16");

Upvotes: 0

Views: 272

Answers (2)

andriycodes
andriycodes

Reputation: 34

The only thing you are missing here is a proper layout manager. You can read up on this right here: A Visual Guide to Layout Managers

So adding something like this for example should help you achieve what you desire:

AddImages.setLayout(new BoxLayout(AddImages, BoxLayout.Y_AXIS));
AddImages.add(label);
AddImages.revalidate();
this.pack();

Upvotes: 0

Joni
Joni

Reputation: 111219

After adding a component to a container, you have to call revalidate() so that its layout manager (re)computes the positions for child components and repaints them:

JLabel label = new JLabel(icon, JLabel.CENTER);
AddImages.add(label);
AddImages.revalidate();

If you upload multiple images using the same "AddImages" panel you'll see that they are all added the panel as separate components. If that's not what you expect, you'll need to rethink how you approach this.

Upvotes: 1

Related Questions