Results 1 to 3 of 3
Thread: Problem with GridBagLayout
- 07-01-2007, 06:47 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
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!
Java Code:----------------------------------------------------- | Text Box 1 | |---------------------------------------------------| | Text Box 2 | |---------------------------------------------------| | Text Box 3 | |---------------------------------------------------| | Text Box 4 | |---------------------------------------------------| | Button | Button | Button | Button | |---------------------------------------------------| | Button | Button | Button | |------------------------------| | | Button | | |---------------------------------------------------|
Thanks.Java 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); } }
Daniel:o
- 07-01-2007, 06:52 PM #2
Senior Member
- Join Date
- Jun 2007
- Posts
- 114
- Rep Power
- 0
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:rolleyes:
- 07-01-2007, 06:57 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
Similar Threads
-
GridBagLayout
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:16 AM -
GridBagLayout...please help
By newtojava7 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:16 AM -
Problems with gridBaglayout when I resize the window
By Iyengar in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:43 PM -
Need a idea on GridBagLayout
By ddsuresh in forum AWT / SwingReplies: 1Last Post: 01-29-2008, 01:32 AM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks