-2

I have a JFrame opening with 12 different pictures. I want to click on one and then in the new JFrame, show that same picture. In my code, shirts is my JFrame with the 12 pictures and I want the clicked picture to appear in the new JFrame called sizes. I made an ArrayList of type JLabel called select. Here is my code:

final JFrame shirts = new JFrame("T-shirts");

          JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));

          for (int i = 1; i < 13; i++) {
            l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);

            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setFont(l.getFont().deriveFont(20f));
            panel.add(l);
            select.add(l);
          }//end of for loop

          panel.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
              sizes = new JFrame("Shopping");
              sizes.setVisible(true);
              sizes.setSize(500, 500);
              sizes.setLocation(100,200);
              shirts.dispose();


            for(int i=0; i<12; i++){
                if(e.getSource()==select.get(i)){
            JLabel addition = newJLabel(newImageIcon(select.get(i).getText()));
                  //sizes.add(select.get(i));//Picture isn't added!!
               }//end of if

            }//end of for
            }//end of method  
          });//end of listener


          shirts.setContentPane(panel);
          shirts.setSize(1000, 1000);
          shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          shirts.setVisible(true);
jjj
  • 27
  • 8

3 Answers3

3

Start with How to Write a Mouse Listener.

Basically, you want to register a MouseListener with the JLabel which represents your picture and implement it's mouseClicked method.

You then want to get the Icon property of the JLabel that was clicked and pass that to your new frame, which you can simply use to set the Icon property of another JLabel

Something like...

addition.addMouseListener(new MouseAdapter() {

    @Override
    public void mouseClicked(MouseEvent e) {
        JLabel label = (JLabel) e.getComponent();
        Icon icon = label.getIcon();
        // Create new frame and pass it the icon value
    }

});

as an example

You might also like to take a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using either a CardLayout or JDialog instead of another JFrame ... which could become messy

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Try making each picture a JButton, or at least placing an invisible button behind each picture. Then, when they are pressed in the ActionListener extended class, have the new JFrame be created, with the picture large.

MLavrentyev
  • 1,827
  • 2
  • 24
  • 32
2

You can use a JButton and make it look like a JLabel:

JButton button = new JButton( new ImageIcon("..." ));
button.setBorderPainted( false );
button.setContentFilled( false );
button.setFocusPainted( false );
button.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        Icon icon = button.getIcon();
        // do something with the Icon.
    }
});

Then you can add an ActionListener to the button.

An ActionListener is more reliable than using a MouseListener and handling the mouseClicked event. The mouseClicked event is only generated when a mousePressed and mouseReleased event is generated at the same mouse point. So if the mouse moves by even pixel between the two event you will not get a mouseClicked event which users think is a random problem.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    Yes, I suggested this to the OP two days ago on their [other question](http://stackoverflow.com/q/29735016/418556).. :-/ You can lead a horse to water.. – Andrew Thompson Apr 22 '15 at 03:19
  • 1
    @AndrewThompson, thanks for the heads up. Three questions on this topic.... and counting. Won't be so quick to help the next time a question is posted. – camickr Apr 22 '15 at 04:01