Skip to content

Commit

Permalink
Better looking tab close button.
Browse files Browse the repository at this point in the history
  • Loading branch information
GraxCode committed Apr 17, 2022
1 parent 80a3e8b commit fb4a15f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Expand Up @@ -32,13 +32,15 @@
/**
* @author Konloch
* @since 6/25/2021
* Using CloseButton of darklaf instead. 4/17/2022
*/
@Deprecated
public class TabExitButton extends JButton implements ActionListener
{
private final TabbedPane tabbedPane;
private final int tabIndex;
private final String tabWorkingName;

public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
{
this.tabbedPane = tabbedPane;
Expand All @@ -62,12 +64,12 @@ public TabExitButton(TabbedPane tabbedPane, int tabIndex, String tabWorkingName)
// Close the proper tab by clicking the button
addActionListener(this);
}

public int getTabIndex()
{
return tabIndex;
}

@Override
public void actionPerformed(final ActionEvent e)
{
Expand All @@ -77,11 +79,11 @@ public void actionPerformed(final ActionEvent e)
tabbedPane.tabs.remove(i);
}
}

// we don't want to update UI for this button
@Override
public void updateUI() { }

// paint the cross
@Override
protected void paintComponent(final Graphics g)
Expand All @@ -91,33 +93,33 @@ protected void paintComponent(final Graphics g)
// shift the image for pressed buttons
if (getModel().isPressed())
g2.translate(1, 1);

g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLACK);

if (getModel().isRollover())
g2.setColor(Color.MAGENTA);

final int delta = 6;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
g2.dispose();
}

public TabbedPane getTabbedPane()
{
return tabbedPane;
}

public String getTabWorkingName()
{
return tabWorkingName;
}

public static long getSerialVersionUID()
{
return serialVersionUID;
}

private static final long serialVersionUID = -4492967978286454159L;
}
Expand Up @@ -4,7 +4,6 @@
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
Expand All @@ -16,6 +15,8 @@
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import com.github.weisj.darklaf.components.CloseButton;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.gui.components.ButtonHoverAnimation;
import the.bytecode.club.bytecodeviewer.gui.components.MaxWidthJLabel;
Expand Down Expand Up @@ -84,7 +85,7 @@ public TabbedPane(int tabIndex, String tabWorkingName, String fileContainerName,
// add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
// tab button
JButton exitButton = new TabExitButton(this, tabIndex, tabWorkingName);
JButton exitButton = new CloseButton();
this.add(exitButton);
// add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
Expand All @@ -101,34 +102,24 @@ public TabbedPane(int tabIndex, String tabWorkingName, String fileContainerName,
exitButton.setComponentPopupMenu(rightClickMenu);
exitButton.addMouseListener(new MouseClickedListener(e ->
{
if (e.getModifiersEx() != InputEvent.ALT_DOWN_MASK || System.currentTimeMillis() - lastMouseClick < 100)
return;

lastMouseClick = System.currentTimeMillis();
final int i = existingTabs.indexOfTabComponent(TabbedPane.this);
if (i != -1)
existingTabs.remove(i);
if (this.getTabIndex() != -1)
existingTabs.remove(this.getTabIndex());
}));

closeTab.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();

if (index != -1)
existingTabs.remove(index);
if (this.getTabIndex() != -1)
existingTabs.remove(this.getTabIndex());
});
closeAllTabs.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();


while (true)
{
if (existingTabs.getTabCount() <= 1)
return;

if (index != 0)
if (this.getTabIndex() != 0)
existingTabs.remove(0);
else
existingTabs.remove(1);
Expand Down Expand Up @@ -257,5 +248,8 @@ public void onMousePressed(MouseEvent e)
}

private static final long serialVersionUID = -4774885688297538774L;


public int getTabIndex() {
return tabs.indexOfTabComponent(this);
}
}

0 comments on commit fb4a15f

Please sign in to comment.