Hallo zusammen,
ich habe folgendes Problem:
Ich habe eine UI für ein JTabbedPane gebaut, um die tabs neu zu gestalten. Hat alles auch super geklappt, bis auf das Zeichnen der Border mit der Funktion
(soll jeweils nur einen schwarzen und einen weißen (Trenn-)Strich rechts an die Tabs malen)
Wenn ich damit das Programm starte, werden die beiden Tabs in dem Pane auch ordentlich damit gezeichnet. Wenn ich dann allerdings das zweite Tab anklicke, wir nur dieses richtig gezeichnet, das erste ist ganz ohne Striche. Gehe ich wieder auf das erste Tab, werden wieder beide richtig gezeichnet. (sind übrigens nur zwei Tabs)
Jemand eine Idee, wie ich es hinkriege, das beide Tabs immer richtig gezeichnet werden?
Gruß
vom forfaro
P.S.: Hier noch der komplette Code der UI:
ich habe folgendes Problem:
Ich habe eine UI für ein JTabbedPane gebaut, um die tabs neu zu gestalten. Hat alles auch super geklappt, bis auf das Zeichnen der Border mit der Funktion
Java:
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected) {
Rectangle rect = rects[tabIndex];
int width = rect.width;
int xpos = rect.x;
if (tabIndex > 0) {
width--;
xpos++;
}
g.setColor(dividerColor);
g.drawLine(rect.x + rect.width, 0, rect.x + rect.width, 20);
g.setColor(Color.BLACK);
g.fillRect(xpos + width - 1, 0, 1, 21);
g.setColor(Color.WHITE);
g.fillRect(xpos + width + 1, 0, 2, 21);
}
(soll jeweils nur einen schwarzen und einen weißen (Trenn-)Strich rechts an die Tabs malen)
Wenn ich damit das Programm starte, werden die beiden Tabs in dem Pane auch ordentlich damit gezeichnet. Wenn ich dann allerdings das zweite Tab anklicke, wir nur dieses richtig gezeichnet, das erste ist ganz ohne Striche. Gehe ich wieder auf das erste Tab, werden wieder beide richtig gezeichnet. (sind übrigens nur zwei Tabs)
Jemand eine Idee, wie ich es hinkriege, das beide Tabs immer richtig gezeichnet werden?
Gruß
vom forfaro
P.S.: Hier noch der komplette Code der UI:
Java:
public class MyTabbedPaneUI extends WindowsTabbedPaneUI {
private static final Insets NO_INSETS = new Insets(0, 0, 0, 0);
private Color selectedColor;
private Color defaultColor;
private Color hoverColor;
private Color bgColor;
private boolean contentTopBorderDrawn = true;
private Color lineColor = new Color(158, 158, 158);
private Color dividerColor = new Color(200, 200, 200);
private Insets contentInsets = new Insets(10, 10, 10, 10);
private int lastRollOverTab = -1;
public static ComponentUI createUI(JComponent c) {
return new MyTabbedPaneUI();
}
public MyTabbedPaneUI() {
selectedColor = new Color(207, 207, 207);
defaultColor = new Color(142, 142, 142);
hoverColor = new Color(172, 172, 172);
bgColor = new Color(255, 255, 255);
maxTabHeight = 20;
setContentInsets(0);
}
public void setContentTopBorderDrawn(boolean b) {
contentTopBorderDrawn = b;
}
public void setContentInsets(Insets i) {
contentInsets = i;
}
public void setContentInsets(int i) {
contentInsets = new Insets(i, i, i, i);
}
public int getTabRunCount(JTabbedPane pane) {
return 1;
}
protected void installDefaults() {
super.installDefaults();
RollOverListener l = new RollOverListener();
tabPane.addMouseListener(l);
tabPane.addMouseMotionListener(l);
tabAreaInsets = NO_INSETS;
tabInsets = new Insets(0, 0, 0, 1);
}
protected boolean scrollableTabLayoutEnabled() {
return false;
}
protected Insets getContentBorderInsets(int tabPlacement) {
return contentInsets;
}
protected int calculateTabHeight(int tabPlacement, int tabIndex,
int fontHeight) {
return 21;
}
protected int calculateTabWidth(int tabPlacement, int tabIndex,
FontMetrics metrics) {
int w = super.calculateTabWidth(tabPlacement, tabIndex, metrics);
int wid = metrics.charWidth('M');
w += wid * 2;
return w;
}
protected int calculateMaxTabHeight(int tabPlacement) {
return 21;
}
protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(bgColor);
g2d.fillRect(0, 0, tabPane.getWidth(), 21);
super.paintTabArea(g, tabPlacement, selectedIndex);
if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(0, 20, tabPane.getWidth() - 1, 20);
}
}
protected void paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2d = (Graphics2D) g;
Color color;
Rectangle rect = rects[tabIndex];
if (isSelected) {
color = selectedColor;
} else if (getRolloverTab() == tabIndex) {
color = hoverColor;
} else {
color = defaultColor;
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int width = rect.width;
int xpos = rect.x;
if (tabIndex > 0) {
width--;
xpos++;
}
g2d.setPaint(color);
// g2d.setPaint(colorSet.topGradColor2);
g2d.fillRect(xpos, 0, width, 21);
// g2d.setPaint(colorSet.bottomGradColor1);
// g2d.fillRect(xpos, 10, width, 11);
// if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(rect.x, 20, rect.x + rect.width - 1, 20);
// }
}
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected) {
// super.paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
// Rectangle rect = getTabBounds(tabIndex, new Rectangle(x, y, w, h));
Rectangle rect = rects[tabIndex];
int width = rect.width;
int xpos = rect.x;
if (tabIndex > 0) {
width--;
xpos++;
}
g.setColor(dividerColor);
g.drawLine(rect.x + rect.width, 0, rect.x + rect.width, 20);
g.setColor(Color.BLACK);
g.fillRect(xpos + width - 1, 0, 1, 21);
g.setColor(Color.WHITE);
g.fillRect(xpos + width + 1, 0, 2, 21);
}
protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
}
protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
super.paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h);
}
protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintFocusIndicator(Graphics g, int tabPlacement,
Rectangle[] rects, int tabIndex, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
// Do nothing
}
@Override
protected void paintText(Graphics g, int tabPlacement, Font font,
FontMetrics metrics, int tabIndex, String title,
Rectangle textRect, boolean isSelected) {
super.paintText(g, tabPlacement, font, metrics, tabIndex, title,
textRect, isSelected);
g.setFont(font);
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
if (isSelected) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
// g.setColor(tabPane.getForegroundAt(tabIndex));
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
} else { // tab disabled
g.setColor(Color.BLACK);
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex,
textRect.x, textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex,
textRect.x - 1, textRect.y + metrics.getAscent() - 1);
}
}
protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
boolean isSelected) {
return 0;
}
private class RollOverListener implements MouseMotionListener,
MouseListener {
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
checkRollOver();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
checkRollOver();
}
public void mouseExited(MouseEvent e) {
tabPane.repaint();
}
private void checkRollOver() {
int currentRollOver = getRolloverTab();
if (currentRollOver != lastRollOverTab) {
lastRollOverTab = currentRollOver;
Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), 20);
tabPane.repaint(tabsRect);
}
}
}
}