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

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

Enable a JButton when some component changed its border color

问题

  1. public static boolean countBoards(JPanel panel){
  2. boolean red = false;
  3. for(Component control : panel.getComponents())
  4. {
  5. if(control instanceof JTextField)
  6. {
  7. JTextField ctrl = (JTextField) control;
  8. Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
  9. if(lineColor.equals(Color.green))
  10. red = true;
  11. }
  12. else if(control instanceof JComboBox)
  13. {
  14. JComboBox ctr = (JComboBox) control;
  15. Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
  16. if(lineColor.equals(Color.green))
  17. red = true;
  18. }
  19. }
  20. return red;
  21. }
英文:

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?

  1. public static boolean countBoards(JPanel panel){
  2. boolean red = false;
  3. for(Component control : panel.getComponents())
  4. {
  5. if(control instanceof JTextField)
  6. {
  7. JTextField ctrl = (JTextField) control;
  8. Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
  9. if(lineColor.equals(Color.red))
  10. red = true;
  11. }
  12. else if(control instanceof JComboBox)
  13. {
  14. JComboBox ctr = (JComboBox) control;
  15. Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
  16. if(lineColor.equals(Color.red))
  17. red = true;
  18. }
  19. }
  20. return red;
  21. }

答案1

得分: 0

当您更改组件的边框时,将触发属性侦听器。您可以在组合框/文本字段上注册属性侦听器,并根据新边框启用/禁用按钮。

示例:

  1. @Test
  2. public void test() {
  3. JButton myButton = new JButton();
  4. JComboBox<String> combo = new JComboBox<>();
  5. combo.addPropertyChangeListener("border", e -> {
  6. if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
  7. LineBorder border = (LineBorder) e.getNewValue();
  8. myButton.setEnabled(border.getLineColor().equals(Color.green));
  9. }
  10. });
  11. assertTrue(myButton.isEnabled(), "Button should be initially enabled.");
  12. combo.setBorder(BorderFactory.createLineBorder(Color.red));
  13. assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");
  14. combo.setBorder(BorderFactory.createLineBorder(Color.green));
  15. assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");
  16. }
英文:

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:

  1. @Test
  2. public void test() {
  3. JButton myButton = new JButton();
  4. JComboBox&lt;String&gt; combo = new JComboBox&lt;&gt;();
  5. combo.addPropertyChangeListener(&quot;border&quot;, e -&gt; {
  6. if (e.getNewValue() != null &amp;&amp; e.getNewValue() instanceof LineBorder) {
  7. LineBorder border = (LineBorder) e.getNewValue();
  8. myButton.setEnabled(border.getLineColor().equals(Color.green));
  9. }
  10. });
  11. assertTrue(myButton.isEnabled(), &quot;Button should be initially enabled.&quot;);
  12. combo.setBorder(BorderFactory.createLineBorder(Color.red));
  13. assertFalse(myButton.isEnabled(), &quot;Button should be disabled when red line border occurs.&quot;);
  14. combo.setBorder(BorderFactory.createLineBorder(Color.green));
  15. assertTrue(myButton.isEnabled(), &quot;Button should be enabled when green line border occurs.&quot;);
  16. }

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:

确定