0

I am wondering if it is possible to have a Java Swing control like a check box to the right of the tabs on a JTabbedPane so that space is not wasted by putting it above the tab control and also makes it look more professional.

Any examples out there of this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tony Anecito
  • 347
  • 2
  • 13
  • 1
    Answers to this question will tend to be almost entirely based on opinions regarding _wasted_ and _professional_. See the [`TabComponentsDemo`](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html). – trashgod May 28 '14 at 17:31
  • 1
    Thanks I will try this out. The number of tabbed panes is fixed so no problem with more being added. Looks more professional to me if I have the control placed where I mentioned. Thanks for the recommendation! – Tony Anecito May 29 '14 at 17:02

1 Answers1

2

The only proper way do to this is to create a custom UI for the tabbed pane.

As a hack you can overlay a component on top of the tabbed pane, which of course only works if you have enough space available to the right of the tabs, so it probably doesn't meet your criteria of professional.

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class TabbedPaneWithComponent
{
    private static void createAndShowUI()
    {
        JPanel panel = new JPanel();
        panel.setLayout( new OverlayLayout(panel) );

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("1", new JTextField("one"));
        tabbedPane.add("2", new JTextField("two"));
        tabbedPane.setAlignmentX(1.0f);
        tabbedPane.setAlignmentY(0.0f);

        JCheckBox checkBox = new JCheckBox("Check Me");
        checkBox.setOpaque( false );
        checkBox.setAlignmentX(1.0f);
        checkBox.setAlignmentY(0.0f);

        panel.add( checkBox );
        panel.add(tabbedPane);

        JFrame frame = new JFrame("TabbedPane With Component");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setLocationByPlatform( true );
        frame.setSize(400, 100);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I agree about the UI delegate. Can you comment critically on the approach shown in [`TabComponentsDemo`](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg)? – trashgod May 30 '14 at 00:19
  • @trashgod, I think we are talking about two different examples. The tutorial explains how to add a component to each individual tab. I thought the question was about adding a single component at the right of the tabbed pane itself. This is what my posted code attempts to do. I guess using the tutorial approach you would need to add a separate tab with the check box as a component on that tab. From a UI point of view it would be more reliable since it will always be painted properly, but I would guess you would also selected the tab when the check box is clicked. – camickr Jun 02 '14 at 13:17
  • Ah, I see the distinction now. Thanks! – trashgod Jun 02 '14 at 18:42