Implementing a closing button for game pane tabs #17

Merged
CyB3RC0nN0R merged 3 commits from f/close_tabs into master 2019-12-11 21:26:39 +01:00
2 changed files with 46 additions and 7 deletions
Showing only changes of commit 17d2080e27 - Show all commits

View File

@ -0,0 +1,38 @@
package dev.kske.chess.ui;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/**
* 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 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");
// Create title JLabel
add(new JLabel() {
private static final long serialVersionUID = 7902391411509551586L;
@Override
public String getText() {
int i = tabbedPane.indexOfTabComponent(GameTabComponent.this);
return i != -1 ? tabbedPane.getTitleAt(i) : "";
}
});
// TODO: Add closing button
}
}

View File

@ -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;
}