7 Replies - 659 Views - Last Post: 04 December 2011 - 01:51 PM Rate Topic: -----

#1 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-November 11

How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 11:18 AM

I need to write an action listener so that the current selected item in a combobox decides which jpg is displayed in a panel. My problem is I havent had much luck with getting images to display in my jlabels and i'm also not 100% sure on the exact code for the action listener.

so far i have a constructor creating my image icons and labels

CWSwing()
   {
       image1 = new ImageIcon(getClass().getResource("2320.jpg"));
       image2 = new ImageIcon(getClass().getResource("5733.jpg"));
       image3 = new ImageIcon(getClass().getResource("C325.jpg"));
       image4 = new ImageIcon(getClass().getResource("G575.jpg"));
       image5 = new ImageIcon(getClass().getResource("Monza C1.jpg"));
       image6 = new ImageIcon(getClass().getResource("Z5771.jpg"));
       
       imglabel1 = new JLabel(image1);
       imglabel2 = new JLabel(image2);
       imglabel3 = new JLabel(image3);
       imglabel4 = new JLabel(image4);
       imglabel5 = new JLabel(image5);
       imglabel6 = new JLabel(image6);
   }




Is This A Good Question/Topic? 0
  • +

Replies To: How do I use an action listener to choose an image to display?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 11:41 AM

Are you getting errors? You didn't share the ActionListener, so we've no way of telling what may be going wrong there.

In general, when the ActionListener fires, check the contents of the JComboBox, choose the image to display based on the JComboBox contents, and display the image. We'll need to see the code and/or any error messages you may be getting to figure out where the relatively simple chain of required events is breaking down.
Was This Post Helpful? 0
  • +
  • -

#3 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-November 11

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 11:52 AM

static class ComboListener implements ActionListener {
        @Override
   public void actionPerformed(ActionEvent e) {
       
            if (combochoice == 1)
             {
                 if (DeskList.getSelectedItem().equals("Lenovo C325"));
                 {
                     p6.add(imglabel1);
                 }
             }
            else
            {
            
            }
    }
}



i have no errors it just doesn't display the image i think it is because there is no assignment to the right panel within the constructor. But i don't know where to go from here.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,171
  • Joined: 05-April 11

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 11:59 AM

I would rather you make a class named something like ComboBoxElement, where you give it a String (that should be shown), and the image.

You can give your JComboBox an array of ComboBoxElement, and the toString() of ComboBoxElement will be shown.
Cast the getSelectedItem() to a ComboBoxElement, and get the image for it :)
Was This Post Helpful? 0
  • +
  • -

#5 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-November 11

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 12:29 PM

Thank you for your suggestion Casi0o but i'd like to avoid refactoring too much.

I've found my problem so far is that the inner if statements condition isn't being met do i have the syntax right?

static class ComboListener implements ActionListener {
        @Override
   public void actionPerformed(ActionEvent e) {
       
            if (combochoice == 1)
             {
                 if (DeskList.getSelectedItem().equals("Lenovo C325"));
                 {
                     
                 }
             }
            else
            {
            
            }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 01:11 PM

And what is the variable combochoice ?
Where is it defined ? What changes its value ?
Was This Post Helpful? 0
  • +
  • -

#7 nexus490  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-November 11

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 01:13 PM

the variable is declared outside of the main and it has initial value of 1. Here is the whole code i think it will make things a bit easier.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CWSwing extends JFrame {
    
   private static ImageIcon image1, image2, image3, image4, image5, image6;
   private static  JLabel imglabel1, imglabel2, imglabel3, imglabel4, imglabel5, imglabel6 = new JLabel();
   
   
   
   CWSwing()
   {
       image1 = new ImageIcon(getClass().getResource("2320.jpg"));
       image2 = new ImageIcon(getClass().getResource("5733.jpg"));
       image3 = new ImageIcon(getClass().getResource("C325.jpg"));
       image4 = new ImageIcon(getClass().getResource("G575.jpg"));
       image5 = new ImageIcon(getClass().getResource("Monza C1.jpg"));
       image6 = new ImageIcon(getClass().getResource("Z5771.jpg"));
       
       imglabel1 = new JLabel(image1);
       imglabel2 = new JLabel(image2);
       imglabel3 = new JLabel(image3);
       imglabel4 = new JLabel(image4);
       imglabel5 = new JLabel(image5);
       imglabel6 = new JLabel(image6);
       
       p6.add(imglabel3);
       p6.add(imglabel1);
       p6.add(imglabel2);
       p6.add(imglabel4);
       p6.add(imglabel5);
       p6.add(imglabel6);
       
   }
   
   
   
   static JPanel p6 = new JPanel();
   
   static JPanel p7inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   static JPanel p7inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   static JPanel p7inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    

    static JLabel btnSelectedLbl;
    static JRadioButton aButton;
    static JRadioButton bButton;
    
    static String[] CompNames = {"Lenovo C325", "ACER Z5771", "Dell Inspiron One  2320"};
    static String[] LapNames = {"ADVENT Monza C1", "Lenovo G575", "ACER Aspire 5733"};
 
    static JComboBox DeskList; 
    static JComboBox LapList;
    
    
    static int combochoice = 1;
  
   
    
   static class MyTypeRadioListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
       
        if(e.getActionCommand().equals("Desktop"))
        {
            btnSelectedLbl.setText("Desktop");
            DeskList = new JComboBox(CompNames);
            p7inner2.add(DeskList);
            DeskList.setVisible(true);
            LapList.setVisible(false);
            combochoice = 1;
            System.out.println(combochoice);
           
        }
        else
        {
            btnSelectedLbl.setText("Laptop");
            LapList = new JComboBox(LapNames);
            DeskList.setVisible(false);
            LapList.setVisible(true);
            p7inner2.add(LapList);
            combochoice = 2;
            System.out.println(combochoice);   
        }    
       
        
    }
}
   
static class ComboListener implements ActionListener {
        @Override
   public void actionPerformed(ActionEvent e) {
       JComboBox DeskList = (JComboBox)e.getSource();
            
          if (combochoice == 1)
             {
                 if (DeskList.getSelectedItem().equals("Lenovo C325"));
                 {
                     System.out.println("we did it"); 
                 }
                 
             }
            else
            {
                
            
            }
    }
}
    public static void main(String[] args) {
       
   JFrame frame = new JFrame("Test");
   
   
   
   frame.setSize(1000, 700);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
   
   JPanel panel = new JPanel();
   
   panel.setLayout(new GridLayout(5,3,0,0));
   
   JPanel p1 = new JPanel();
   JPanel p2 = new JPanel();
   JPanel p3 = new JPanel();
   JPanel p4 = new JPanel(new GridLayout(3, 0));
   JPanel p4inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p4inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p4inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p5 = new JPanel(new GridLayout(3, 0));
   JPanel p5inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p5inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p5inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p7 = new JPanel(new GridLayout(3, 0));
   //JPanel p7inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   //JPanel p7inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   //JPanel p7inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p8 = new JPanel(new GridLayout(3, 0));
   JPanel p8inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p8inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p8inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   final JPanel p9 = new JPanel(new GridLayout(5, 0));
   JPanel p9inner = new JPanel(new FlowLayout(FlowLayout.CENTER));
   final JPanel p9inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p9inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p9inner4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   p9.setBackground(Color.red);
   p9inner.setBackground(Color.WHITE);
   p9inner2.setBackground(Color.green);
   p9inner3.setBackground(Color.yellow);
   p9inner4.setBackground(Color.MAGENTA);
   JPanel p10 = new JPanel(new GridLayout(2, 0));
   JPanel p10inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p10inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p11 = new JPanel();
   JPanel p12 = new JPanel();
   p12.setBackground(Color.WHITE);
   JPanel p13 = new JPanel(new GridLayout(4, 1));
   JPanel p13inner = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p13inner8 = new JPanel(new FlowLayout(FlowLayout.LEFT));
   JPanel p14 = new JPanel();
   JPanel p15 = new JPanel();     
   

   p1.setBorder(BorderFactory.createLineBorder(Color.black));
   p2.setBorder(BorderFactory.createLineBorder(Color.black));
   p3.setBorder(BorderFactory.createLineBorder(Color.black));
   p4.setBorder(BorderFactory.createLineBorder(Color.black));
   p5.setBorder(BorderFactory.createLineBorder(Color.black));
   p6.setBorder(BorderFactory.createLineBorder(Color.black));
   p7.setBorder(BorderFactory.createLineBorder(Color.black));
   p8.setBorder(BorderFactory.createLineBorder(Color.black));
   p9.setBorder(BorderFactory.createLineBorder(Color.black));
   p10.setBorder(BorderFactory.createLineBorder(Color.black));
   p11.setBorder(BorderFactory.createLineBorder(Color.black));
   p12.setBorder(BorderFactory.createLineBorder(Color.black));
   p13.setBorder(BorderFactory.createLineBorder(Color.black));
   p14.setBorder(BorderFactory.createLineBorder(Color.black));
   p15.setBorder(BorderFactory.createLineBorder(Color.black));
   p4inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p4inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p4inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p5inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p5inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p5inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p7inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p7inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p7inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p8inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p8inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p8inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p9inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p9inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p9inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p9inner4.setBorder(BorderFactory.createLineBorder(Color.black));
   p10inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p10inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner2.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner3.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner4.setBorder(BorderFactory.createLineBorder(Color.black));      
   p13inner5.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner6.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner7.setBorder(BorderFactory.createLineBorder(Color.black));
   p13inner8.setBorder(BorderFactory.createLineBorder(Color.black)); 
  
   JLabel label2 = new JLabel("Ye Old Computer Shoppe", JLabel.CENTER);
   label2.setFont(new Font("Monospaced", Font.BOLD, 24));
   label2.setForeground(Color.decode("0x27BC10"));
   
   JLabel selType = new JLabel("Select Type:");
   selType.setFont(new Font("Monospaced", Font.BOLD, 20));
   
   
   JLabel selDskspace = new JLabel("Select Hard Drive:");
   selDskspace.setFont(new Font("Monospaced", Font.BOLD, 20));
   JRadioButton radhd1 = new JRadioButton("500 GB    ",true);
   JRadioButton radhd2 = new JRadioButton("1 TB");
   JRadioButton radhd3 = new JRadioButton("2 TB");
   radhd1.setFont(new Font("Monospaced", Font.BOLD, 17));
   radhd2.setFont(new Font("Monospaced", Font.BOLD, 17));
   radhd3.setFont(new Font("Monospaced", Font.BOLD, 17));
   
   ButtonGroup HDgroup = new ButtonGroup();
   HDgroup.add(radhd1);
   HDgroup.add(radhd2);
   HDgroup.add(radhd3);
   
   
   
   
   JLabel selRam = new JLabel("Select RAM:");
   selRam.setFont(new Font("Monospaced", Font.BOLD, 20));
   JRadioButton radram1 = new JRadioButton("1 GB        ",true);
   JRadioButton radram2 = new JRadioButton("2 GB");
   JRadioButton radram3 = new JRadioButton("4 GB        ");
   JRadioButton radram4 = new JRadioButton("8 GB");
   radram1.setFont(new Font("Monospaced", Font.BOLD, 17));
   radram2.setFont(new Font("Monospaced", Font.BOLD, 17));
   radram3.setFont(new Font("Monospaced", Font.BOLD, 17));
   radram4.setFont(new Font("Monospaced", Font.BOLD, 17));
   
   ButtonGroup rambtngroup = new ButtonGroup();
   rambtngroup.add(radram1);
   rambtngroup.add(radram2);
   rambtngroup.add(radram3);
   rambtngroup.add(radram4);
  
   aButton = new JRadioButton("Desktop", true);
   bButton = new JRadioButton("Laptop");
   aButton.setFont(new Font("Monospaced", Font.BOLD, 18));
   bButton.setFont(new Font("Monospaced", Font.BOLD, 18));
   btnSelectedLbl = new JLabel( aButton.getText() );
   ButtonGroup myButtonGroup = new ButtonGroup();
   myButtonGroup.add(aButton);
   myButtonGroup.add(bButton);
   
   JLabel selMod = new JLabel("Select Model:");
   selMod.setFont(new Font("Monospaced", Font.BOLD, 20));
   
  
  
   ActionListener ComboListener = new ComboListener();
   DeskList = new JComboBox(CompNames);
   LapList = new JComboBox(LapNames);
   LapList.setVisible(false);
   DeskList.addActionListener(ComboListener);
   
   JLabel selOS = new JLabel("Select Operating System:");
   selOS.setFont(new Font("Monospaced", Font.BOLD, 20));
   JRadioButton OS1 = new JRadioButton("Windows 7 Home Premium",true);
   JRadioButton OS2 = new JRadioButton("Windows 7 Professional");
   JRadioButton OS3 = new JRadioButton("Linux Mint 12");
   OS1.setFont(new Font("Monospaced", Font.BOLD, 16));
   OS2.setFont(new Font("Monospaced", Font.BOLD, 16));
   OS3.setFont(new Font("Monospaced", Font.BOLD, 16));
   
   ButtonGroup myOSButtonGroup = new ButtonGroup();
   myOSButtonGroup.add(OS1);
   myOSButtonGroup.add(OS2);
   myOSButtonGroup.add(OS3);
   
   JLabel selExtra = new JLabel("Extras:");
   selExtra.setFont(new Font("Monospaced", Font.BOLD, 20));
   JCheckBox dvdwritechk = new JCheckBox("DVD Writer");
   JCheckBox webcamchk = new JCheckBox("Webcam");
   JCheckBox usbdrivechk = new JCheckBox("USB Pen Drive");
   JCheckBox extmousechk = new JCheckBox("Extra Mouse");
   JCheckBox extkeybrdchk = new JCheckBox("Extra Keyboard");
   JCheckBox laptopbag = new JCheckBox("Laptop Bag");
   dvdwritechk.setFont(new Font("Monospaced", Font.BOLD, 15));
   webcamchk.setFont(new Font("Monospaced", Font.BOLD, 15));
   usbdrivechk.setFont(new Font("Monospaced", Font.BOLD, 15));
   extmousechk.setFont(new Font("Monospaced", Font.BOLD, 15));
   extkeybrdchk.setFont(new Font("Monospaced", Font.BOLD, 15));
   laptopbag.setFont(new Font("Monospaced", Font.BOLD, 15));
   
   JLabel summary = new JLabel("Summary");
   summary.setFont(new Font("Monospaced", Font.BOLD, 18));
   
  
   CWSwing img = new CWSwing();
   
   ActionListener radioListener = new MyTypeRadioListener();
   aButton.addActionListener(radioListener);
   bButton.addActionListener(radioListener);
   
  
        
   panel.add(p1);
   panel.add(p2);
   panel.add(p3);
   panel.add(p4);
   panel.add(p5);   
   panel.add(p6);
   panel.add(p7);
   panel.add(p8);
   panel.add(p9);
   panel.add(p10);
   panel.add(p11);
   panel.add(p12);
   panel.add(p13);
   panel.add(p14);
   panel.add(p15);
   
   p4.add(p4inner);
   p4.add(p4inner2);
   p4.add(p4inner3);
   p5.add(p5inner);
   p5.add(p5inner2);
   p5.add(p5inner3);
   p7.add(p7inner);
   p7.add(p7inner2);
   p7.add(p7inner3);
   p8.add(p8inner);
   p8.add(p8inner2);
   p8.add(p8inner3);
   p9.add(p9inner);
   p9.add(p9inner2);
   p9.add(p9inner3);
   p9.add(p9inner4);
   p10.add(p10inner);
   p10.add(p10inner2);
   p13.add(p13inner);
   p13.add(p13inner2);
   p13.add(p13inner3);
   p13.add(p13inner4);
   p13.add(p13inner5);
   p13.add(p13inner6);
   p13.add(p13inner7);
   p13.add(p13inner8);
   
  
   
   p2.add(label2);
   p4inner.add(selType);
   p4inner2.add(aButton);
   p4inner2.add(bButton);
   p5inner.add(selDskspace);
   p5inner2.add(radhd1);
   p5inner2.add(radhd2);
   p5inner3.add(radhd3);
   p7inner.add(selMod);
   p7inner3.add(selOS);
   p8inner.add(selRam);
   p8inner2.add(radram1);
   p8inner2.add(radram2);
   p8inner3.add(radram3);
   p8inner3.add(radram4);
   p10inner.add(OS1);
   p10inner.add(OS2);
   p10inner2.add(OS3);
   p13inner.add(selExtra);
   p13inner3.add(dvdwritechk);
   p13inner4.add(webcamchk);
   p13inner5.add(usbdrivechk);
   p13inner6.add(extmousechk);
   p13inner7.add(extkeybrdchk);
   p13inner8.add(laptopbag);
   p9inner.add(summary);
   p9inner2.add(btnSelectedLbl);
   p7inner2.add(DeskList);
 
   
   
   frame.add(panel);
   frame.setVisible(true);     
        
}
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How do I use an action listener to choose an image to display?

Posted 04 December 2011 - 01:51 PM

You should definitively learn how to use array before tempting to make a project like that
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1