File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Left-align in Jpanel or JTabbedPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Left-align in Jpanel or JTabbedPane" Watch "Left-align in Jpanel or JTabbedPane" New topic
Author

Left-align in Jpanel or JTabbedPane

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.

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


SCJP 1.4
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
<guessing>
in your TabbedPaneDemo() constructor

super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = createImageIcon("tree.bmp");
JPanel jp = new JPanel(new FlowLayout(FlowLayout.LEFT));//<-----added
jp.add(new QueryBuilder());//<----------added
tabbedPane.addTab("Query Builder", icon,jp,"Build a query.");//<---changed
Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
Wow! It worked. But, why? It still doesn't make sense to me. Why should putting it inside another panel with a left aligned layout make any difference? The content in the QueryBuilder is in a panel and seems to be at least sort of left aligned... just left aligned in the center of the page. But the borders of the panel show it to be right next to the edges. So it seemed like the panel already was left aligned but the contents weren't. Is there some logic I'm missing here or are the border displays just somewhat of a misrepresentation?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'd say your queryBuilder panel is smaller than the other panel/tabpane, so it
is 'centered' in its own tabpane (default for panel is flowlayout.center)

adding another panel (which adopts the size of the tabpane) and setting the
layout to LEFT, ensures its child panel will be on the left.

you could set the JTabbedPane to flowlayout.left, but this will affect the
other tabs, something you may not want.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Left-align in Jpanel or JTabbedPane