Merge pull request #17 from CyB3RC0nN0R/f/close_tabs
Implementing a closing button for game pane tabs
This commit is contained in:
commit
8cdaee349f
99
src/dev/kske/chess/ui/GameTabComponent.java
Normal file
99
src/dev/kske/chess/ui/GameTabComponent.java
Normal file
@ -0,0 +1,99 @@
|
||||
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>
|
||||
* File: <strong>GameTabComponent.java</strong><br>
|
||||
* Created: <strong>11 Dec 2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
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
|
||||
JLabel label = new JLabel() {
|
||||
|
||||
private static final long serialVersionUID = 7902391411509551586L;
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
int i = tabbedPane.indexOfTabComponent(GameTabComponent.this);
|
||||
return i != -1 ? tabbedPane.getTitleAt(i) : "";
|
||||
}
|
||||
};
|
||||
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
|
||||
add(label);
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -91,7 +91,7 @@ public class MainWindow extends JFrame {
|
||||
*
|
||||
* @return The new {@link GamePane}
|
||||
*/
|
||||
public GamePane addGamePane() { return addGamePane("Game " + (tabbedPane.getComponentCount() + 1)); }
|
||||
public GamePane addGamePane() { return addGamePane("Game " + (tabbedPane.getTabCount() + 1)); }
|
||||
|
||||
/**
|
||||
* Creates a new {@link GamePane}, adds it to the tabbed pane and opens it.
|
||||
@ -102,7 +102,8 @@ public class MainWindow extends JFrame {
|
||||
public GamePane addGamePane(String title) {
|
||||
GamePane gamePane = new GamePane();
|
||||
tabbedPane.add(title, gamePane);
|
||||
tabbedPane.setSelectedIndex(tabbedPane.getComponentCount() - 1);
|
||||
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, new GameTabComponent(tabbedPane));
|
||||
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
|
||||
return gamePane;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user