I am working on one of my first GUI apps and I am having some trouble with alignment and sizing. I started from
Java's JTabbedPane tutorial and in the first tabbed pane i made the component it included be a QueryBuilder() which is a class I wrote that extends JPanel. The second pane holds something similar, though larger. However, the first pane is displaying everything in the center of the screen with a large gap around it. I want it to show in the top left. There are multiple JPanels inside of the QueryBuilder() panel and they are all in perfect alignment in relationship to each other. They _look_ left aligned. It's just that they look like they are left-aligned inside a box that is centered in the middle of the page. I have turned on all the borders but it looks like the box they are inside is the JPanel that QueryBuilder() makes... only it's been weirdly stretched out to fit the tabbed pane. Can someone clue me in? Below are code snippets.
QueryBuilder
--------------------------------
package cvg.corpus.gui;
import cvg.corpus.CorpusObjectFactory;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
//import java.util.HashMap;
//import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import cvg.corpus.CorpusException;
import cvg.corpus.CorpusQueryParam;
import cvg.corpus.ListPopulator;
//import cvg.corpus.Session;
//import cvg.corpus.Session_Segment;
//import cvg.corpus.Participant;
/**
* @author rwitmer
*/
/**
* This class creates the QueryBuilder screen and all its components, collects
* data from the user and submits it to ???.
*/
public class QueryBuilder extends JPanel implements ActionListener {
protected JTextField dateField1, dateField2;
protected JComboBox annotList, programList;
protected JTextField origField, destField;
protected JButton submit;
protected JRadioButton sessionsRB, segmentsRB, participantsRB;
//Use factory object instead of using database retriever, which you deleted.
protected CorpusObjectFactory factory = new CorpusObjectFactory();
protected CorpusQueryParam cqp = new CorpusQueryParam();
protected
String program;
protected Vector annotStrings;
protected ListPopulator lp = new ListPopulator();
/**
* Creates and lays out gui components. Asks the DatabaseController for data to
* populate fields.
* @throws CorpusException
*/
public QueryBuilder() throws CorpusException {
//Create and set up the window.
this.setLayout(new GridBagLayout());
this.setBorder(BorderFactory.createLineBorder(Color.red));
GridBagConstraints c = new GridBagConstraints();
//Make a panel
JPanel queryPanel = new JPanel();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0,0,0,0);
this.add(queryPanel, c);
//this.setSize(1, 1);//here's the top level panel "this"
//Add label.
JLabel label = new JLabel(" Build a Query:");
queryPanel.add(label);
queryPanel.setBorder(BorderFactory.createLineBorder(Color.black));
//Make a grid layout
queryPanel.setLayout(new GridLayout(0,2));
//Top Panel
JLabel label2 = new JLabel(" ");
queryPanel.add(label2);
ButtonGroup bGroup = new ButtonGroup();
sessionsRB = new JRadioButton("Show Session as Root", true);
bGroup.add(sessionsRB);
queryPanel.add(sessionsRB);
JLabel label3 = new JLabel("");
queryPanel.add(label3);
segmentsRB = new JRadioButton("Show Segment as Root", false);
bGroup.add(segmentsRB);
queryPanel.add(segmentsRB);
JLabel label4 = new JLabel("");
queryPanel.add(label4);
participantsRB = new JRadioButton("Show Participant as Root", false);
bGroup.add(participantsRB);
queryPanel.add(participantsRB);
JLabel label5 = new JLabel("");
queryPanel.add(label5);
queryPanel.add(new JSeparator(SwingConstants.HORIZONTAL));
queryPanel.add(new JSeparator(SwingConstants.HORIZONTAL));
queryPanel.setVisible(true);
--------------------------------------
etc....
and TabbedPaneDemo
--------------------------------------
package cvg.corpus.gui;
/*
* TabbedPaneDemo.java is a 1.4 example that requires one additional file:
* images/middle.gif.
*/
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.UIManager;
import cvg.corpus.CorpusException;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() throws CorpusException, IOException, UnsupportedAudioFileException, LineUnavailableException {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = createImageIcon("tree.bmp");
tabbedPane.addTab("Query Builder", icon, new QueryBuilder(),
"Build a query.");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
tabbedPane.addTab("Results Tree", icon, new TreeDemo(),
"Display the results hierarchically.");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
//Add the tabbed pane to this panel.
add(tabbedPane);
//Uncomment the following line to use scrolling tabs.
//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
-------------------------------------
etc