Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2007, 07:47 PM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
Problem with GridBagLayout
Hi, Im trying to make a GUI for a docking control program. It comprises of 4 text boxes at the top, then 8 buttons under these.
The text boxes have to be done using a BoxLayout, and the buttons using a GridBagLayout. They must then both be put in a JPanel.
I have got the text boxes sorted, and have been doing a lot of reading on how to make GridBagLayouts. I think the code I have should have the layout made. I belive the problem is that the GridBagLayout is hiding somwhere behind the panel I wish to put it on.
I have tried a few ways of putting the GridBagLayout on the panel, but it isn't having any of it!
Code:
----------------------------------------------------- | Text Box 1 | |---------------------------------------------------| | Text Box 2 | |---------------------------------------------------| | Text Box 3 | |---------------------------------------------------| | Text Box 4 | |---------------------------------------------------| | Button | Button | Button | Button | |---------------------------------------------------| | Button | Button | Button | |------------------------------| | | Button | | |---------------------------------------------------|
Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.color.*; import java.applet.Applet; public class GUI extends Applet { JFrame frame; // TEXT FIELDS JTextField seaShipsID; JTextField queueShipsID; JTextField dockShipsID; JTextField shipMessages; // BUTTONS JButton dockFromSea; JButton dockFromQueue; JButton setSail; JButton queue; JButton admit; JButton dequeue; JButton refuse; JButton bye; // PANELS JPanel textFieldPanel; JPanel mainPanel; // GRID BAG LAYOUT GridBagLayout buttonLayout; GridBagConstraints buttonConstraints; public static void main(String[] args) { GUI gui = new GUI(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonLayout = new GridBagLayout(); buttonConstraints = new GridBagConstraints(); textFieldPanel = new JPanel(); textFieldPanel.setBackground(Color.lightGray); textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.Y_AXIS)); mainPanel = new JPanel(); mainPanel.setBackground(Color.lightGray); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)); // BUTTONS dockFromSea = new JButton("Dock From Sea"); dockFromQueue = new JButton("Dock From Queue"); setSail = new JButton("Set Sail"); queue = new JButton("Queue"); admit = new JButton("Admit"); dequeue = new JButton("Dequeue"); refuse = new JButton("Refuse"); bye = new JButton("Bye"); // BUTTON LAYOUT buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dockFromSea, 0,0,2,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(queue, 0,2,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(admit, 0,3,1,1); buttonConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent(admit, 0,4,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dockFromQueue, 1,0,2,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(dequeue, 1,2,1,1); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(refuse, 1,3,2,2); buttonConstraints.fill = GridBagConstraints.BOTH; addComponent(setSail, 2,1,3,1); // TEXT FIELDS seaShipsID = new JTextField("ID of boats at sea"); queueShipsID = new JTextField("ID of boats in queue to dock"); dockShipsID = new JTextField("ID of the boats in dock"); shipMessages = new JTextField("No Message"); // TEXT FIELD PANEL IMPLEMENTATION frame.getContentPane().add(BorderLayout.NORTH, textFieldPanel); textFieldPanel.add(seaShipsID); textFieldPanel.add(queueShipsID); textFieldPanel.add(dockShipsID); textFieldPanel.add(shipMessages); // MAIN PANEL IMPLEMENTATION frame.getContentPane().add(BorderLayout.NORTH, mainPanel); mainPanel.add(textFieldPanel); frame.setSize(300,200); frame.setVisible(true); } private void addComponent(Component component, int row, int column, int width, int height) { buttonConstraints.gridx = column; buttonConstraints.gridy = row; buttonConstraints.gridwidth = width; buttonConstraints.gridheight = height; buttonLayout.setConstraints(component, buttonConstraints); add(component); } }
Thanks.

Daniel
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2007, 07:52 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
I'm not sure if that is the problem but by implementing ActionListener you'll need to have an implementation for that method.

Greetings.

Albert
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2007, 07:57 PM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
I would recommend adding an ActionListener to each individual button, thereby deleting the need for the memory check and cleaning up your code.

Greetings.

Felissa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridBagLayout newtojava7 New To Java 2 03-07-2008 01:16 AM
GridBagLayout...please help newtojava7 Advanced Java 1 02-17-2008 02:16 AM
Problems with gridBaglayout when I resize the window Iyengar AWT / Swing 1 02-17-2008 12:43 AM
Need a idea on GridBagLayout ddsuresh AWT / Swing 1 01-29-2008 02:32 AM
gridbaglayout newtojava7 New To Java 4 01-27-2008 09:03 PM


All times are GMT +3. The time now is 01:15 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org