Skip to content

Commit 551b62e

Browse files
committed
Fix minor layout alignments and implement demo for all widgets
1 parent 457f9cd commit 551b62e

4 files changed

Lines changed: 207 additions & 3 deletions

File tree

‎src/main/java/org/scijava/ui/swing/task/SwingTaskMonitorComponent.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void mouseClicked(java.awt.event.MouseEvent evt) {
261261
Task selectedTask = taskTableModel.getTask(row);
262262
if (selectedTask!=null) {
263263
if (SwingTaskMonitorComponent.this.confirmBeforeCancel) {
264-
int userconfirmation = JOptionPane.showConfirmDialog(null, "Do you really want to cancel this task ?", "Canceling " + selectedTask.getName(), JOptionPane.YES_NO_OPTION);
264+
int userconfirmation = JOptionPane.showConfirmDialog(null, "Do you really want to cancel this task?", "Canceling " + selectedTask.getName(), JOptionPane.YES_NO_OPTION);
265265
if (userconfirmation == JOptionPane.YES_OPTION) {
266266
selectedTask.cancel("User cancellation (table task)");
267267
}

‎src/main/java/org/scijava/ui/swing/widget/SwingInputWidget.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ public abstract class SwingInputWidget<T> extends
6969
public void set(final WidgetModel model) {
7070
super.set(model);
7171
uiComponent = new JPanel();
72+
// Zero column gaps: empty trailing columns must not take up space, so that
73+
// single-component widgets align with the right edge. Widgets needing
74+
// spacing between components add it explicitly
7275
final MigLayout layout =
73-
new MigLayout("fillx,ins 3 0 3 0", "[fill,grow|pref]");
76+
new MigLayout("fillx,ins 3 0 3 0", "[fill,grow]0[pref]0[pref]");
7477
uiComponent.setLayout(layout);
7578
}
7679

‎src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ else if (model.isStyle(NumberWidget.SLIDER_STYLE)) {
133133
spinner.setPreferredSize(spinnerSize);
134134
fixSpinner(type);
135135
setToolTip(spinner);
136-
getComponent().add(spinner);
136+
// add spacing only if there is a slider or scroll bar before the spinner
137+
getComponent().add(spinner, slider != null || scrollBar != null
138+
? "gapleft 6" : "");
137139
limitWidth(200);
138140
spinner.addChangeListener(this);
139141

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2026 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ui.swing.widget;
31+
32+
import java.io.File;
33+
import java.util.Date;
34+
35+
import org.scijava.Context;
36+
import org.scijava.ItemVisibility;
37+
import org.scijava.command.Command;
38+
import org.scijava.command.CommandService;
39+
import org.scijava.object.ObjectService;
40+
import org.scijava.plugin.Parameter;
41+
import org.scijava.ui.UIService;
42+
import org.scijava.util.ColorRGB;
43+
import org.scijava.widget.Button;
44+
import org.scijava.widget.ChoiceWidget;
45+
import org.scijava.widget.FileListWidget;
46+
import org.scijava.widget.FileWidget;
47+
import org.scijava.widget.NumberWidget;
48+
import org.scijava.widget.TextWidget;
49+
50+
/**
51+
* Shows one instance of every widget supported by the Swing UI in a single
52+
* dialog, for visual testing of layout and alignment (e.g., that all widgets
53+
* share the same right edge).
54+
*/
55+
public class SwingWidgetLayoutDemo implements Command {
56+
57+
/** Dummy type to populate the object widget. */
58+
public static class Thing {
59+
60+
private final String name;
61+
62+
public Thing(final String name) {
63+
this.name = name;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return name;
69+
}
70+
}
71+
72+
// -- Message --
73+
74+
@Parameter(visibility = ItemVisibility.MESSAGE)
75+
private String message = "Every supported widget, for layout testing";
76+
77+
// -- Text --
78+
79+
@Parameter(label = "Text (default)", persist = false)
80+
private String text = "text";
81+
82+
@Parameter(label = "Text (field)", style = TextWidget.FIELD_STYLE,
83+
persist = false)
84+
private String field = "field";
85+
86+
@Parameter(label = "Text (password)", style = TextWidget.PASSWORD_STYLE,
87+
persist = false)
88+
private String password = "secret";
89+
90+
@Parameter(label = "Text (area)", style = TextWidget.AREA_STYLE,
91+
persist = false)
92+
private String area = "area\nmultiple lines";
93+
94+
@Parameter(label = "Character", persist = false)
95+
private Character character = 'c';
96+
97+
// -- Number --
98+
99+
@Parameter(label = "Number (spinner)", persist = false)
100+
private Double spinner = 1.5;
101+
102+
@Parameter(label = "Number (integer)", min = "0", max = "100",
103+
persist = false)
104+
private Integer integer = 10;
105+
106+
@Parameter(label = "Number (slider)", style = NumberWidget.SLIDER_STYLE,
107+
min = "0", max = "10", stepSize = "0.01", persist = false)
108+
private Double slider = 1d;
109+
110+
@Parameter(label = "Number (scroll bar)",
111+
style = NumberWidget.SCROLL_BAR_STYLE, min = "0", max = "10",
112+
stepSize = "0.01", persist = false)
113+
private Double scrollBar = 1d;
114+
115+
// -- Boolean --
116+
117+
@Parameter(label = "Toggle", persist = false)
118+
private Boolean toggle = true;
119+
120+
// -- Choices --
121+
122+
@Parameter(label = "Choice (combo box)", choices = { "One", "Two", "Three" },
123+
persist = false)
124+
private String choice = "One";
125+
126+
@Parameter(label = "Choice (radio, horizontal)",
127+
style = ChoiceWidget.RADIO_BUTTON_HORIZONTAL_STYLE,
128+
choices = { "One", "Two", "Three" }, persist = false)
129+
private String radioH = "One";
130+
131+
@Parameter(label = "Choice (radio, vertical)",
132+
style = ChoiceWidget.RADIO_BUTTON_VERTICAL_STYLE,
133+
choices = { "One", "Two", "Three" }, persist = false)
134+
private String radioV = "One";
135+
136+
@Parameter(label = "Object", persist = false)
137+
private Thing thing;
138+
139+
// -- Files --
140+
141+
@Parameter(label = "File (open)", style = FileWidget.OPEN_STYLE,
142+
persist = false)
143+
private File open;
144+
145+
@Parameter(label = "File (open, tif/png)",
146+
style = FileWidget.OPEN_STYLE + ",extensions:tif/png", persist = false)
147+
private File openFiltered;
148+
149+
@Parameter(label = "File (save)", style = FileWidget.SAVE_STYLE,
150+
persist = false)
151+
private File save;
152+
153+
@Parameter(label = "Folder", style = FileWidget.DIRECTORY_STYLE,
154+
persist = false)
155+
private File directory;
156+
157+
@Parameter(label = "File list (files)", style = FileListWidget.FILES_ONLY,
158+
persist = false)
159+
private File[] files;
160+
161+
@Parameter(label = "File list (folders)",
162+
style = FileListWidget.DIRECTORIES_ONLY, persist = false)
163+
private File[] folders;
164+
165+
@Parameter(label = "File list (both)",
166+
style = FileListWidget.FILES_AND_DIRECTORIES, persist = false)
167+
private File[] both;
168+
169+
// -- Other --
170+
171+
@Parameter(label = "Date", persist = false)
172+
private Date date = new Date();
173+
174+
@Parameter(label = "Color", persist = false)
175+
private ColorRGB color = new ColorRGB(255, 128, 0);
176+
177+
@Parameter(label = "Button", callback = "onButton", persist = false)
178+
private Button button;
179+
180+
@Override
181+
public void run() {
182+
// Nothing to do
183+
}
184+
185+
@SuppressWarnings("unused")
186+
private void onButton() {
187+
System.out.println("Button pressed");
188+
}
189+
190+
public static void main(final String... args) throws Exception {
191+
final Context context = new Context();
192+
final ObjectService objectService = context.service(ObjectService.class);
193+
objectService.addObject(new Thing("Thing A"));
194+
objectService.addObject(new Thing("Thing B"));
195+
context.service(UIService.class).showUI();
196+
context.service(CommandService.class).run(SwingWidgetLayoutDemo.class,
197+
true);
198+
}
199+
}

0 commit comments

Comments
 (0)