Skip to content

Commit fb4a15f

Browse files
committed
Better looking tab close button.
1 parent 80a3e8b commit fb4a15f

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/TabExitButton.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
/**
3333
* @author Konloch
3434
* @since 6/25/2021
35+
* Using CloseButton of darklaf instead. 4/17/2022
3536
*/
37+
@Deprecated
3638
public class TabExitButton extends JButton implements ActionListener
3739
{
3840
private final TabbedPane tabbedPane;
3941
private final int tabIndex;
4042
private final String tabWorkingName;
41-
43+
4244
public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
4345
{
4446
this.tabbedPane = tabbedPane;
@@ -62,12 +64,12 @@ public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
6264
// Close the proper tab by clicking the button
6365
addActionListener(this);
6466
}
65-
67+
6668
public int getTabIndex()
6769
{
6870
return tabIndex;
6971
}
70-
72+
7173
@Override
7274
public void actionPerformed(final ActionEvent e)
7375
{
@@ -77,11 +79,11 @@ public void actionPerformed(final ActionEvent e)
7779
tabbedPane.tabs.remove(i);
7880
}
7981
}
80-
82+
8183
// we don't want to update UI for this button
8284
@Override
8385
public void updateUI() { }
84-
86+
8587
// paint the cross
8688
@Override
8789
protected void paintComponent(final Graphics g)
@@ -91,33 +93,33 @@ protected void paintComponent(final Graphics g)
9193
// shift the image for pressed buttons
9294
if (getModel().isPressed())
9395
g2.translate(1, 1);
94-
96+
9597
g2.setStroke(new BasicStroke(2));
9698
g2.setColor(Color.BLACK);
97-
99+
98100
if (getModel().isRollover())
99101
g2.setColor(Color.MAGENTA);
100-
102+
101103
final int delta = 6;
102104
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
103105
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
104106
g2.dispose();
105107
}
106-
108+
107109
public TabbedPane getTabbedPane()
108110
{
109111
return tabbedPane;
110112
}
111-
113+
112114
public String getTabWorkingName()
113115
{
114116
return tabWorkingName;
115117
}
116-
118+
117119
public static long getSerialVersionUID()
118120
{
119121
return serialVersionUID;
120122
}
121-
123+
122124
private static final long serialVersionUID = -4492967978286454159L;
123125
}

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/TabbedPane.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.awt.Component;
55
import java.awt.FlowLayout;
66
import java.awt.Rectangle;
7-
import java.awt.event.InputEvent;
87
import java.awt.event.MouseAdapter;
98
import java.awt.event.MouseEvent;
109
import java.awt.event.MouseListener;
@@ -16,6 +15,8 @@
1615
import javax.swing.JPopupMenu;
1716
import javax.swing.JTabbedPane;
1817
import javax.swing.SwingUtilities;
18+
19+
import com.github.weisj.darklaf.components.CloseButton;
1920
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
2021
import the.bytecode.club.bytecodeviewer.gui.components.ButtonHoverAnimation;
2122
import the.bytecode.club.bytecodeviewer.gui.components.MaxWidthJLabel;
@@ -84,7 +85,7 @@ public TabbedPane(int tabIndex, String tabWorkingName, String fileContainerName,
8485
// add more space between the label and the button
8586
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
8687
// tab button
87-
JButton exitButton = new TabExitButton(this, tabIndex, tabWorkingName);
88+
JButton exitButton = new CloseButton();
8889
this.add(exitButton);
8990
// add more space to the top of the component
9091
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
@@ -101,34 +102,24 @@ public TabbedPane(int tabIndex, String tabWorkingName, String fileContainerName,
101102
exitButton.setComponentPopupMenu(rightClickMenu);
102103
exitButton.addMouseListener(new MouseClickedListener(e ->
103104
{
104-
if (e.getModifiersEx() != InputEvent.ALT_DOWN_MASK || System.currentTimeMillis() - lastMouseClick < 100)
105-
return;
106-
107-
lastMouseClick = System.currentTimeMillis();
108-
final int i = existingTabs.indexOfTabComponent(TabbedPane.this);
109-
if (i != -1)
110-
existingTabs.remove(i);
105+
if (this.getTabIndex() != -1)
106+
existingTabs.remove(this.getTabIndex());
111107
}));
112108

113109
closeTab.addActionListener(e ->
114110
{
115-
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
116-
final int index = tabExitButton.getTabIndex();
117-
118-
if (index != -1)
119-
existingTabs.remove(index);
111+
if (this.getTabIndex() != -1)
112+
existingTabs.remove(this.getTabIndex());
120113
});
121114
closeAllTabs.addActionListener(e ->
122115
{
123-
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
124-
final int index = tabExitButton.getTabIndex();
125-
116+
126117
while (true)
127118
{
128119
if (existingTabs.getTabCount() <= 1)
129120
return;
130121

131-
if (index != 0)
122+
if (this.getTabIndex() != 0)
132123
existingTabs.remove(0);
133124
else
134125
existingTabs.remove(1);
@@ -257,5 +248,8 @@ public void onMousePressed(MouseEvent e)
257248
}
258249

259250
private static final long serialVersionUID = -4774885688297538774L;
260-
251+
252+
public int getTabIndex() {
253+
return tabs.indexOfTabComponent(this);
254+
}
261255
}

0 commit comments

Comments
 (0)