Skip to content

Instantly share code, notes, and snippets.

@gwenn
Last active March 26, 2025 07:07
Show Gist options
  • Save gwenn/3a4c2c39a635b41658a0f72788b671f9 to your computer and use it in GitHub Desktop.
Save gwenn/3a4c2c39a635b41658a0f72788b671f9 to your computer and use it in GitHub Desktop.
public interface AbstractListDataListener extends ListDataListener {
@Override
default void intervalAdded(ListDataEvent e) {
contentsChanged(e);
}
@Override
default void intervalRemoved(ListDataEvent e) {
contentsChanged(e);
}
}
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
@FunctionalInterface
public interface InternalFrameClosedListener extends InternalFrameListener {
@Override
default void internalFrameOpened(InternalFrameEvent e) {
}
@Override
default void internalFrameClosing(InternalFrameEvent e) {
}
@Override
default void internalFrameIconified(InternalFrameEvent e) {
}
@Override
default void internalFrameDeiconified(InternalFrameEvent e) {
}
@Override
default void internalFrameActivated(InternalFrameEvent e) {
}
@Override
default void internalFrameDeactivated(InternalFrameEvent e) {
}
}
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
@FunctionalInterface
public interface ItemSelectedListener extends ItemListener {
@Override
default void itemStateChanged(ItemEvent e) {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
itemSelected(e);
}
void itemSelected(ItemEvent e);
}
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
@FunctionalInterface
public interface MouseClickedListener extends MouseListener {
@Override
default void mousePressed(MouseEvent e) {
}
@Override
default void mouseReleased(MouseEvent e) {
}
@Override
default void mouseEntered(MouseEvent e) {
}
@Override
default void mouseExited(MouseEvent e) {
}
}
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* {@link javax.swing.JPopupMenu#show(Component, int, int)}
*/
@FunctionalInterface
public interface PopupListener extends MouseListener {
@Override
default void mouseClicked(MouseEvent e) {
}
@Override
default void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
default void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
showPopupMenu(e.getComponent(), e.getX(), e.getY());
}
}
void showPopupMenu(Component invoker, int x, int y);
@Override
default void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
@Override
default void mouseEntered(MouseEvent e) {
}
@Override
default void mouseExited(MouseEvent e) {
}
static void autoSelectRow(JTable table, int x, int y) {
int selRow = table.rowAtPoint(new Point(x, y));
if (selRow != -1 && !table.isRowSelected(selRow)) {
table.setRowSelectionInterval(selRow, selRow);
}
}
static PopupListener add(JTable table, JPopupMenu popup) {
return add(table, popup, false);
}
static PopupListener add(JTable table, JPopupMenu popup, boolean checkSelectionIsNotEmpty) {
PopupListener popupListener = (invoker, x, y) -> {
autoSelectRow(table, x, y);
if (checkSelectionIsNotEmpty && table.getSelectedRow() < 0) {
return;
}
popup.show(invoker, x, y);
};
table.addMouseListener(popupListener);
return popupListener;
}
static void add(JScrollPane scrollPane, JTable table, JPopupMenu popup) {
scrollPane.addMouseListener(add(table, popup));
}
}
import org.slf4j.LoggerFactory;
import javax.swing.SwingWorker;
import java.util.concurrent.ExecutionException;
/**
* No progress.
*/
public abstract class SimpleSwingWorker<V> extends SwingWorker<V, Void> {
@Override
protected final void done() {
anyway();
V v;
try {
v = get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return;
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause == null) {
cause = e;
}
onFailure(cause);
return;
}
onSuccess(v);
}
/**
* Invoked whether on success or failure
*/
protected void anyway() {
}
/**
* Invoked when a computation fails or is canceled.
*/
protected void onFailure(Throwable e) {
LoggerFactory.getLogger(getClass()).error(e.toString(), e);
}
/**
* Invoked with the result of the computation when it is successful.
*/
protected abstract void onSuccess(V v);
public void progress(int progress) {
super.setProgress(progress);
}
}
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* Leave only {@link WindowListener#windowClosed(WindowEvent)} undefined.
*/
@FunctionalInterface
public interface WindowClosedListener extends WindowListener {
@Override
default void windowOpened(WindowEvent e) {
}
@Override
default void windowClosing(WindowEvent e) {
}
@Override
default void windowIconified(WindowEvent e) {
}
@Override
default void windowDeiconified(WindowEvent e) {
}
@Override
default void windowActivated(WindowEvent e) {
}
@Override
default void windowDeactivated(WindowEvent e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment