From c6db7da6984d3d97aa3ae46ee1e783283a2670c9 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 11 Dec 2019 14:02:31 +0100 Subject: [PATCH] Added custom tab component --- src/dev/kske/chess/ui/GameTabComponent.java | 38 +++++++++++++++++++++ src/dev/kske/chess/ui/MainWindow.java | 15 ++++---- 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/dev/kske/chess/ui/GameTabComponent.java diff --git a/src/dev/kske/chess/ui/GameTabComponent.java b/src/dev/kske/chess/ui/GameTabComponent.java new file mode 100644 index 0000000..e436348 --- /dev/null +++ b/src/dev/kske/chess/ui/GameTabComponent.java @@ -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: Chess
+ * File: GameTabComponent.java
+ * Created: 11 Dec 2019
+ * + * @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 + } +} diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index 3dc54b5..22ccbc3 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -26,7 +26,7 @@ import dev.kske.chess.pgn.PGNGame; * Project: Chess
* File: MainWindow.java
* Created: 01.07.2019
- * + * * @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 files) {