1

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();
      }
  });


 }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kendra Cheatham
  • 117
  • 5
  • 16
  • 1
    ok when the program starts i want there to be 3 tabs (which is what I have now) and when the user clicks on the different tabs I want there to be buttons in the window (i guess it would be the JPanel) and when the user clicks the buttons i want another window to pop up and allow the user to input data. Does that make sense? – Kendra Cheatham May 20 '13 at 15:04
  • +1 for [sscce](http://sscce.org/). – trashgod May 20 '13 at 16:15

5 Answers5

1

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

fGo
  • 1,146
  • 5
  • 11
  • in the second example you have an action listener, you then handle it propery to show the window you want – fGo May 20 '13 at 14:20
1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • ok when the program starts i want there to be 3 tabs (which is what I have now) and when the user clicks on the different tabs I want there to be buttons in the window (i guess it would be the JPanel) and when the user clicks the buttons i want another window to pop up and allow the user to input data. Does that make sense? – Kendra Cheatham May 20 '13 at 15:13
  • 'I want there to be buttons in the window' - I assume you mean buttons on the panel displayed in the tab. If you need a popup window then you should use a modal JDialog. Yes, this is a common way to get user data. – camickr May 20 '13 at 15:19
  • yes that is exactly what i mean. I looked at example here http://www.java2s.com/Code/Java/Swing-JFC/TabbedPaneDemo.htm but where it says panel #1 i need buttons and i dont know how to do that – Kendra Cheatham May 20 '13 at 15:27
  • See [How to Make Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html). – camickr May 20 '13 at 15:35
1

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

rebeliagamer
  • 1,257
  • 17
  • 33
1

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.

image

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;
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

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.

Community
  • 1
  • 1
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • I don't want a close button. I want a button that the user can click that will cause another window to come up and the user inputs data. I am making a program for the card reader that you use when you purchase something at a store – Kendra Cheatham May 20 '13 at 14:11
  • 1
    I understood it very well, this is just example, similar way you can add button in your Jpanel, specify location in and you will have those buttons created on Jtabpane. – Alpesh Gediya May 20 '13 at 14:14
  • So, do you want to switch tabs by using button? – akki0996 May 20 '13 at 14:40
  • So as the @Alpesh suggested, first create buttons in your every tab, then add your actionlistener to every button. – akki0996 May 20 '13 at 14:42
  • i do not want to switch tabs by using a button – Kendra Cheatham May 20 '13 at 15:05