Implementing a closing button for game pane tabs #17
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
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ import dev.kske.chess.pgn.PGNGame;
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>MainWindow.java</strong><br>
|
||||
* Created: <strong>01.07.2019</strong><br>
|
||||
*
|
||||
*
|
||||
* @since Chess v0.1-alpha
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
@ -88,21 +88,22 @@ public class MainWindow extends JFrame {
|
||||
/**
|
||||
* Creates a new {@link GamePane}, adds it to the tabbed pane and opens it.
|
||||
* The new tab has the title {@code Game n} where {@code n} is its number.
|
||||
*
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
*
|
||||
* @param title The title of the {@link GamePane}
|
||||
* @return The new {@link GamePane}
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@ -119,14 +120,14 @@ public class MainWindow extends JFrame {
|
||||
|
||||
/**
|
||||
* Removes a {@link GamePane} form the tabbed pane.
|
||||
*
|
||||
*
|
||||
* @param index The index of the {@link GamePane} to remove
|
||||
*/
|
||||
public void removeGamePane(int index) { tabbedPane.remove(index); }
|
||||
|
||||
/**
|
||||
* Loads a game file (FEN or PGN) and adds it to a new {@link GamePane}.
|
||||
*
|
||||
*
|
||||
* @param files the files to load the game from
|
||||
*/
|
||||
public void loadFiles(List<File> files) {
|
||||
|
Reference in New Issue
Block a user