启用一个JButton,当某个组件改变其边框颜色时。

huangapple go评论75阅读模式
英文:

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&lt;String&gt; combo = new JComboBox&lt;&gt;();
	combo.addPropertyChangeListener(&quot;border&quot;, e -&gt; {
		if (e.getNewValue() != null &amp;&amp; e.getNewValue() instanceof LineBorder) {
			LineBorder border = (LineBorder) e.getNewValue();
			myButton.setEnabled(border.getLineColor().equals(Color.green));
		}
	});
	assertTrue(myButton.isEnabled(), &quot;Button should be initially enabled.&quot;);

	combo.setBorder(BorderFactory.createLineBorder(Color.red));
	assertFalse(myButton.isEnabled(), &quot;Button should be disabled when red line border occurs.&quot;);

	combo.setBorder(BorderFactory.createLineBorder(Color.green));
	assertTrue(myButton.isEnabled(), &quot;Button should be enabled when green line border occurs.&quot;);
}

huangapple
  • 本文由 发表于 2020年8月19日 06:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63477711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定