Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default 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);  
                }  
            }  
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 05:27 PM.

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Replies: 12
    Last Post: 05-04-2011, 10:31 PM
  2. Replies: 4
    Last Post: 08-11-2010, 01:47 PM
  3. combo box listener?
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-08-2010, 01:26 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  5. how to add links (Action) to a combo box
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 08:04 AM

Posting Permissions

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