Results 1 to 3 of 3

Thread: JButton help

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

    Default JButton help

    Ultimately I am trying to write a program with a main menu where you press a button, and it will take you to another menu with other buttons. So if there is a better way to do this then just removing and adding buttons from the ContentPane then let me know please.

    But my current problem is I don't see why when I push one of my buttons it doesn't get removed and replaced with the other one that I want. As you can see by my actionPerformed it should remove() the button pressed and just add() the other button that I have. But it isn't, I can press the button and it recognizes the button press (Hence the system.out.print("hello")) but it is only removing the button and not replacing it with the other.

    Java Code:
    package counter;
    
    import java.awt.Color;
    import javax.swing.JFrame;
    
    public class CounterMain {
    	public static void main(String[] args) {
    		DrawPanel panel = new DrawPanel();
    		panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel.setSize(800,600);
    		panel.setVisible(true);
    
    	}
    }
    Java Code:
    package counter;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class DrawPanel extends JFrame implements ActionListener{
    	
    	public Container cp;
    	JButton switchToMain = new JButton("Switch to Main Menu");
    	JButton switchToDnd = new JButton("Swtich to DnD");
    
    	
    	public DrawPanel(){
    		cp = getContentPane();
    		cp.setBackground(Color.BLACK);
    		cp.setLayout(new FlowLayout());
    		cp.add(switchToMain);
    		switchToMain.addActionListener(this);	
    		switchToDnd.addActionListener(this);	
    				
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("Hello");
    		if(e.getSource() == switchToMain){
    			cp.add(switchToDnd);
    			cp.remove(switchToMain);
    		}
    		
    		if(e.getSource() == switchToDnd){
    			cp.add(switchToMain);
    			cp.remove(switchToDnd);
    		}
    		cp.repaint();		
    	}
    }

  2. #2
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class CustomerNew{

    CreateSlider_7 cs_7=new CreateSlider_7();

    JFrame frm;
    JPanel jp;
    JButton SMM = new JButton("SMM");
    JButton SD = new JButton("SD");


    public CustomerNew() {
    frm = new JFrame();
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    frm.setSize(1000, 735);
    frm.setVisible(true);
    jp = new JPanel();
    jp.setBounds(10, 280, 110, 270);
    frm.add(jp);
    jp.add(SMM);
    SMM.setBounds(2, 25, 50, 20);

    SMM.addActionListener(new ButtonClicked());
    SD.addActionListener(new ButtonClicked());
    }

    class ButtonClicked implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    System.out.println("button1 clicked" + e.getActionCommand());
    String btnname = e.getActionCommand();
    if (btnname.equals("SMM"))
    cs_7.callingmain();
    if (btnname.equals("SD"))
    cs_7.frame.setVisible(false);

    }
    }



    public static void main(String[] args) {
    CustomerNew a= new CustomerNew();


    }

    class CreateSlider_7 extends JPanel {
    JFrame frame;
    public void callingmain() {
    frame = new JFrame("Slider Frame");
    frame.setContentPane(cs_7);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_C LOSE);
    frame.add(SD);

    }

    }

    }

    try this code. hope this will help u.

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

    Default

    Quote Originally Posted by ThrashingBoy View Post

    Java Code:
    //....
    
    
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("Hello");
    		if(e.getSource() == switchToMain){
    			cp.add(switchToDnd);
    			cp.remove(switchToMain);
    		}
    		
    		if(e.getSource() == switchToDnd){
    			cp.add(switchToMain);
    			cp.remove(switchToDnd);
    		}
    		cp.repaint();		
    	}

    One quick fix is to revalidate the container after making the changes:

    Java Code:
    //....
    
    
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("Hello");
    		if(e.getSource() == switchToMain){
    			cp.add(switchToDnd);
    			cp.remove(switchToMain);
    		}
    		
    		if(e.getSource() == switchToDnd){
    			cp.add(switchToMain);
    			cp.remove(switchToDnd);
    		}
    		((JPanel)cp).revalidate();
    		cp.repaint();		
    	}

    As this tells the layout managers to layout the new components in the container. Then repaint redraws the newly laid out contents.

    A more robust solution though is to use a CardLayout to allow you to swap JPanels or other JComponents. Please check out the Swing tutorial on this for the details.

Similar Threads

  1. JButton Help
    By ravjot28 in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2010, 01:07 PM
  2. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 PM
  3. How to insert a JButton?
    By aRTx in forum New To Java
    Replies: 1
    Last Post: 04-02-2009, 09:43 PM
  4. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM
  5. Problem with JButton
    By Marcus in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:56 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
  •