Results 1 to 1 of 1
  1. #1
    albertkao is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default Position 5 buttons with GridBagConstraints in Swing

    How to position 5 buttons with GridBagConstraints in Swing so that the first button (B1) are at the first column, the next four buttons (B2, B3, B4, B5) are grouped together with some space ( ) between the first button and next four buttons, and the fifth button (B5) is placed at the end of the row of the screen?
    i.e.
    B1           B2B3B4B5
    The following code does not work - there is space between each button.
    The posted source is a simplication of my real source code, which is more complex, so I use GridBagLayout.
    Java Code:
    JButton button = new JButton("Note");
    c.fill = GridBagConstraints.LAST_LINE_START;
    c.gridx = 0;
    c.gridwidth = 1;
    c.gridy = 5;
    pane.add(button, c);
    
    button = new JButton("Remove");
    c.gridx = 5;
    pane.add(button, c);
    
    button = new JButton("Add");
    c.gridx = 6;
    pane.add(button, c);
    
    button = new JButton("Add Component");
    c.gridwidth = GridBagConstraints.RELATIVE;
    c.gridx = 7;
    pane.add(button, c);
    
    button = new JButton("Read");
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.gridx = 8;
    pane.add(button, c);
    Last edited by albertkao; 02-21-2011 at 06:19 PM.

Similar Threads

  1. GridBagConstraints help
    By noobgrammer in forum New To Java
    Replies: 5
    Last Post: 07-03-2010, 04:12 AM
  2. how to build reuasble swing buttons
    By adi in forum AWT / Swing
    Replies: 3
    Last Post: 12-28-2008, 05:04 PM
  3. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 AM
  4. GridBagConstraints
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-21-2008, 09:08 PM
  5. Demonstration of various Swing buttons
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •