Better looking tab close button.
This commit is contained in:
parent
80a3e8b7f0
commit
fb4a15f0c3
2 changed files with 27 additions and 31 deletions
|
@ -32,13 +32,15 @@ import javax.swing.plaf.basic.BasicButtonUI;
|
||||||
/**
|
/**
|
||||||
* @author Konloch
|
* @author Konloch
|
||||||
* @since 6/25/2021
|
* @since 6/25/2021
|
||||||
|
* Using CloseButton of darklaf instead. 4/17/2022
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public class TabExitButton extends JButton implements ActionListener
|
public class TabExitButton extends JButton implements ActionListener
|
||||||
{
|
{
|
||||||
private final TabbedPane tabbedPane;
|
private final TabbedPane tabbedPane;
|
||||||
private final int tabIndex;
|
private final int tabIndex;
|
||||||
private final String tabWorkingName;
|
private final String tabWorkingName;
|
||||||
|
|
||||||
public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
|
public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
|
||||||
{
|
{
|
||||||
this.tabbedPane = tabbedPane;
|
this.tabbedPane = tabbedPane;
|
||||||
|
@ -62,12 +64,12 @@ public class TabExitButton extends JButton implements ActionListener
|
||||||
// Close the proper tab by clicking the button
|
// Close the proper tab by clicking the button
|
||||||
addActionListener(this);
|
addActionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTabIndex()
|
public int getTabIndex()
|
||||||
{
|
{
|
||||||
return tabIndex;
|
return tabIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(final ActionEvent e)
|
public void actionPerformed(final ActionEvent e)
|
||||||
{
|
{
|
||||||
|
@ -77,11 +79,11 @@ public class TabExitButton extends JButton implements ActionListener
|
||||||
tabbedPane.tabs.remove(i);
|
tabbedPane.tabs.remove(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we don't want to update UI for this button
|
// we don't want to update UI for this button
|
||||||
@Override
|
@Override
|
||||||
public void updateUI() { }
|
public void updateUI() { }
|
||||||
|
|
||||||
// paint the cross
|
// paint the cross
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(final Graphics g)
|
protected void paintComponent(final Graphics g)
|
||||||
|
@ -91,33 +93,33 @@ public class TabExitButton extends JButton implements ActionListener
|
||||||
// shift the image for pressed buttons
|
// shift the image for pressed buttons
|
||||||
if (getModel().isPressed())
|
if (getModel().isPressed())
|
||||||
g2.translate(1, 1);
|
g2.translate(1, 1);
|
||||||
|
|
||||||
g2.setStroke(new BasicStroke(2));
|
g2.setStroke(new BasicStroke(2));
|
||||||
g2.setColor(Color.BLACK);
|
g2.setColor(Color.BLACK);
|
||||||
|
|
||||||
if (getModel().isRollover())
|
if (getModel().isRollover())
|
||||||
g2.setColor(Color.MAGENTA);
|
g2.setColor(Color.MAGENTA);
|
||||||
|
|
||||||
final int delta = 6;
|
final int delta = 6;
|
||||||
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
|
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
|
||||||
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
|
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
|
||||||
g2.dispose();
|
g2.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TabbedPane getTabbedPane()
|
public TabbedPane getTabbedPane()
|
||||||
{
|
{
|
||||||
return tabbedPane;
|
return tabbedPane;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTabWorkingName()
|
public String getTabWorkingName()
|
||||||
{
|
{
|
||||||
return tabWorkingName;
|
return tabWorkingName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long getSerialVersionUID()
|
public static long getSerialVersionUID()
|
||||||
{
|
{
|
||||||
return serialVersionUID;
|
return serialVersionUID;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = -4492967978286454159L;
|
private static final long serialVersionUID = -4492967978286454159L;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.awt.Color;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.event.InputEvent;
|
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
|
@ -16,6 +15,8 @@ import javax.swing.JPanel;
|
||||||
import javax.swing.JPopupMenu;
|
import javax.swing.JPopupMenu;
|
||||||
import javax.swing.JTabbedPane;
|
import javax.swing.JTabbedPane;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
import com.github.weisj.darklaf.components.CloseButton;
|
||||||
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
||||||
import the.bytecode.club.bytecodeviewer.gui.components.ButtonHoverAnimation;
|
import the.bytecode.club.bytecodeviewer.gui.components.ButtonHoverAnimation;
|
||||||
import the.bytecode.club.bytecodeviewer.gui.components.MaxWidthJLabel;
|
import the.bytecode.club.bytecodeviewer.gui.components.MaxWidthJLabel;
|
||||||
|
@ -84,7 +85,7 @@ public class TabbedPane extends JPanel
|
||||||
// add more space between the label and the button
|
// add more space between the label and the button
|
||||||
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
|
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
|
||||||
// tab button
|
// tab button
|
||||||
JButton exitButton = new TabExitButton(this, tabIndex, tabWorkingName);
|
JButton exitButton = new CloseButton();
|
||||||
this.add(exitButton);
|
this.add(exitButton);
|
||||||
// add more space to the top of the component
|
// add more space to the top of the component
|
||||||
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
|
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
|
||||||
|
@ -101,34 +102,24 @@ public class TabbedPane extends JPanel
|
||||||
exitButton.setComponentPopupMenu(rightClickMenu);
|
exitButton.setComponentPopupMenu(rightClickMenu);
|
||||||
exitButton.addMouseListener(new MouseClickedListener(e ->
|
exitButton.addMouseListener(new MouseClickedListener(e ->
|
||||||
{
|
{
|
||||||
if (e.getModifiersEx() != InputEvent.ALT_DOWN_MASK || System.currentTimeMillis() - lastMouseClick < 100)
|
if (this.getTabIndex() != -1)
|
||||||
return;
|
existingTabs.remove(this.getTabIndex());
|
||||||
|
|
||||||
lastMouseClick = System.currentTimeMillis();
|
|
||||||
final int i = existingTabs.indexOfTabComponent(TabbedPane.this);
|
|
||||||
if (i != -1)
|
|
||||||
existingTabs.remove(i);
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
closeTab.addActionListener(e ->
|
closeTab.addActionListener(e ->
|
||||||
{
|
{
|
||||||
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
|
if (this.getTabIndex() != -1)
|
||||||
final int index = tabExitButton.getTabIndex();
|
existingTabs.remove(this.getTabIndex());
|
||||||
|
|
||||||
if (index != -1)
|
|
||||||
existingTabs.remove(index);
|
|
||||||
});
|
});
|
||||||
closeAllTabs.addActionListener(e ->
|
closeAllTabs.addActionListener(e ->
|
||||||
{
|
{
|
||||||
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
|
|
||||||
final int index = tabExitButton.getTabIndex();
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (existingTabs.getTabCount() <= 1)
|
if (existingTabs.getTabCount() <= 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (index != 0)
|
if (this.getTabIndex() != 0)
|
||||||
existingTabs.remove(0);
|
existingTabs.remove(0);
|
||||||
else
|
else
|
||||||
existingTabs.remove(1);
|
existingTabs.remove(1);
|
||||||
|
@ -257,5 +248,8 @@ public class TabbedPane extends JPanel
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long serialVersionUID = -4774885688297538774L;
|
private static final long serialVersionUID = -4774885688297538774L;
|
||||||
|
|
||||||
|
public int getTabIndex() {
|
||||||
|
return tabs.indexOfTabComponent(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue