I'm going to make an application (in Swing) that uses a tree to visualize a data structure (JTree). A tree will be on the left side of a window. The user will be able to browse a tree. The parameters of every tree node will be displayed on the right side of the window. The example windows will be looking like this
我将创建一个使用树来显示数据结构(JTree)的应用程序(在Swing中)。树将位于窗口的左侧。用户将能够浏览树。每个树节点的参数将显示在窗口的右侧。示例窗口看起来像这样
===========================
| tree panel | data panel |
| | |
| | |
| | |
---------------------------
The problem arises when a user wants to change that data. when should I validate them ? The easy approach is to open a new modal dialog (JDialog) and let the user to change this data in it. Validation of data would take place in an "ok" button listener method but this is a little clunky. I would like to allow the user to edit those data right in the data panel. In that case when should I validate them?
当用户想要更改该数据时会出现问题。我什么时候应该验证它们?简单的方法是打开一个新的模态对话框(JDialog),让用户在其中更改这些数据。数据的验证将在“ok”按钮监听器方法中进行,但这有点笨重。我想允许用户在数据面板中编辑这些数据。在那种情况下,我应该何时验证它们?
Is there a pattern of such solution in Swing?
Swing中是否有这种解决方案的模式?
Or any online tutorial how to do it?
或者任何在线教程怎么做?
Thanks in advance.
提前致谢。
2 个解决方案
#1
1
Dialogs are bad.
对话很糟糕。
Immediately discard any complete nonsense input immediately. For instance, typing a letter in the numerical field (use Document filters). Don't beep. Don't require any particular commit step. You may have retain partially entered data.
立即丢弃任何完整的废话输入。例如,在数字字段中键入字母(使用文档过滤器)。不要发出哔哔声。不需要任何特定的提交步骤。您可能保留了部分输入的数据。
#2
0
I'm not totally sure what you're after, but..
我不完全确定你追求的是什么,但是......
You could maybe add this "ok" button (or "commit changes" or whatever) to data panel and when the button would be pressed, you would validate the data and save the changes if the changes are valid?
您可以将此“ok”按钮(或“提交更改”或其他内容)添加到数据面板,当按下该按钮时,如果更改有效,您将验证数据并保存更改?
(So you'd have editable components at the data panel)
(所以你在数据面板上有可编辑的组件)
Edit: if this wasn't good, could you clarify me a bit:
编辑:如果这不好,你能否澄清一点:
- Is the data panel showing data for one item of the tree at time?
- What kind of data is there to change (and to validate)
数据面板是否显示了树中一个项目的数据?
什么样的数据可以改变(并验证)
Anyway, if you want to validate straight the changes made to an edit component (for example JTextField), you can use for example
无论如何,如果你想直接验证对编辑组件所做的更改(例如JTextField),你可以使用例如
- Formatted text fields, see How to Use Formatted Text Fields
- For more general validating, see InputVerifier
- More ideas, see Validating Numerical Input in a JTextField (concentrates of numerical input but usable for other purposes also)
格式化文本字段,请参见如何使用格式化文本字段
有关更一般的验证,请参阅InputVerifier
更多想法,请参阅在JTextField中验证数字输入(集中数字输入但也可用于其他目的)
#1
1
Dialogs are bad.
对话很糟糕。
Immediately discard any complete nonsense input immediately. For instance, typing a letter in the numerical field (use Document filters). Don't beep. Don't require any particular commit step. You may have retain partially entered data.
立即丢弃任何完整的废话输入。例如,在数字字段中键入字母(使用文档过滤器)。不要发出哔哔声。不需要任何特定的提交步骤。您可能保留了部分输入的数据。
#2
0
I'm not totally sure what you're after, but..
我不完全确定你追求的是什么,但是......
You could maybe add this "ok" button (or "commit changes" or whatever) to data panel and when the button would be pressed, you would validate the data and save the changes if the changes are valid?
您可以将此“ok”按钮(或“提交更改”或其他内容)添加到数据面板,当按下该按钮时,如果更改有效,您将验证数据并保存更改?
(So you'd have editable components at the data panel)
(所以你在数据面板上有可编辑的组件)
Edit: if this wasn't good, could you clarify me a bit:
编辑:如果这不好,你能否澄清一点:
- Is the data panel showing data for one item of the tree at time?
- What kind of data is there to change (and to validate)
数据面板是否显示了树中一个项目的数据?
什么样的数据可以改变(并验证)
Anyway, if you want to validate straight the changes made to an edit component (for example JTextField), you can use for example
无论如何,如果你想直接验证对编辑组件所做的更改(例如JTextField),你可以使用例如
- Formatted text fields, see How to Use Formatted Text Fields
- For more general validating, see InputVerifier
- More ideas, see Validating Numerical Input in a JTextField (concentrates of numerical input but usable for other purposes also)
格式化文本字段,请参见如何使用格式化文本字段
有关更一般的验证,请参阅InputVerifier
更多想法,请参阅在JTextField中验证数字输入(集中数字输入但也可用于其他目的)