Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the blue border highlight that appears when selecting a tab in a JTabbedPane?

This is sort of a continuation of my previous question, but it addresses a specific concern that could be useful to someone else so I thought I would post it as a individual question.

I have successfully created a JTabbedPane but there is an blue border highlight that shows which tab has been selected that I would like to remove:

Blue border highlight

To clarify what I mean here is a picture of a JTabbedPane without the blue border highlight from Eclipse:

Eclipse

The things I have tried have been commented out:

public class SeaGlassExercise {

    public static void initWindow() {
        JFrame frame = new JFrame("Application Name");
        CustomTabbedPane content = new CustomTabbedPane();
        frame.setContentPane(content);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);
//                try {
//                    UIManager.setLookAndFeel(
//        UIManager.getSystemLookAndFeelClassName());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                SwingUtilities.updateComponentTreeUI(frame);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initWindow();
            }
        });
    }

}

class CustomTabbedPane extends JPanel {

    public CustomTabbedPane() {
        super(new GridLayout(1, 1));

//        UIManager.put("TabbedPane.contentAreaColor", Color.GREEN);
//        UIManager.put("TabbedPane.light", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.highlight", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.shadow", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.darkShadow", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.selected", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.borderHightlightColor", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.borderHightlightColor", ColorUIResource.GREEN);
//        UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

        JTabbedPane tabbedPane = new JTabbedPane();
        JComponent panel1 = makeTextPanel("Panel #1");
        tabbedPane.addTab("AAA", panel1);
        JComponent panel2 = makeTextPanel("Panel #2");
        tabbedPane.addTab("BBB", panel2);
        JComponent panel3 = makeTextPanel("Panel #3");
        tabbedPane.addTab("CCC", panel3);
        JComponent panel4 = makeTextPanel("Panel #4");
        tabbedPane.addTab("DDD", panel4);
        add(tabbedPane);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel();
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
}

Additional Information

I am currently running my program with OS X Mountain Lion along with java version "1.7.0_25", Java(TM) SE Runtime Environment (build 1.7.0_25-b15). I am using the default look and feel (i.e. I have not specified anything with .setUI() in my code).

Here are some questions I have looked at:

  1. Controlling Color in Java Tabbed Pane
  2. JTabbedPane - set default border around tabs..?
  3. remove blue color from JTabbedPane
like image 595
Kent Shikama Avatar asked Nov 01 '22 06:11

Kent Shikama


2 Answers

Note the current approach may not work with other platforms / look and feels. If your current project is intended to work only on your Mac and you don't plan change this in the future, well in that case it might work. But normally Java applications are intended to work across different platforms and look and feels.

Having said this you may want to take a look to this interesting article about most popular look and feels defaults: All UI defaults names for common Java look and feels on Windows, Mac OS X, and Linux. When you look at the tables then you'll see not all L&Fs support the same properties and may ignore them when create an UI object (f.i. TabbedPaneUI).

If you like Mac L&F (I do) then I'd suggest you try customizing Seaglass Look and Feel which is pretty similar than Mac's. This way you will get this benefits:

  • Standard cross-platform L&F similar to Mac's L&F provided with your app.
  • Customize the L&F and this change will be also cross-platform.
  • Unify user experience (non trivial matter). Probably you and me will be able to work with the same app on Mac OS, Windows or Linux with different L&F. But many users can't do it: they get lost when the GUI looks different.

To customize Seaglass you can list the default properties as follow:

for(Object key : UIManager.getLookAndFeel().getDefaults().keySet()) {
    System.out.println(key + " = " + UIManager.get(key));
}

These are quite much and I really don't have time enough to give you a working example so I hope the idea is good enough to help you.

Note

If you don't want frames and dialogs have the default decoration provided with Seaglass (it's pretty ugly to me) then you need to do as follw:

UIManager.setLookAndFeel(new SeaGlassLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(false);
JDialog.setDefaultLookAndFeelDecorated(false);

This way frames and dialogs will have their Window decorations provided by the current window manager (up to the OS).

like image 71
dic19 Avatar answered Nov 14 '22 03:11

dic19


I know the correct visual solution to remove tabs' border:

tabbedPane.setFocusable(false);

But you lost posibility to use keys for tabbed pane.

like image 33
user and Avatar answered Nov 14 '22 02:11

user and