Question

I am creating my own custom JTabbedPane object, with a simple little class that extends JTabbedPane. I have gotten a fair amount of this done, but recently found a problem, that being that the Title text for a tab does not auto align to the center of the Tab. And I want it to. Here is what it looks like:

Just imagine that the tabs aren

As I said, it's just an extension of JTabbedArea, like so:

private static final long serialVersionUID = 1L;
private String name;
private final int width = 150, height = 50;
public ColoredTabs(String paneName,  int tabPlacement, String[] names, Color[] colors, JComponent[] components){
    super(tabPlacement);
    this.name = paneName;
    if(names.length != components.length || names.length != colors.length || components.length != colors.length){
        throw new IllegalArgumentException("The arguments for COMPONENTS, COLORS, and NAMES do not match up for '"+this.name+"'...");
    }
    setFont(Resources.getFont());
    setBackground(Color.YELLOW);
    for(int i = 0; i < names.length; i++){
        addTab(names[i], components[i]);
        setBackgroundAt(i, colors[i]);
        setIconAt(i, new ImageIcon(new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)));

    }
}

The reason I go through the trouble of making the BufferedImage is to stretch the area of the Tab, for the ICON, maybe there is a way to center the text on the BufferedImage, any ideas?

Était-ce utile?

La solution

For a custom tabbedPane you should be able to create Labels with a set alignment, then add them to the jTabbedPane, something like this:

//Create new label to be used as a tab name
JLabel tabLabel = new JLabel("Tab", JLabel.CENTER);
//add new label at set location
jTabbedPane.setTabComponentAt(0, tabLabel);

For more help please show us how you are creating the tabs.

Edit: This may be relevant: Aligning icon to the left in JTabbedPane in Nimbus Look and Feel

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top