英文:
Enable a JButton when some component changed its border color
问题
public static boolean countBoards(JPanel panel){
boolean red = false;
for(Component control : panel.getComponents())
{
if(control instanceof JTextField)
{
JTextField ctrl = (JTextField) control;
Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
if(lineColor.equals(Color.green))
red = true;
}
else if(control instanceof JComboBox)
{
JComboBox ctr = (JComboBox) control;
Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
if(lineColor.equals(Color.green))
red = true;
}
}
return red;
}
英文:
My goal is to enable a JButton only when some JTextFields and JComboBox change its border color from red to green.<br>
These components are included in three different JPanel.<br>
I try to create a function that reads all components in a JPanel but, when I'm going to compared the colors, the program returns me that I'm casting the variables in a bad way.<br>
Below there's my function.<br>
Can someone help me?
public static boolean countBoards(JPanel panel){
boolean red = false;
for(Component control : panel.getComponents())
{
if(control instanceof JTextField)
{
JTextField ctrl = (JTextField) control;
Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
else if(control instanceof JComboBox)
{
JComboBox ctr = (JComboBox) control;
Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
}
return red;
}
答案1
得分: 0
当您更改组件的边框时,将触发属性侦听器。您可以在组合框/文本字段上注册属性侦听器,并根据新边框启用/禁用按钮。
示例:
@Test
public void test() {
JButton myButton = new JButton();
JComboBox<String> combo = new JComboBox<>();
combo.addPropertyChangeListener("border", e -> {
if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
LineBorder border = (LineBorder) e.getNewValue();
myButton.setEnabled(border.getLineColor().equals(Color.green));
}
});
assertTrue(myButton.isEnabled(), "Button should be initially enabled.");
combo.setBorder(BorderFactory.createLineBorder(Color.red));
assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");
combo.setBorder(BorderFactory.createLineBorder(Color.green));
assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");
}
英文:
When you change the border of a component, a property listener will be fired. You can register a property listener to the combobox/textfield and enable/disable the button according to the new border.
An example:
@Test
public void test() {
JButton myButton = new JButton();
JComboBox<String> combo = new JComboBox<>();
combo.addPropertyChangeListener("border", e -> {
if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
LineBorder border = (LineBorder) e.getNewValue();
myButton.setEnabled(border.getLineColor().equals(Color.green));
}
});
assertTrue(myButton.isEnabled(), "Button should be initially enabled.");
combo.setBorder(BorderFactory.createLineBorder(Color.red));
assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");
combo.setBorder(BorderFactory.createLineBorder(Color.green));
assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论