Results 1 to 3 of 3
- 06-25-2011, 04:14 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Help with Combo box and action listener
Hi,
I have problem with below code, i want to populate the value of combobox into textfield,
I have defined action listener- migButtonAction. seems its in the new actionlistener class-migButtonAction, the object combo1 and myField is not visible,
I know it cant be as its defined in main class, Please suggest me . I tried defining those object as public but its not allowing me.
Please help
-Sunil
Java Code:import javax.swing.*; import combo.exitButttonAction; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class test { public static void main(String args[]){ JFrame frame = new JFrame("Test"); JPanel panel1 = new JPanel(); String[] env = {"env1", "env2", "env3"}; JButton migButton = new JButton("mig"); JButton exitButton = new JButton("exit"); JTextField myField = new JTextField(20); JComboBox combo1 = new JComboBox(env); frame.add(panel1); panel1.add(migButton); panel1.add(exitButton); panel1.add(combo1); panel1.add(myField); migButton.addActionListener(new migButtonAction()); exitButton.addActionListener(new exitButtonAction()); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public class migButtonAction implements ActionListener{ public void actionPerformed(ActionEvent e){ // I Have problem here String str = (String)combo1.getSelectedValue(); myField.setText(str); } } public class exitButtonAction implements ActionListener{ public void actionPerformed(ActionEvent e){ System.exit(0); } } }
-
Your code won't work first and foremost because you don't have a true OOP class creating your GUI but rather are doing way too much in the static main method. So first you need to learn object oriented programming techniques like creating classes with fields and such. Then you can pass a reference of the main GUI to the listener and have the listener class call public methods on the main GUI object. Also, you have your listener classes as public inner classes, and that won't do either. Instead put them in their own files or have them as private inner classes.
So your JComboBox and your JTextField should both be declared as non-static class fields, and the GUI should also be created in a non-static way, likely in the class constructor. I'd give the GUI a public method, say getComboText() that returns a String representing the text of the selected combo box item (or null or throw exception if no item selected). I'd give the GUI a public void method say setTextField(String text) whereby an outside class can set the text of the JTextField. I'd pass a reference to the visualized main GUI to the listener objects that you create, allowing the listener to call the main GUI's public methods.Last edited by Fubarable; 06-25-2011 at 04:27 PM.
- 06-25-2011, 08:32 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Thanx buddy, I was able to get desired output with below code. Still i would like to know the beset approach if you could suggest please
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class test extends JFrame { JComboBox combo1; JTextField myField; public test(){ //JFrame frame = new JFrame("Test"); JPanel panel1 = new JPanel(); String[] env = {"env1", "env2", "env3"}; JButton migButton = new JButton("mig"); JButton exitButton = new JButton("exit"); myField = new JTextField(20); combo1 = new JComboBox(env); this.add(panel1); panel1.add(migButton); panel1.add(exitButton); panel1.add(combo1); panel1.add(myField); migButton.addActionListener(new migButtonAction()); exitButton.addActionListener(new exitButtonAction()); } public String getCombo(){ return (String)combo1.getSelectedItem(); } public void setTextField(String str){ myField.setText(str); } private class migButtonAction implements ActionListener{ public void actionPerformed(ActionEvent e){ // I Have problem here String str = getCombo(); setTextField(str); } } private class exitButtonAction implements ActionListener{ public void actionPerformed(ActionEvent e){ System.exit(0); } } public static void main(String[] args){ test t1 = new test(); t1.setSize(500,500); t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t1.setVisible(true); } }
Similar Threads
-
having issues getting my action listener to listen to my combo boxes
By dbomb in forum AWT / SwingReplies: 12Last Post: 05-04-2011, 09:31 PM -
Show label in panel, combo box, action listener
By cselic in forum Java 2DReplies: 4Last Post: 08-11-2010, 12:47 PM -
combo box listener?
By Prajin in forum AWT / SwingReplies: 1Last Post: 07-08-2010, 12:26 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
how to add links (Action) to a combo box
By impact in forum New To JavaReplies: 2Last Post: 05-03-2008, 07:04 AM


LinkBack URL
About LinkBacks

Bookmarks