Vertical panel positioning
I have a JTabbedPane and a JPanel in my Java program (extending JFrame) and I want to place the JPanel directly under the JTabbedPane (basically have one item per row and for each component added, place it on a new row). However, when I add the JPanel, it goes to the right of the JTabbedPane.
Is there a way to add items vertically instead of horizontally?
Here is my code:
<FILENAME: "Store.java">
import java.awt.*;
import javax.swing.*;
public class Store extends JFrame
{
// define constants used throughout the program
static final String WINDOW_TITLE = "Store Inventory Tracker";
TopMenu topMenu;
ItemPane itemPane;
SearchPane searchPane;
public Store()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
int windowPosX = (screen.width-getWidth())/2;
int windowPosY = (screen.height-getHeight())/2;
this.setLocation(windowPosX, windowPosY);
this.setTitle(WINDOW_TITLE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
topMenu = new TopMenu(this);
setJMenuBar(topMenu);
// get content pane for frame
Container c = getContentPane();
c.setLayout(new FlowLayout());
JTabbedPane tabbedPane = new JTabbedPane();
itemPane = new ItemPane();
searchPane = new SearchPane();
tabbedPane.addTab("Items", itemPane);
tabbedPane.addTab("Search", searchPane);
JPanel clockPane = new JPanel();
JLabel clock = new JLabel("time");
clockPane.add(clock);
add(tabbedPane);
add(clockPane);
pack();
}
public static void main(String [] args)
{
Store store = new Store();
store.setVisible(true);
}
}
<FILENAME: "ItemPane.java">
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class ItemPane extends JPanel
{
public ItemPane()
{
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
// item list
JPanel itemList = new JPanel();
Border itemListBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder itemListBorderWithTitle = BorderFactory.createTitledBorder(itemListBorder, "Items");
itemListBorderWithTitle.setTitleJustification(TitledBorder.LEFT);
itemList.setBorder(itemListBorderWithTitle);
itemList.setLayout(new GridLayout(0, 1));
String items[] = { "Item 1", "Item 2", "Item 3" };
itemList.add(new JComboBox(items));
// item info
JPanel itemInfo = new JPanel();
Border itemInfoBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder itemInfoBorderWithTitle = BorderFactory.createTitledBorder(itemInfoBorder, "Item Information");
itemInfoBorderWithTitle.setTitleJustification(TitledBorder.LEFT);
itemInfo.setBorder(itemInfoBorderWithTitle);
itemInfo.setLayout(new GridLayout(0, 1));
itemInfo.add(new JLabel("Name"));
itemInfo.add(new JTextField(15));
itemInfo.add(new JLabel("Price"));
itemInfo.add(new JTextField(15));
itemInfo.add(new JLabel("Type"));
itemInfo.add(new JTextField(15));
itemInfo.add(new JLabel("Supplier"));
itemInfo.add(new JTextField(15));
add(itemList);
add(itemInfo);
}
}
<FILENAME: "SearchPane.java">
import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;
public class SearchPane extends JPanel
{
public SearchPane()
{
//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setLayout(new FlowLayout(FlowLayout.CENTER));
// item info
JPanel searchBox = new JPanel();
Border searchBoxBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder searchBoxBorderWithTitle = BorderFactory.createTitledBorder(searchBoxBorder, "Search");
searchBoxBorderWithTitle.setTitleJustification(TitledBorder.LEFT);
searchBox.setBorder(searchBoxBorderWithTitle);
searchBox.setLayout(new GridLayout(0, 1));
searchBox.add(new JLabel("Search for:"));
searchBox.add(new JTextField(15));
add(searchBox);
}
}
<FILENAME: "TopMenu.java">
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TopMenu extends JMenuBar implements ActionListener
{
private JMenuItem exit, about;
private Store store;
public TopMenu(Store s)
{
store = s;
// create "File" menu
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
exit = new JMenuItem("Exit");
exit.setMnemonic('x');
exit.addActionListener(this);
fileMenu.add(exit);
add(fileMenu);
// create "Help" menu
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
about = new JMenuItem("About");
about.setMnemonic('A');
about.addActionListener(this);
helpMenu.add(about);
add(helpMenu);
}
public void actionPerformed(ActionEvent e)
{
JMenuItem src = (JMenuItem)e.getSource(); // cast event source to JMenuItem object
if(src == exit)
{
//store.setStatusMsg("Exit selected");
}
else if(src == about)
{
//store.setStatusMsg("About selected");
}
}
}
Thanks!

