0

I have a Java Swing Dialog with a hidden JLabel above each input component (i.e. JTextField). The purpose of this hidden JLabel is to use it as validation output for its input component.

Let's say, there is an input field for the description of some entity, which has to be non empty, and should contain some special stuff. On error, the action could call the following method:

private void invalidateDescription(String errMessage) {
    errDescriptionLabel.setText(errMessage);                      
    errDescriptionLabel.setVisible(true);
    descriptionTextField.setBackground(ERR_COLOR);
}

After that, I call pack() and invalidate()

The problem is, that the JDialog still has the same vertical size, so that some of the components (the buttons in the bottom of the dialog) disapear (because they're out of view).

Do you have any suggestion how to fix it?

Best Regards.

edit: I forgott to mention, that the JDialog has a "Free Design" Layout (Netbeans GUI Builder default). edit 2: I'm looking for a solution which doesn't require kind of a placeholder for (error) JLabel. "Empty Space" is not a desired solution because the dialog doesn't look balanced.

4

3 回答 3

1

如有必要,请使用CardLayout放置您的标签和空答案JPanels

于 2013-06-06T12:23:21.780 回答
1

我有一个 Java Swing 对话框,每个输入组件上方都有一个隐藏的 JLabel

我不会为此使用隐藏组件。我会改变:

private void invalidateDescription(String errMessage)

private void invalidateDescription(String errMessage, component inputComponent)

然后我会显示一个带有错误消息的弹出窗口。您可以使用未装饰的 JDialog 作为弹出窗口。您甚至可以使用 JPopupMenu 作为弹出窗口。

当您显示弹出窗口时,您会将弹出窗口相对于输入组件定位。

于 2013-06-06T15:11:35.900 回答
1

当条目有效时,不要使用setVisible(),而是提供errDescriptionLabel与封闭面板匹配的背景颜色。

于 2013-06-06T13:14:24.943 回答