Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a way to make all of these actionlisteners into one statement?
jbtn0.addActionListener(this);
jbtn1.addActionListener(this);
jbtn2.addActionListener(this);
jbtn3.addActionListener(this);
jbtn4.addActionListener(this);
jbtn5.addActionListener(this);
jbtn6.addActionListener(this);
jbtn7.addActionListener(this);
jbtn8.addActionListener(this);
jbtn9.addActionListener(this);
jbtnAdd.addActionListener(this);
jfrm.add(jbtn0);
jfrm.add(jbtn1);
jfrm.add(jbtn2);
jfrm.add(jbtn3);
jfrm.add(jbtn4);
jfrm.add(jbtn5);
jfrm.add(jbtn6);
jfrm.add(jbtn7);
jfrm.add(jbtn8);
jfrm.add(jbtn9);
I am rather new to java, and I am using eclipse.
Yes, in a way. I do this sometimes:
for(JButton btn : new JButton[] {
jbtn0, jbtn1, jbtn2, ... , jbtn9
}) {
jbtn.addActionListener(this);
jfrm.add(jbtn);
}
I do that even when my components do not start out in an array, for example if I am filling a JToolBar and need to refer to the components by name somewhere else.
Judging by your naming scheme, it may also be advantageous for you to use an array from the beginning.
As pzaenger said in his comment, you could add all your buttons into an ArrayList or an array, and then just iterate through all the entries and add an actionlistener to each one.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to create JLabels in a loop and remove them at the End. So I need to give every Label its own name (Like Label[1], Label[2],...). I'm very new to Java so I can't imagine how to solve this problem. I already tried different ways but they all didn't work.
I am not sure but you can add a number of anonymous objects(in this case they will be JLabel or Label) to the Array List without naming them.(You need to use for loops). If you want to delete them, you need to use the same logic.
Let me know this works or not. If you stuck again, I can help you by writing the code. However, writing yourself will be more beneficial for you.
Have a nice day !!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm thinking on possibility of making a Huffman code without "node".
It's based on the nodes then my question seems a bit ambiguous.
I know,maybe we have to use string's abilites ...
Is it possible ? and if "YES" how Is it possible in java?
Thanks.
You can use indices instead of nodes but you need somewhere nodes and/or vertices.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm debugging an application and realized that the debugger displays String[] in two different ways, namely these two: ['test'] and [test]. Those displayed in the second way work for my purpose and the other doesn't.
What's the difference between these two?
It sounds like in the first case, the single quote characters are part of the string. Try adding code to remove them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am a very new programmer, it is part of my degree requirements and I am having a hard time with it. I am working on basically a three part GUI and I am wondering how I would create a JTextArea that will display each item that is entered.
Start by taking a look at Creating a GUI With JFC/Swing, this will give you a basic grounding in how to use Swing.
Take a closer look at How to Use Text Areas for specific details about how to use JTextArea
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm currently making a mortgage calculator, but I need some help. I have a reset button on my GUI that makes all of the forms blank again after typing something into them. The IDE already helped me make the button, so here is where I'm at:
private void resetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
What do I need to put in here?
You either recreate your complete GUI (probably not an option), or painstakingly use .setText("") against each and every input field you have, and similar logic for radio buttons, etc. There's no royal way to it, unfortunately.