Reputation: 127
I have three tabs total and when the user clicks on the different tabs I need there to be buttons that the user can click. I also want to know how to make my window bigger. Thanks in advance for your answers.
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
public class Tabs extends JPanel {
public Tabs() {
super(new GridLayout(10, 10));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Initialize", null, null,"Does Nothing");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
tabbedPane.addTab("LLP", null, null, "Does Nothing");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_2);
tabbedPane.addTab("POS", null, null, "Does Nothing");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_3);
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Tabbed Pane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Tabs(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
Upvotes: 2
Views: 2900
Reputation: 1278
Add Components to JTabbedPane
It is very simple. You want to show buttons on the JTabbedPane containing panels, so you need to create buttons and panels first, add buttons to panels and add panels to JTabbedPane.
Example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TabbedPaneExample {
private JFrame frame = new JFrame();
public TabbedPaneExample() {
JButton btn = new JButton("Test");
JPanel panel = new JPanel();
panel.add(btn);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Tab1", panel);
frame.add(tabbedPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);
}
public static void main(String[] args) {
TabbedPaneExample main = new TabbedPaneExample();
}
}
JButton event handling
Also you asked how to handle button click event. It is well explained here: A JButton listener example
Upvotes: 1
Reputation: 205785
Here's a concrete example based on your sscce.
how to make my window bigger?
When you pack()
the enclosing Window
, as you;re doing, JTabbedPane
adopts the preferred size of it's largest component. Just add the desired components to each added panel.
public Tabs() {
super(new GridLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(createPanel("Initialize"));
tabbedPane.add(createPanel("LLP"));
tabbedPane.add(createPanel("POS"));
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private JPanel createPanel(final String name) {
final JPanel p = new JPanel();
p.setName(name);
p.add(new JButton(new AbstractAction(name) {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(p, name);
}
}));
return p;
}
Upvotes: 1
Reputation: 324118
I want a button that the user can click that will cause another window to come up and the user inputs data.
Like others I am confused with your question, but I will make a wild guess:
tabbedPane.addTab("LLP", null, null, "Does Nothing");
You are not adding any components to the tab. You need to create a JPanel and add your buttons to the panel. Then you add the panel to the tab when you use the addTab(...) method.
It is just like adding a panel to a JFrame. You can add any component you want to the panel.
Upvotes: 1
Reputation: 3794
To add buttons follow this How to add close button to a JTabbedPane Tab? .
very good tutorial http://paperjammed.com/2012/11/22/adding-tab-close-buttons-to-a-jtabbedpane-in-java-swing/
You can add other button similar way to your JPanel.
Tutorial from Oracle.
Upvotes: 0
Reputation: 1146
what you want seems to be a custom tab component, take a look at this http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
and also this example http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java
Upvotes: 1