Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    Hi there!

    Here's the scenario:
    + Suppose we have a class GUI which extends JFrame and implements ActionListener.
    + We define a class Special_JPanel which extends JPanel.
    + A JComboBox, jCB, is a field in Special_JPanel.
    + A JPanel, baseJPnl, is a field in GUI.
    + A bunch of different components are placed into baseJPnl, including an instance of Special_JPanel.
    + The GUI class also functions as the driver class for my app.

    The Problem:
    + A part of GUI's state needs to be dependent upon the ActionEvent which is generated by jCB ( e.g., If the user selects item 'a' from jCB, the GUI field 'm' is assigned value 'x', but if the user selects 'b' from jCB, 'm' is assigned value 'y'. )

    The Current (Improper) Solution:
    + Set the access modifier of jCB to 'public' instead of 'private'. Then, it can be accessed directly from GUI.

    The Query:
    + Aside form defining all of Special_JPanel within the body of GUI, is there a better way to solve this problem? I've been taught that the fields of a class must always be 'private' to maintain proper encapsulation.


    Thanks in advance!
    Chris


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,942
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    Can you post some code that shows the problem?
    Are you asking about something like this: 'A'. addActionListener('F')
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    Here's code that uses the current (improper) solution:

    import javax.swing.JPanel;
    import javax.swing.JComboBox;
     
    import java.awt.Dimension;
     
    public class Special_JPanel extends JPanel{
     
    	public JComboBox<String> jCB;
     
    	public Special_JPanel(){
    		setPreferredSize( new Dimension(200, 200) );
    		String[] options = { "X", "Y" };
    		jCB = new JComboBox<String>(options);
    		jCB.setActionCommand("changeMode");
    		add(jCB);
     
    	}
     
    	public static void main(String[] args) {}
    }
     
    //----------------------------------------------------------
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    import java.awt.Color;
     
     
    public class GUI extends JFrame implements ActionListener{
     
    	private Special_JPanel sJP;
    	private JPanel baseJPnl;
    	private JPanel otherComponents;
     
    	private Integer mode;
     
    	public GUI(){
    		otherComponents = new JPanel();
    		otherComponents.setPreferredSize( new Dimension( 200, 300 ) );
     
    		sJP = new Special_JPanel();
    		baseJPnl = new JPanel();
    		mode = 1;
     
    		sJP.jCB.addActionListener(this);
    	}
     
    	public void createAndShowGUI(){
    		update();
    	}
     
    	private void update(){
    		revalidate();
     
    		if (mode == 1){
    			otherComponents.setBackground(Color.blue);
    		}
    		else{
    			otherComponents.setBackground(Color.green);
    		}
     
    		baseJPnl.add(otherComponents);
    		baseJPnl.add(sJP);
     
    		add(baseJPnl);
     
    		pack();
    		setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		GUI g = new GUI();
    		g.createAndShowGUI();
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
     
    		if ( e.getActionCommand().equals("changeMode") ){
    			if ( sJP.jCB.getSelectedItem().equals("X") ){
    				mode = 1;
    			}
    			else if ( sJP.jCB.getSelectedItem().equals("Y") ){
    				mode = 2;
    			}
    			update();
    		}
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,942
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    code that uses the current (improper) solution:
    Can you explain why this is an improper solution?
    I don't see any comments in the code about any problems or anything?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    It's improper because the first class, Special_JPanel, has a field which is left as public. --And, this is done so that the first class can access the JComboBox, jCB, for the sake of 'hearing' its ActionEvent. While I have been taught that this is not the proper way to code in OOED languages, it's the only way that I have been able to make my driver class, GUI, respond to an event which occurs within Special_JPanel.

    I'm sure that there's got to be some way to do this, but I've always found awt and javax.swing to be confusing.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,942
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Let 'A' be an extension of JPanel, 'B' is a JPanel, and 'F' is a JFrame. Suppose B.add(A), and F.add(B). How do I access an ActionEvent, which occurs within 'A', from 'F'?

    The two classes are too tightly connected to be separate, external classes.
    The Strings: changeMode, X and Y should all be defined and used in one class.
    mode is something defined and used in the other class.
    Perhaps a method setMode() in the big class could be called from the smaller listener class.
    I don't know how to connect the "X" to a 1 and the "Y" to a 2. Perhaps an enum
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to Add JPanel to JFrame during run-time
    By patelramanna in forum AWT / Java Swing
    Replies: 2
    Last Post: December 12th, 2012, 07:32 PM
  2. how to add a JFrame listener for a JPanel
    By netll in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 06:54 AM
  3. Dynamically add a Jpanel to a Jframe
    By Closet_Rambo in forum AWT / Java Swing
    Replies: 6
    Last Post: October 3rd, 2011, 03:51 AM
  4. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  5. Add JScrollPane to a JPanel
    By alibm in forum AWT / Java Swing
    Replies: 2
    Last Post: March 7th, 2011, 01:59 PM

Tags for this Thread