Results 1 to 9 of 9
  1. #1
    retrosleeper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default actionlistener/itemStateChange help?

    Im sorry if this sounds really silly but I could do with some help with a bit of code.
    I have setup a jcombobox in my GUI from an array of months i.e jan, feb etc.
    What I am trying to do is write an actionlistener/itemStateChange to getselectedindex of the combobox and then at the moment just System.out.println(i)
    But I am really struggling as I cannot seem to get it to work. Hope you can help
    thank you :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: actionlistener/itemStateChange help?

    Post the code you need help with. Be sure to put it in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    retrosleeper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: actionlistener/itemStateChange help?

    Java Code:
    public class RainfallGUI extends JFrame implements ActionListener, ItemListener {
    	JPanel mainPanel = new JPanel();
    	JPanel topPanel = new JPanel();
    	JLabel testinfo = new JLabel("Rain",JLabel.CENTER);
    	
    	String[] YearString = { "1974", "1975", "1976", "1977", "1978", "1979","1980","1981","1982","1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012" };
    	String[] MonthString = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    	String[] DayString = { "1St", "2nd", "3rd", "4th", "5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"};
    	JComboBox DayList1 = new JComboBox(DayString);
    	JComboBox MonthList1 = new JComboBox(MonthString);
    	JComboBox YearList1 = new JComboBox(YearString);
    	int getdaynumber =0;
    	int getmonthnumber =0;
    	int getyearnumber =0;
    	
    	public RainfallGUI() {
    		super("rain fall data");
    		this.setSize(800,800);
    		this.setResizable(false);
    		this.setLayout(new BorderLayout());
    		this.add("Center", mainPanel);
    		
    		this.add("North", topPanel);
    		topPanel.setLayout(new GridLayout());
    		topPanel.add(DayList1);
    		topPanel.add(MonthList1);
    		topPanel.add(YearList1);
    	
    	this.setVisible(true);
    	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	
    }
    	public void actionPerformed(ActionEvent e) {
    		        JComboBox getmonthnumber = (JComboBox)e.getSource();
    		    	getmonthnumber = MonthList1.getSelectedIndex();
    		    	System.out.println(getmonthnumber);
    		    }
    		}
    im trying to get the getmonthnumber variable to change when i change the selection on the MonthList1 combobox and print to the system so know it has changed

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: actionlistener/itemStateChange help?

    I see two variables with that name. Which one are you having problems with?
    Does that code compile?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    retrosleeper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: actionlistener/itemStateChange help?

    im having a problem with the whole actionperformed part.
    it runs and shows no errors in eclipse but does not print anything to system when i change the combo box im rather new to java, i am sorry if its something really silly i have done and am thankful for any help

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: actionlistener/itemStateChange help?

    Why are there two variables with the same name?
    it runs and shows no errors in eclipse
    The posted code does not compile without errors. Without a clean compile I can not execute it.
    Fix the compiler errors and try it again.

    Also read up on listeners:
    http://docs.oracle.com/javase/tutori...nts/index.html
    Last edited by Norm; 05-01-2012 at 02:13 AM. Reason: Added link
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    retrosleeper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: actionlistener/itemStateChange help?

    http://www.java-forums.org/newreply....3171&noquote=1

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class RainfallGUI extends JFrame implements ActionListener, ItemListener {
        JPanel mainPanel = new JPanel();
        JPanel topPanel = new JPanel();  
        String[] YearString = { "1974", "1975", "1976", "1977", "1978", "1979","1980","1981","1982","1983","1984" };
        String[] MonthString = {"January","February","March","April","May","June","July","August","September"};
        String[] DayString = { "1St", "2nd", "3rd", "4th", "5th","6th","7th","8th","9th","10th","11th","12th"};
        JComboBox DayList1 = new JComboBox(DayString);
        JComboBox MonthList1 = new JComboBox(MonthString);
        JComboBox YearList1 = new JComboBox(YearString);
        int getdaynumber =0;
        int getmonthnumber =0;
        int getyearnumber =0;
         
        public RainfallGUI() {
            super("rain fall data");
            this.setSize(800,800);
            this.setResizable(false);
            this.setLayout(new BorderLayout());
            this.add("Center", mainPanel);
             
            this.add("North", topPanel);
            topPanel.setLayout(new GridLayout());
            topPanel.add(DayList1);
            topPanel.add(MonthList1);
            topPanel.add(YearList1);
         
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
         
    }
        public void actionPerformed(ActionEvent e) {
        	MonthList1 = (JComboBox) e.getSource();
                    getmonthnumber = MonthList1.getSelectedIndex();
                    System.out.println(getmonthnumber);
                }
    	@Override
    	public void itemStateChanged(ItemEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
            }
    I have removed/corrected some things and made the arrays smaller so they fit on the page this should now compile
    cheers

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: actionlistener/itemStateChange help?

    What happens when you execute it?

    Have you looked at the tutorial on how to write listeners?
    Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: actionlistener/itemStateChange help?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:59 PM
  2. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  3. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM
  4. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 07:43 AM
  5. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •