Added custom tab component
This commit is contained in:
parent
9b71dbeb82
commit
c6db7da698
38
src/dev/kske/chess/ui/GameTabComponent.java
Normal file
38
src/dev/kske/chess/ui/GameTabComponent.java
Normal 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
|
||||
}
|
||||
}
|
@ -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