how to disable mulitple JButtons in one class from another class in Java swing?

I have this code snippet (a part of code actually) where in one class I have all the Jbutton created for 26 alphabets. I have another class that keeps track of time and when the time is over or the game is over, I want to disable all the 26 JButtons in one shot. here is the code for creating Jbuttons public class DetailsPanel extends JPanel { public DetailsPanel() { setLayout(new BorderLayout()); setBorder(BorderFactory.createTitledBorder(" click here ")); JPanel letterPanel = new JPanel(new GridLayout(0, 5)); for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) { String buttonText = String.valueOf(alphabet); JButton letterButton = new JButton(buttonText); letterButton.addActionListener(clickedbutton()); letterPanel.add(letterButton, BorderLayout.CENTER); } add(letterPanel, BorderLayout.CENTER); } } In my `maincontrol class`, I want to turn off all the Jbuttons like in public class maincontrol { int counter; DetailsPanel dp; public maincontrol(DetailsPanel dp) { this.dp = dp; int counter = 0; } public void turnoff(){ if ( counter>10){ //turn off all here// } } }
Why not simply put the buttons, or perhaps better their AbstractActions, in a collection, and give it a setEnabled(boolean enabled) method. In the method iterate through the collection setting the enabled property of each button or Action.

以上就是how to disable mulitple JButtons in one class from another class in Java swing?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏