Results 1 to 9 of 9

Thread: JButton help

  1. #1
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default JButton help

    Ok, basically when I add my dndBtn to only my mainMenu JPanel it goes on there and works fine, but when I add it to both my mainMenu and my dice JPanels then it doesn't show up on the mainMenu then.
    So how do you put a JButton on two different JPanels because it doesn't seem to be as simple as just adding it.

    I want to be able to put in the commented out portion of the code but it doesn't work as intended.
    Java Code:
    package counter;
    
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class DrawPanel extends JFrame {
    	private static final long serialVersionUID = 1L;
    	
    	
    	Tools tools = new Tools();
    	public Container cp;
    	JButton switchToMain = new JButton("Switch to Main Menu");
    	JButton switchToDnd = new JButton("Swtich to DnD");
    	private JPanel cardPanel;
    	private CardLayout cl;
    	
    	public DrawPanel(){
    		cardPanel = new JPanel();		
    		cl = new CardLayout();
    		cardPanel.setLayout(cl);
    		
    		JButton diceBtn = new JButton("Roll a die");  //DICE BUTTON
    			diceBtn.setForeground(Color.RED);
    			diceBtn.setBackground(Color.BLACK);	
    		
    		JButton mainMenuBtn = new JButton("Main Menu"); // MENU BUTTON
    			mainMenuBtn.setForeground(Color.RED);
    			mainMenuBtn.setBackground(Color.BLACK);
    		
    		JButton dndBtn = new JButton("D&D Menu");		// DND BUTTON
    			dndBtn.setForeground(Color.RED);
    			dndBtn.setBackground(Color.BLACK);
    
    		JPanel mainMenu = new JPanel();					//MENU PANEL
    			mainMenu.setBackground(Color.RED);
    			mainMenu.add(dndBtn);
    ////////		//mainMenu.add(diceBtn);
    
    		JPanel dnd = new JPanel();	//DND PANEL
    			dnd.setBackground(Color.BLUE);
    			dnd.add(mainMenuBtn);
    			dnd.add(diceBtn);
    			
    		JPanel dice = new JPanel();	//DICE PANEL
    			dice.setBackground(Color.GREEN);
    /////////		//dice.add(dndBtn);
    /////////		//dice.add(mainMenuBtn);
    			dice.add(tools.diePanel);
    			
    		cardPanel.add(mainMenu, "1");
    		cardPanel.add(dnd, "2");
    		cardPanel.add(dice, "3");
    		
    		mainMenuBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("Hello");
    				cl.first(cardPanel);
    			}
    		});	
    		dndBtn.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("Hello");
    				cl.next(cardPanel);
    			}
    		});	
    		diceBtn.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				System.out.println("Hello");
    				cl.last(cardPanel);	
    			}
    		});
    		getContentPane().add(cardPanel);
    	}	
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    I vaguely recall reading that a Component can only be added to a single Container. Thus as soon as you add your button to a second Container it is removed from the first.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    The following code supports this:
    Java Code:
    import javax.swing.*;
    
    class Test {
        public static void main(String args[]) {
            JButton button = new JButton("hellO");
            JPanel p1 = new JPanel();
            p1.add(button);
            System.out.println(p1.getComponents().length);
            JPanel p2 = new JPanel();
            p2.add(button);
            System.out.println(p1.getComponents().length);
            System.out.println(p2.getComponents().length);
        }
    }

  4. #4
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    That explains why it is doing what it is doing but how can I work around that? Make several of the same button with a slightly different name and add them to the appropriate JPanels? It just sounds so inefficient.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Try with one more panel in contentPane that is separated from cardPanel and add all your buttons there. In that case they will always be visible...

  6. #6
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    I don't fully understand. I have tried this but am afraid I don't fully understand how to call the buttons that are in the panel in the contentPane into the Panels that are in the cardPanel. Can somebody please elaborate.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    As Junky said, a component can have only one parent. Think about it, if you could add the same component to more than one container, which would be returned by the getParent() method?

    The usual approach to providing more than one control (JButton, JMenuItem etc) with the same functionality is to construct an Action (usually a concretized AbstractAction) and share the Action between the components.

    db

  8. #8
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    Ok, I understand why the buttons can't be assigned to more then one panel, but if I have them in a panel assigned to the contentPane, then how to I call them from the contentPane to the other panels I have created? I have made it so I have another Jpanel called contentPane and added the buttons to that as Milovan suggested but do not know what to do with it. I can't find any method to call the button from the other panels.

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Why not add accessor methods to your various classes so you can pass references back and forth? For example, this pseudocode:
    Java Code:
    class A{
        B b = new B(this);
        public doSomething(){
            //do something
        }
    }
    
    class B{
        A a;
        public B(A a){
            this.a = a;
        }
    
        private callAnAMethod(){
            a.doSomething();
        }
    }

Similar Threads

  1. JButton help
    By ThrashingBoy in forum New To Java
    Replies: 2
    Last Post: 01-29-2011, 12:39 PM
  2. JButton Help
    By ravjot28 in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2010, 01:07 PM
  3. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 PM
  4. JButton Problem
    By wassim in forum AWT / Swing
    Replies: 6
    Last Post: 02-18-2009, 10:29 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 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
  •