Skip to content

Commit 19ecbdf

Browse files
committed
SwingSearchBar: make top-right buttons extensible
Now we use a JToolBar so that additional buttons can be added. We do not always include a close button, because in the default floating dialog mode, it is not needed. Instead, a close button can now be added in the following way: searchBar.addButton("\u2715", "Close the search results pane", ae -> searchBar.close());
1 parent 03865a9 commit 19ecbdf

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/main/java/org/scijava/ui/swing/search/SwingSearchBar.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.awt.Font;
3838
import java.awt.GridLayout;
3939
import java.awt.Window;
40+
import java.awt.event.ActionListener;
4041
import java.awt.event.FocusEvent;
4142
import java.awt.event.FocusListener;
4243
import java.awt.event.KeyAdapter;
@@ -71,6 +72,7 @@
7172
import javax.swing.JTextArea;
7273
import javax.swing.JTextField;
7374
import javax.swing.JTextPane;
75+
import javax.swing.JToolBar;
7476
import javax.swing.ScrollPaneConstants;
7577
import javax.swing.SwingUtilities;
7678
import javax.swing.UIManager;
@@ -111,7 +113,7 @@ public class SwingSearchBar extends JTextField {
111113
private static final int PAD = 5;
112114

113115
private final DocumentListener documentListener;
114-
private final JButton closeButton;
116+
private final JToolBar buttons;
115117

116118
@Parameter
117119
private SearchService searchService;
@@ -180,13 +182,8 @@ public void focusLost(final FocusEvent e) {
180182
}
181183
});
182184

183-
closeButton = new JButton("<html><span style=\"font-size: 2.5em\">\u2612</span>");
184-
closeButton.setToolTipText("Close the search results pane");
185-
closeButton.setBorder(null);
186-
closeButton.addActionListener(ae -> {
187-
reset();
188-
loseFocus();
189-
});
185+
buttons = new JToolBar();
186+
buttons.setFloatable(false);
190187
}
191188

192189
/** Called externally to bring the search bar into focus. */
@@ -197,6 +194,12 @@ public void activate() {
197194
});
198195
}
199196

197+
/** Closes the search results pane. */
198+
public void close() {
199+
reset();
200+
loseFocus();
201+
}
202+
200203
/** Gets the maximum number of results per search category. */
201204
public int getResultLimit() {
202205
return resultLimit;
@@ -218,6 +221,16 @@ public void setMouseoverEnabled(final boolean mouseoverEnabled) {
218221
this.mouseoverEnabled = mouseoverEnabled;
219222
}
220223

224+
/** Adds a button to the search pane's toolbar. */
225+
public void addButton(final String label, final String tooltip,
226+
final ActionListener action)
227+
{
228+
final JButton button = new JButton(label);
229+
if (tooltip != null) button.setToolTipText(tooltip);
230+
if (action != null) button.addActionListener(action);
231+
buttons.add(button);
232+
}
233+
221234
// -- Utility methods --
222235

223236
// TODO: Move this method to PluginService.
@@ -536,7 +549,7 @@ public void mouseMoved(final MouseEvent e) {
536549
}
537550
});
538551

539-
detailsPane.add(closeButton, "pos n 0 100% n");
552+
detailsPane.add(buttons, "pos n 0 100% n");
540553
detailsPane.add(detailsTitle, "growx, pad 0 0 0 -20");
541554
detailsPane.add(detailsScrollPane, "growx, hmin 0, wmin 0");
542555
detailsPane.add(detailsButtons, "growx");

0 commit comments

Comments
 (0)