Results 1 to 17 of 17

Thread: JButton issue

  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default JButton issue

    I need to create 3 buttons like this picture


    This is my code
    Java Code:
     import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       
       public class Layout1
       {
          public static void main(String [] args)
          {
             MyFrame frame = new MyFrame("Alan Window");
             frame.setSize(400, 300);
             frame.setLocation(100, 75);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          }
       }
       
       class MyFrame extends JFrame
       {
    		JPanel pan = new JPanel();
    		JRadioButton Rbtn = new JRadioButton("One", true);
    		JTextField txt1 = new JTextField("one"); 
    		JTextField txt2 = new JTextField("two");
    		JTextField txt3 = new JTextField("three"); 
    		JTextField txt4 = new JTextField("four"); 
    		
    	
    		JButton btn1 = new JButton("btn1");
    		JButton btn2 = new JButton("btn2");
    		JButton btn3 = new JButton("btn2");
    		JButton btn4 = new JButton("btn3");
    		
    		JPanel btnpanel = new JPanel();
    		
    		ButtonGroup group = new ButtonGroup();
    
    	
    	
          public MyFrame(String s)
          {
             super(s);
    			
    			setLayout ( new GridLayout (2, 3));
    			
    			add(Rbtn, BorderLayout.NORTH);
    			add(txt1);
    			add(txt2);
    			add(txt3);
    			add(txt4);
    			
    		
    			
    			btnpanel.add(btn1);
    			add(btn1);
    				
    					
    
          }
       }

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

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Could you give an example?

  4. #4
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    I did this but did not work
    JPanel btnpan = new JPanel();

    btnpan.setLayout(new GridLayout(1, 3));

    btnpan.add(btn1);
    add(btn1);
    btnpan.add(btn2);
    add(btn2);

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You need to (1) add all three buttons to btnpan then (2) add btnpan to the frame's content pane (the one with the 3x2 layout).

  6. #6
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    I understand adding 3 buttons to btnpan, but I did not understand the second part you said.
    How to add btnpan to frame content pane??

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How to add btnpan to frame content pane??

    You add btnpan the same way you added the radio button and the text fields.

  8. #8
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    I am sorry did not get, could you show how
    Java Code:
     import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       
       public class Layout1
       {
          public static void main(String [] args)
          {
             MyFrame frame = new MyFrame("Alan Window");
             frame.setSize(400, 300);
             frame.setLocation(100, 75);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          }
       }
       
       class MyFrame extends JFrame
       {
    		JPanel pan = new JPanel();
    		
    		JRadioButton Rbtn = new JRadioButton("One", true);
    		
    		JTextField txt1 = new JTextField("one"); 
    		JTextField txt2 = new JTextField("two");
    		JTextField txt3 = new JTextField("three"); 
    		JTextField txt4 = new JTextField("four"); 
    		
    		
    		JButton btnpan = new JButton();
    		
    		
    		
    		JButton btn1 = new JButton("btn1");
    		JButton btn2 = new JButton("btn2");
    		JButton btn3 = new JButton("btn2");
    		JButton btn4 = new JButton("btn3");
    		
    	
    	
    	
          public MyFrame(String s)
          {
             super(s);
    			
    			setLayout ( new GridLayout (2, 1));
    			
    			add(Rbtn);
    			
    			add(txt1);
    			add(txt2);
    			add(txt3);
    			add(txt4);
    			
    		
    			btnpan.add(btn1);
    			btnpan.add(btn2);
    			btnpan.add(btn3);
    			
    						
    		
    
    			add(btn1);
    			add(btn2);
    			//add(btn3);*/
    
    
    		
    				
    	
        }
      }

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

    Default

    Your adding the buttons to both the contentPane and the btnpan, don't do that.
    Java Code:
    			// adding buttons to the btnpan
    			btnpan.add(btn1);
    			btnpan.add(btn2);
    			btnpan.add(btn3);
    			
    			// ??? adding buttons to the contentPane too?
    			add(btn1);
    			add(btn2);
    Again, add btn1, btn2, btn3 to the btnpan only. Then add the btnpan to the contentPane the very same way that you're adding txt1, txt2, txt3 to the contentPane. You can do this.
    Last edited by Fubarable; 11-14-2010 at 01:03 AM.

  10. #10
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Thanks I got it. But the buttons showed very small not filling all the space.

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

  12. #12
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Java Code:
     import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       
       public class Layout1
       {
          public static void main(String [] args)
          {
             MyFrame frame = new MyFrame("Alan Window");
             frame.setSize(400, 300);
             frame.setLocation(100, 75);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          }
       }
       
       class MyFrame extends JFrame
       {
    		JPanel pan = new JPanel();
    		
    		JRadioButton Rbtn = new JRadioButton("One", true);
    		
    		JTextField txt1 = new JTextField("one"); 
    		JTextField txt2 = new JTextField("two");
    		JTextField txt3 = new JTextField("three"); 
    		JTextField txt4 = new JTextField("four"); 
    		
    		JPanel btnpan = new JPanel();
    
    					
    		JButton btn1 = new JButton("btn1");
    		JButton btn2 = new JButton("btn2");
    		JButton btn3 = new JButton("btn3");
    			
    	
    	
    	
          public MyFrame(String s)
          {
             super(s);
    			
    			setLayout ( new GridLayout (2, 3));
    			
    			add(Rbtn);
    			
    			add(txt1);
    			add(txt2);
    			add(txt3);
    			add(txt4);
    			
    				
    			btnpan.add(btn1);
    			btnpan.add(btn2);
    			btnpan.add(btn3);
    		
    			add(btnpan);				
    	
        }
      }

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

  14. #14
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    That's when I added did this
    btnpanel.add(btn1);
    add(btn1);

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

  16. #16
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Got it I set the layout to 3 rows Thanks man you the best.

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

Similar Threads

  1. JButton with Icon & text alignment issue
    By ShardaD in forum AWT / Swing
    Replies: 4
    Last Post: 01-11-2011, 11:10 AM
  2. Replies: 7
    Last Post: 08-29-2010, 10:33 AM
  3. JButton Help
    By ravjot28 in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2010, 01:07 PM
  4. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 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
  •