4

I was wondering how you would get the color of the inset of a JTabbedPane. I cannot seem to get this color. Every attempt I make I get 236,236,236 which is the outside frame color, where the inside frame color is about 10 darker, 227,227,227 (using the built in apple color meter).

I am setting the look and feel using UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

You can see this in an image that I found on the internet. http://pagesofinterest.net/wordpress/wp-content/uploads/2009/06/Quaqua-Maven-Netbeans.jpg Where the words "Panel's Title" is the area that I am getting the lighter color that is not useful to me. Inside the round corners is the darker color I am trying to obtain. I tried getting the color of the content pane to no avail.

Thanks for all your help!

**EDIT:**Added code! As you see, I am trying to get the color of the area inside the rounded corners(if your on a mac) not the color of the frame or the tabs that say "1" "2". I have attached a photo and I am trying to get the background color of the portion that says "Here" Thanks!

Screenshot

import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

public class main {
JFrame frame;
Container c1 = new Container();
Container c2 = new Container();
JTabbedPane top = new JTabbedPane();
static main GUI;
public void createGUI(){
    frame = new JFrame();
    Container c = frame.getContentPane();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    top = new JTabbedPane(JTabbedPane.TOP);
    top.setFocusTraversalKeysEnabled(false);
    top.setFocusable(false);
    top.addTab("1", c1);
    top.addTab("2", c2);
    frame.setSize(315,450);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(true);
    c.add(top);
    frame.pack();
    frame.setVisible(true);
} 
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {}
    GUI = new main();
    GUI.createGUI();
}
}

EDIT: camickr, Here is a screenshot of the UIManager Defaults. Unfortunately none of there colors in the are the correct color that the inset is.

UI Manager Defaults

dannyn382
  • 363
  • 2
  • 5
  • 15
  • 1
    Can you please put your code here, that would be great and it will easy for us to give you advice :) – akki0996 May 18 '13 at 09:19
  • The color is a semi gradient, ranging from about RGB/195, 195, 195 to about RGB/208, 208, 208 – MadProgrammer May 18 '13 at 09:57
  • Thank you for your suggestion. On my mac, when I run the program, there is no gradient at all when I am inside the round corners in the box with "Here" in it from the screen shot(using mac digital color meter). The issue is I cannot statically define this color, because on Windows, this color is entirely different. I need to find a way to get this color pragmatically so I can use it in my program later. Thanks again! – dannyn382 May 18 '13 at 10:02

2 Answers2

2

You might be able to use UIMangaer Defaults to find the color.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You can override paintComponent() to use a GradientPaint in the tab's background, as shown below. A few notes,

  • Let the content adopt the preferred size of it contents, as shown here.

  • Construct the GUI in the event dispatch thread.

  • Use conventional Java names.

Addendum: the elements are not always in the same spot, so I do not know what place to get the color.

It sounds like you want to match a color used internally by the TabbedPaneUI delegate. One approach would be as follows:

  • Render a BufferedImage of the component, as shown here.

  • Determine the coordinates of a Point in top relative to the top of c1.

    Point p = SwingUtilities.convertPoint(c1, 0, -1, top);
    
  • Obtain the color using getRGB(), as shown here; use Zoom to verify the result.

Main image

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/** @see https://stackoverflow.com/a/16625260/230513 */
public class Main {

    JFrame frame;
    Container c1 = new GradientPanel();
    Container c2 = new GradientPanel();
    JTabbedPane top = new JTabbedPane();

    private static class GradientPanel extends JPanel {

        public GradientPanel() {
            this.add(new JLabel("Here"));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            GradientPaint p = new GradientPaint(0, 0, Color.white,
                getWidth(), getHeight(), Color.gray);
            g2d.setPaint(p);
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    public void createGUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        top = new JTabbedPane(JTabbedPane.TOP);
        top.addTab("1", c1);
        top.addTab("2", c2);
        frame.add(top);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().createGUI();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hello Trashgod, Thank you for your response, but I do not want to change the color at all, or put in any gradients. I just want to get the color. – dannyn382 May 18 '13 at 18:57
  • Ah, I see you want to match the color, not change it. I don't think `com.apple.laf.AquaLookAndFeel` exposes that color as a `UIManager` property. You could use `Robot`, shown [here](http://stackoverflow.com/a/3742841/230513). – trashgod May 18 '13 at 22:45
  • Thanks for your reply! I am unsure how to implement what you reference in Robot. Could you help me use that to get the color? – dannyn382 May 19 '13 at 00:28
  • Run the program and mouse over the area of interest to see the color; mine says `0xFFDFDFDF`. – trashgod May 19 '13 at 03:16
  • I know what the color code is, the issue is I have to get this on every computer every time that the program starts. So every time I start the program I need to have the color of the background of the container. On Mac this is a greyish color. On Windows it is almost white. The issue is not getting it once, the issue is getting it every time the program starts. I wanted to do something like jtabpane.getBackground(), but this does not get the color of the container, just the color of the tab. I need to find a way to get the color of the container. Thanks again, hopefully this is more clear now – dannyn382 May 19 '13 at 04:24
  • You can render into a `bufferedImage`, as shown [here](http://stackoverflow.com/a/7028497/230513), and use `getRGB()`. – trashgod May 19 '13 at 04:49
  • Please excuse my ignorance, but can you help me do that. I am struggling with how to approach that. There is no way to get the container color? The only way would be to take a screen shot of the container then get that color? Thanks! – dannyn382 May 19 '13 at 06:43
  • I don't know where you're stuck. Please edit your question with an update of your [sscce](http://sscce.org/) that shows your current approach based on the examples cited. – trashgod May 19 '13 at 13:58
  • The issue is I this is just a very small app that I threw together to show what portion of the frame I needed the color of. The issue is when the frame opens, all the elements are not always in the same spot, so I do not know what place to get the color of. – dannyn382 May 20 '13 at 07:06
  • @dannyn382: I outlined an approach above. – trashgod May 20 '13 at 09:05