Skip to content

Commit c7d104e

Browse files
authored
Merge pull request #26 from maarzt/fix-text-area
Fix Issue #24: Parameter with TextWidget.AREA_STYLE crashes the UI
2 parents 9b795f1 + f959e97 commit c7d104e

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingTextWidget.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.swing.JPasswordField;
3535
import javax.swing.JTextArea;
3636
import javax.swing.JTextField;
37+
import javax.swing.JScrollPane;
3738
import javax.swing.event.DocumentEvent;
3839
import javax.swing.event.DocumentListener;
3940
import javax.swing.text.AbstractDocument;
@@ -95,8 +96,10 @@ public void set(final WidgetModel model) {
9596
final int columns = model.getItem().getColumnCount();
9697

9798
// construct text widget of the appropriate style, if specified
99+
boolean addScrollPane = false;
98100
if (model.isStyle(TextWidget.AREA_STYLE)) {
99101
textComponent = new JTextArea("", 5, columns);
102+
addScrollPane = true;
100103
}
101104
else if (model.isStyle(TextWidget.PASSWORD_STYLE)) {
102105
textComponent = new JPasswordField("", columns);
@@ -105,7 +108,7 @@ else if (model.isStyle(TextWidget.PASSWORD_STYLE)) {
105108
textComponent = new JTextField("", columns);
106109
}
107110
setToolTip(textComponent);
108-
getComponent().add(textComponent);
111+
getComponent().add(addScrollPane ? new JScrollPane(textComponent) : textComponent);
109112
limitLength();
110113
textComponent.getDocument().addDocumentListener(this);
111114

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.ui.swing.widget;
32+
33+
/**
34+
* Demonstrate SwingTextWidget
35+
*
36+
* @author Matthias Arzt
37+
*/
38+
39+
import org.scijava.Context;
40+
import org.scijava.command.Command;
41+
import org.scijava.command.CommandService;
42+
import org.scijava.plugin.Parameter;
43+
import org.scijava.ui.UIService;
44+
import org.scijava.widget.TextWidget;
45+
46+
public class SwingTextWidgetDemo implements Command {
47+
48+
@Parameter(label = "Default")
49+
private String string;
50+
51+
@Parameter(label = "Password Style", style = TextWidget.PASSWORD_STYLE)
52+
private String password;
53+
54+
@Parameter(label = "Field Style", style = TextWidget.FIELD_STYLE)
55+
private String field;
56+
57+
@Parameter(label = "Area Style", style = TextWidget.AREA_STYLE)
58+
private String area;
59+
60+
@Override
61+
public void run() {
62+
System.out.println(string);
63+
System.out.println(password);
64+
System.out.println(field);
65+
System.out.println(area);
66+
}
67+
68+
public static void main(final String... args) throws Exception {
69+
Context context = new Context();
70+
context.service(UIService.class).showUI();
71+
context.service(CommandService.class).run(SwingTextWidgetDemo.class, true);
72+
}
73+
}

0 commit comments

Comments
 (0)