Improved close button UI
This commit is contained in:
parent
baf55a4c96
commit
9a4d9121de
@ -1,11 +1,20 @@
|
||||
package dev.kske.chess.ui;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.plaf.basic.BasicButtonUI;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
@ -16,14 +25,17 @@ import javax.swing.JTabbedPane;
|
||||
*/
|
||||
public class GameTabComponent extends JPanel {
|
||||
|
||||
private final JTabbedPane tabbedPane;
|
||||
|
||||
private static final long serialVersionUID = 9022979950018125935L;
|
||||
|
||||
public GameTabComponent(JTabbedPane tabbedPane) {
|
||||
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
||||
if (tabbedPane == null) throw new NullPointerException("TabbedPane is null");
|
||||
this.tabbedPane = tabbedPane;
|
||||
|
||||
// Create title JLabel
|
||||
add(new JLabel() {
|
||||
JLabel label = new JLabel() {
|
||||
|
||||
private static final long serialVersionUID = 7902391411509551586L;
|
||||
|
||||
@ -32,13 +44,56 @@ public class GameTabComponent extends JPanel {
|
||||
int i = tabbedPane.indexOfTabComponent(GameTabComponent.this);
|
||||
return i != -1 ? tabbedPane.getTitleAt(i) : "";
|
||||
}
|
||||
});
|
||||
};
|
||||
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
|
||||
add(label);
|
||||
|
||||
JButton btnClose = new JButton("Close");
|
||||
btnClose.addActionListener((evt) -> {
|
||||
int i = tabbedPane.indexOfTabComponent(GameTabComponent.this);
|
||||
if (i != -1) tabbedPane.remove(i);
|
||||
});
|
||||
add(btnClose);
|
||||
// Create close JButton
|
||||
add(new TabButton());
|
||||
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
|
||||
}
|
||||
|
||||
private class TabButton extends JButton {
|
||||
|
||||
private static final long serialVersionUID = -2757857832337636731L;
|
||||
|
||||
public TabButton() {
|
||||
final int size = 17;
|
||||
setPreferredSize(new Dimension(size, size));
|
||||
setToolTipText("Close this tab");
|
||||
setUI(new BasicButtonUI());
|
||||
setContentAreaFilled(false);
|
||||
setFocusable(false);
|
||||
setBorder(BorderFactory.createEtchedBorder());
|
||||
setBorderPainted(false);
|
||||
addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent evt) { setBorderPainted(true); }
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent evt) { setBorderPainted(false); }
|
||||
});
|
||||
setRolloverEnabled(true);
|
||||
addActionListener((evt) -> { int i = tabbedPane.indexOfTabComponent(GameTabComponent.this); if (i != -1) tabbedPane.remove(i); });
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUI() {}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
// shift the image for pressed buttons
|
||||
if (getModel().isPressed()) { g2.translate(1, 1); }
|
||||
g2.setStroke(new BasicStroke(2));
|
||||
g2.setColor(Color.BLACK);
|
||||
if (getModel().isRollover()) { g2.setColor(Color.MAGENTA); }
|
||||
final int delta = 6;
|
||||
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
|
||||
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
|
||||
g2.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user