Results 1 to 9 of 9
- 05-01-2012, 02:27 AM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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 :)
- 05-01-2012, 02:37 AM #2
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.
- 05-01-2012, 02:47 AM #3
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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); } }
- 05-01-2012, 02:54 AM #4
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.
- 05-01-2012, 02:59 AM #5
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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
- 05-01-2012, 03:07 AM #6
Re: actionlistener/itemStateChange help?
Why are there two variables with the same name?
it runs and shows no errors in eclipse
Fix the compiler errors and try it again.
Also read up on listeners:
http://docs.oracle.com/javase/tutori...nts/index.htmlLast edited by Norm; 05-01-2012 at 03:13 AM. Reason: Added link
If you don't understand my response, don't ignore it, ask a question.
- 05-01-2012, 03:17 AM #7
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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 } }
cheers
- 05-01-2012, 03:19 AM #8
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.
- 05-01-2012, 05:29 AM #9
Re: actionlistener/itemStateChange help?
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
ActionListener Help
By rakosky in forum AWT / SwingReplies: 4Last Post: 04-06-2012, 04:59 PM -
Please help with actionlistener
By ADustedEwok in forum New To JavaReplies: 5Last Post: 12-08-2011, 11:04 PM -
ActionListener
By jaylimix in forum Java AppletsReplies: 5Last Post: 11-06-2011, 07:05 PM -
Please Help With ActionListener
By Daman12 in forum New To JavaReplies: 29Last Post: 10-26-2011, 08:43 AM -
ActionListener NOT working
By zed420 in forum New To JavaReplies: 3Last Post: 12-20-2009, 04:40 PM
Bookmarks