[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

Java Swing ActionListener

I would like to get actionlistener method once for all four buttons
I know there is  a possibility but something is going wrong please someone help me :-(

find an attached snippet of code,

Thnxx,
Rachel
import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
public class GridLayoutForm extends JFrame {
 
        
         JTextField  tF1, tF2, tF3, tF4;
         JTextArea jArea;
         JRadioButton rad1, rad2,rad3,rad4;
         JCheckBox cB1, cB2, cB3, cB4;
         JButton RdB, CbB, TfB, RCTB;
         
         
   
    
    public GridLayoutForm() {
 
        super("Whatever");
        myPanel panel1 = new myPanel();
        add(panel1);
        pack();
        setVisible(true);
        setSize(800, 350);
    }
 
    public class myPanel extends JPanel implements ActionListener  {
    
                public myPanel() {
 
            setLayout(new BorderLayout());
            JPanel pan1 = new JPanel();
            
            pan1.setLayout(new java.awt.GridLayout(7, 4));
 
            pan1.add(new JLabel("Label1"));
            pan1.add(new JLabel("Label2"));
            pan1.add(new JLabel("Label3"));
            pan1.add(new JPanel());
            pan1.add(new JPanel());
     
            rad1 = new JRadioButton();
            pan1.add(rad1);
            rad1.setEnabled(false);
            cB1 = new JCheckBox();
            pan1.add(cB1);
            cB1.setEnabled(false);
            tF1 = new JTextField();
            tF1.setEditable(false);
            pan1.add(tF1);
            pan1.add(new JPanel());
            pan1.add(new JPanel());
           
            rad2 = new JRadioButton();
            pan1.add(rad2);
            rad2.setEnabled(false);
            cB2 = new JCheckBox();
            pan1.add(cB2);
            cB2.setEnabled(false);
            ttF2 = new JTextField();
            tF2.setEditable(false);
            pan1.add(tF2);
            pan1.add(new JPanel());
            pan1.add(new JPanel());
 
            rad3 = new JRadioButton();
            pan1.add(rad3);
            rad3.setEnabled(false);
            cB3 = new JCheckBox();
            pan1.add(cB3);  
            cB3.setEnabled(false);
            tF3 = new JTextField();
            tF3.setEditable(false);
            pan1.add(tF3);
            pan1.add(new JPanel());
            pan1.add(new JPanel());
            
            rad4 = new JRadioButton();
            pan1.add(rad4);
            rad4.setEnabled(false);
            cB4 = new JCheckBox();
            pan1.add(cB4);
            cB4.setEnabled(false);
            tF4 = new JTextField();
            tF4.setEditable(false);
            pan1.add(textF4);
 
            pan1.add(new JPanel());
            jArea = new JTextArea(5, 20);
            jArea.setEditable(false);
            pan1.add(jArea);
            JScrollPane jscroll = new JScrollPane(
                    jArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            pan1.add(new JPanel());
            
            RdB.addActionListener(this);
            CbB.addActionListener(this);
            TfB.addActionListener(this);
            RCTB.addActionListener(this);
            
            pan1.add(RdB);
            pan1.add(CbB);
            pan1.add(TfB);
            pan1.add(RCTB);
                         
            pan1.add(new JPanel());  
            pan1.add(new JLabel("Description"));
            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.add(pan1, BorderLayout.WEST);
            this.add(wrapper, BorderLayout.NORTH);
            this.add(jscroll);    
            
        }
 
                public void actionPerformed(ActionEvent event) {
                        // TODO Auto-generated method stub
                        if(event.getSource()== RdB){
                                 rad1.setSelected(true);
                                 cB1.setSelected(false); 
                                  jArea.setEditable(false);
                        }
                        
                        if(event.getSource()== CbB){
                                
                                cB1.setSelected(true);
                                tF4.setEditable(false);
                                jArea.setEditable(false);
 
                        }
                        
                        if(event.getSource()== TfB){
                                
                                textF1.setEditable(true);
                                cB3.setSelected(false);
 
                        }
 
                        }
                        
                }
                
                }
    
 
    // Main Program that starts Execution
    public static void main(String[] args) {
        new GridLayoutForm();
    }
 
}

Open in new window

0
rachelee
Asked:
rachelee
1 Solution
 
objectsCommented:
try this

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GridLayoutForm extends JFrame {

      JTextField tF1, tF2, tF3, tF4;
      JTextArea jArea;
      JRadioButton rad1, rad2, rad3, rad4;
      JCheckBox cB1, cB2, cB3, cB4;
      JButton RdB, CbB, TfB, RCTB;

      public GridLayoutForm() {

            super("Whatever");
            myPanel panel1 = new myPanel();
            add(panel1);
            pack();
            setVisible(true);
            setSize(800, 350);
      }

      public class myPanel extends JPanel implements ActionListener {

            public myPanel() {

                  setLayout(new BorderLayout());
                  JPanel pan1 = new JPanel();

                  pan1.setLayout(new java.awt.GridLayout(7, 4));

                  pan1.add(new JLabel("Label1"));
                  pan1.add(new JLabel("Label2"));
                  pan1.add(new JLabel("Label3"));
                  pan1.add(new JPanel());
                  pan1.add(new JPanel());

                  rad1 = new JRadioButton();
                  pan1.add(rad1);
                  rad1.setEnabled(false);
                  cB1 = new JCheckBox();
                  pan1.add(cB1);
                  cB1.setEnabled(false);
                  tF1 = new JTextField();
                  tF1.setEditable(false);
                  pan1.add(tF1);
                  pan1.add(new JPanel());
                  pan1.add(new JPanel());

                  rad2 = new JRadioButton();
                  pan1.add(rad2);
                  rad2.setEnabled(false);
                  cB2 = new JCheckBox();
                  pan1.add(cB2);
                  cB2.setEnabled(false);
                  tF2 = new JTextField();
                  tF2.setEditable(false);
                  pan1.add(tF2);
                  pan1.add(new JPanel());
                  pan1.add(new JPanel());

                  rad3 = new JRadioButton();
                  pan1.add(rad3);
                  rad3.setEnabled(false);
                  cB3 = new JCheckBox();
                  pan1.add(cB3);
                  cB3.setEnabled(false);
                  tF3 = new JTextField();
                  tF3.setEditable(false);
                  pan1.add(tF3);
                  pan1.add(new JPanel());
                  pan1.add(new JPanel());

                  rad4 = new JRadioButton();
                  pan1.add(rad4);
                  rad4.setEnabled(false);
                  cB4 = new JCheckBox();
                  pan1.add(cB4);
                  cB4.setEnabled(false);
                  tF4 = new JTextField();
                  tF4.setEditable(false);
                  pan1.add(tF4);

                  pan1.add(new JPanel());
                  jArea = new JTextArea(5, 20);
                  jArea.setEditable(false);
                  pan1.add(jArea);
                  JScrollPane jscroll = new JScrollPane(jArea,
                              JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                              JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                  pan1.add(new JPanel());

                  RdB.addActionListener(this);
                  CbB.addActionListener(this);
                  TfB.addActionListener(this);
                  RCTB.addActionListener(this);

                  pan1.add(RdB);
                  pan1.add(CbB);
                  pan1.add(TfB);
                  pan1.add(RCTB);

                  pan1.add(new JPanel());
                  pan1.add(new JLabel("Description"));
                  JPanel wrapper = new JPanel(new BorderLayout());
                  wrapper.add(pan1, BorderLayout.WEST);
                  this.add(wrapper, BorderLayout.NORTH);
                  this.add(jscroll);

            }

            public void actionPerformed(ActionEvent event) {
                  // TODO Auto-generated method stub
                  if (event.getSource() == RdB) {
                        rad1.setSelected(true);
                        cB1.setSelected(false);
                        jArea.setEditable(false);
                  }

                  else if (event.getSource() == CbB) {

                        cB1.setSelected(true);
                        tF4.setEditable(false);
                        jArea.setEditable(false);

                  }

                  else  if (event.getSource() == TfB) {

                        tF1.setEditable(true);
                        cB3.setSelected(false);

                  }
                  else if (event.getSource() == RCTB) {
                  }
                  

            }

      }

      // Main Program that starts Execution
      public static void main(String[] args) {
            new GridLayoutForm();
      }

}
0
 
racheleeAuthor Commented:
Dear Objects ,

it didnt work dat way...because we haven't created buttons at alll...hahahah

See my new code here it works:

However thnx a lot..I got an idea after I appended ur code only..so thankkkkkk you again,

Regards,
Rachel


import java.awt.BorderLayout;
	import java.awt.Event;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
 
	import javax.swing.JButton;
	import javax.swing.JCheckBox;
	import javax.swing.JFrame;
	import javax.swing.JLabel;
	import javax.swing.JPanel;
	import javax.swing.JRadioButton;
	import javax.swing.JRadioButtonMenuItem;
	import javax.swing.JScrollPane;
	import javax.swing.JTextArea;
	import javax.swing.JTextField;
 
	public class GridLayout2 extends JFrame {
 
	      JTextField tF1, tF2, tF3, tF4;
	      JTextArea jArea;
	      JRadioButton rad1, rad2, rad3, rad4;
	      JCheckBox cB1, cB2, cB3, cB4;
	      JButton RdB, CbB, TfB, RCTB;
 
	      public GridLayout2() {
 
	            super("Whatever");
	            myPanel panel1 = new myPanel();
	            add(panel1);
	            pack();
	            setVisible(true);
	            setSize(800, 350);
	      }
 
	      public class myPanel extends JPanel implements ActionListener  {
 
	            public myPanel() {
 
	                  setLayout(new BorderLayout());
	                  JPanel pan1 = new JPanel();
 
	                  pan1.setLayout(new java.awt.GridLayout(7, 4));
 
	                  pan1.add(new JLabel("Label1"));
	                  pan1.add(new JLabel("Label2"));
	                  pan1.add(new JLabel("Label3"));
	                  pan1.add(new JPanel());
	                  pan1.add(new JPanel());
 
	                  rad1 = new JRadioButton();
	                  pan1.add(rad1);
	                  rad1.setEnabled(false);
	                  cB1 = new JCheckBox();
	                  pan1.add(cB1);
	                  cB1.setEnabled(false);
	                  tF1 = new JTextField();
	                  tF1.setEditable(false);
	                  pan1.add(tF1);
	                  pan1.add(new JPanel());
	                  pan1.add(new JPanel());
 
	                  rad2 = new JRadioButton();
	                  pan1.add(rad2);
	                  rad2.setEnabled(false);
	                  cB2 = new JCheckBox();
	                  pan1.add(cB2);
	                  cB2.setEnabled(false);
	                  tF2 = new JTextField();
	                  tF2.setEditable(false);
	                  pan1.add(tF2);
	                  pan1.add(new JPanel());
	                  pan1.add(new JPanel());
 
	                  rad3 = new JRadioButton();
	                  pan1.add(rad3);
	                  rad3.setEnabled(false);
	                  cB3 = new JCheckBox();
	                  pan1.add(cB3);
	                  cB3.setEnabled(false);
	                  tF3 = new JTextField();
	                  tF3.setEditable(false);
	                  pan1.add(tF3);
	                  pan1.add(new JPanel());
	                  pan1.add(new JPanel());
 
	                  rad4 = new JRadioButton();
	                  pan1.add(rad4);
	                  rad4.setEnabled(false);
	                  cB4 = new JCheckBox();
	                  pan1.add(cB4);
	                  cB4.setEnabled(false);
	                  tF4 = new JTextField();
	                  tF4.setEditable(false);
	                  pan1.add(tF4);
 
	                  pan1.add(new JPanel());
	                  jArea = new JTextArea(5, 20);
	                  jArea.setEditable(false);
	                  pan1.add(jArea);
	                  JScrollPane jscroll = new JScrollPane(jArea,
	                              JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
	                              JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	                  pan1.add(new JPanel());
	                  
	                  RdB = new JButton("Radio");
	                  CbB = new JButton("Radio");
	                  TfB = new JButton("Radio");
	                  RCTB = new JButton("Radio");
 
	                  RdB.addActionListener(this);
	                  CbB.addActionListener(this);
	                  TfB.addActionListener(this);
	                  RCTB.addActionListener(this);
 
	                  pan1.add(RdB);
	                  pan1.add(CbB);
	                  pan1.add(TfB);
	                  pan1.add(RCTB);
 
	                  pan1.add(new JPanel());
	                  pan1.add(new JLabel("Description"));
	                  JPanel wrapper = new JPanel(new BorderLayout());
	                  wrapper.add(pan1, BorderLayout.WEST);
	                  this.add(wrapper, BorderLayout.NORTH);
	                  this.add(jscroll);
 
	            }
 
				public void actionPerformed(ActionEvent event) {
					// TODO Auto-generated method stub
					if (event.getSource() == RdB) {
                        rad1.setSelected(true); 
                        tF4.setEditable(false);
                        jArea.setEditable(false);
                  }
 
                  else if (event.getSource() == CbB) {
 
                        cB1.setSelected(true);
            
 
                  }
 
                  else  if (event.getSource() == TfB) {
 
                        tF1.setEditable(true);
                   
                        jArea.setEditable(false);
                        
 
                  }
                  else if (event.getSource() == RCTB) {
                	  
                	  rad1.setSelected(true);
                      rad2.setSelected(true);
                
                  }
                  
					
				}
 
				
	            
	      }
 
	           
 
	      // Main Program that starts Execution
	      public static void main(String[] args) {
	            new GridLayout2();
	      }
 
	}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now