File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Alignment woes in JPanel/JTabbedPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

Alignment woes in JPanel/JTabbedPanel

Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
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.

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());
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:");

//Make a grid layout
queryPanel.setLayout(new GridLayout(0,2));

//Top Panel
JLabel label2 = new JLabel(" ");
ButtonGroup bGroup = new ButtonGroup();
sessionsRB = new JRadioButton("Show Session as Root", true);
JLabel label3 = new JLabel("");
segmentsRB = new JRadioButton("Show Segment as Root", false);
JLabel label4 = new JLabel("");
participantsRB = new JRadioButton("Show Participant as Root", false);
JLabel label5 = new JLabel("");
queryPanel.add(new JSeparator(SwingConstants.HORIZONTAL));
queryPanel.add(new JSeparator(SwingConstants.HORIZONTAL));

and TabbedPaneDemo

package cvg.corpus.gui;

* 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;

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.

//Uncomment the following line to use scrolling tabs.

SCJP 1.4
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Please don't make duplicate posts.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Alignment woes in JPanel/JTabbedPanel
It's not a secret anymore!