Andrei Sh Andrei Sh - 1 year ago 63
Java Question

Is it possible to make this type of jtabbedpane?

I want to create some tabs in my frame, but I don't know is it possible make them as I want. These tabs must be on the left side and below them must be a some jpanel, individually for each tab.

enter image description here

Answer Source

JTabbedPane can’t really do that. I imagine it’s possible to hack the TabbedPaneUI, but I wouldn’t consider that reliable.

Rather, I would just place JToggleButtons in a GridLayout. They won’t look exactly like tabs, but they’ll be just as usable and I doubt users will care either way.

Both the small “info” area below the tabs, and the main content area, should each be a CardLayout. This will ensure that each is large enough to accommodate all possible content.

String title1 = "Tab 1";
String title2 = "Tab 2";

Component infoPanel1 = new FileInfoPanel();
Component infoPanel2 = new OtherInfoPanel();

Component content1 = new WaveFormPanel();
Component content2 = new OtherPanel();

final CardLayout infoLayout = new CardLayout();
final JComponent infoArea = new JPanel(infoLayout);
infoArea.add(infoPanel1, "1");
infoArea.add(infoPanel2, "2");

final CardLayout contentAreaLayout = new CardLayout();
final JComponent contentArea = new JPanel(contentAreaLayout);
contentArea.add(content1, "1");
contentArea.add(content2, "2");

class TabSwitcher
implements ActionListener {
    private String cardID;

    TabSwitcher(String cardID) {
        this.cardID = cardID;

    public void actionPerformed(ActionEvent event) {, cardID);, cardID);

JToggleButton tab1 = new JToggleButton("Tab 1");
tab1.addActionListener(new TabSwitcher("1"));

JToggleButton tab2 = new JToggleButton("Tab 2");
tab2.addActionListener(new TabSwitcher("2"));

ButtonGroup group = new ButtonGroup();

JComponent tabsPanel = new JPanel(new GridLayout(0, 1));
tabsPanel.setBorder(BorderFactory.createEmptyBorder(6, 0, 12, 0));

JComponent tabArea = new JPanel(new BorderLayout());
tabArea.add(tabsPanel, BorderLayout.PAGE_START);
tabArea.add(infoArea, BorderLayout.CENTER);

JComponent analyser = new JPanel(new BorderLayout());
analyser.add(tabArea, BorderLayout.LINE_START);
analyser.add(contentArea, BorderLayout.CENTER);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download