Added custom tab component

This commit is contained in:
Kai S. K. Engelbart 2019-12-11 14:02:31 +01:00
parent 9b71dbeb82
commit c6db7da698
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 46 additions and 7 deletions

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} * @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. * 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) { public GamePane addGamePane(String title) {
GamePane gamePane = new GamePane(); GamePane gamePane = new GamePane();
tabbedPane.add(title, 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; return gamePane;
} }