I want to make it shorter, making them all true in one line, I don't know if this is possible
bckBtn.setEnabled(true);
cBtn.setEnabled(true);
addBtn.setEnabled(true);
btn7.setEnabled(true);
someone gave me this line but I couldn't understand it
Streams.of(obj1, obj2).forEach(obj -> obj.setEnable(true));
sorry if the question was easy, I'm new to java.
thanks again.
Here’s the simplest, pre-java 8 and conceptually easiest, code I could come up with.
for (Button button : Arrays.asList(bckBtn, cBtn, addBtn, etc)) {
button.setEnabled(true);
}
Try this:
Button[] buttons = {bckBtn, cBtn, addBtn, ...};
for (int i = 0; i < buttons.length; i++)
{
Button temp = buttons[i]; // seconds reference to the buttons[i] object
temp.setEnabled(true);
}
Related
So I have 44 buttons in my program, and for one of my methods I want to re-enable all of them. the buttons are easily named btn1, btn2, btn3...btn44. Is there a way I can use a for loop to enable all of them?
I would love to do something like this but I cannot find the resources necessary.
for(int i == 0, i < 44, i++){
btn<i>.setEnabled(true);
}
Without that I would have to go through each button
btn1.setEnabled(true);
btn2.setEnabled(true);
...
btn44.setEnabled(true);
I know this alternate method isn't that bad but I have similar areas in my code where a technique like the one I am looking for would be very useful. Thank you!
You should make an array of buttons:
Button[] buttons = new Button[44];
for (int i = 0; i < 44; i++) {
// Do stuff with buttons[i]
}
You can't get the value of a variable using its string name representation because that would not compile very well; it is possible but not just using Java and it would require some weird roundabout way of doing it. Just use an array.
Create a list to store all the buttons and the iterate it.
...
List<Button> buttons = new ArrayList<>();
buttons.add(btn1);
buttons.add(btn2);
...
buttons.add(btn42);
And then use that list for mass actions:
void setStatus(boolean enabled) {
for (Button b : buttons ) {
b.setEnabled(enabled);
}
}
You could add you Buttons to a collection, like a List (if you have not done so already). Then it's easier to iterate over them.
// this list will grow automatically when you add new elements
List<Button> buttons = new ArrayList<>();
// when you create a button in your code, add them to your collection/list
buttons.add(new Button("1"));
buttons.add(new Button("2"));
buttons.add(new Button("3"));
buttons.add(new Button("4"));
buttons.add(new Button("5"));
// etc.
// in Java 8 you can use lambdas to update your buttons like this
buttons.forEach(button -> button.setEnabled(true));
Just make sure you are importing the correct version of List.
I.e make sure you use this list import java.util.List; and not the one in the windowing toolkit (i.e. not import java.awt.List;)
We have this warm up exercise where we're supposed to create this reallllly simple game which's UI is pretty much set up
.
I got the error "Local variable i defined in an enclosing scope must be final or effectively final".
I didn't understand it, so I googled it, but most of the problems are different.
While typing this question I found this in the stackoverflow suggestions:
Assigning an action to each button, in an array of buttons in JavaFX
But I simply don't understand. I'm learning programming/java from scratch. I hope JavaFX/GuI stuff isn't a hindrance?
The code below only includes my attempt to assign the actions. I separated it from the creation of the buttons for the time being to figure out what the problem is. The problem is only in the if and else conditions.
for(int i=0; i<=4; i++) {
for(int j=0; j<=4; j++) {
buttonGrid[i][j].setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
if (buttonGrid[i][j].getText() == "X") {
buttonGrid[i][j].setText("O");
} else {
buttonGrid[i][j].setText("X");
}
}
});
}
}
For now I just want the button's labels to change from X to O and from O to X as you click them. Btw. If I learn JavaFX and GUI, does it mean I HAVE to learn css? (Not that I don't want to, just.. not now)
If there is a need for the rest of the code to figure the problem:
http://textuploader.com/5b1kh
I'd also appreciate if someone could tell me how to do the Scenes in a more efficent way. (Btw, can I somehow lock the aspect ratio of the sides of all cells of a gridpane?)
I think that the answer in this question explains your problem very well and how to solve it Problems with local variable scope. How to solve it? You can not use i and j inside the Action handler.
Try this. [Notice that I've also changed the string comparison*]
for(int i=0; i<=4; i++) {
for(int j=0; j<=4; j++) {
final Button myButton = buttonGrid[i][j];
myButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
if ("X".equals(myButton.getText())) {
myButton.setText("O");
} else {
myButton.setText("X");
}
}
});
}
}
[*] How do I compare strings in Java?
I have a problem that I don't really know how to add a multiple lines into Label in JavaFX.
For example:
Label label = new Label();
for(int i= 0; i<10; i++){
label.setText(Integer.toString(i));
}
So when the loop finishes, the label just only shows the final value which is 9.
So any solutions that can show all the numbers 1 - 9 with the break lines( such as '\n') between them.
This problem that happens when i want to show the Bill of my project that contain many dishes.
Thank you for your help.
You need to append and not set the text over and over again
AND you need the new line character '\n'
my suggestion would be like using a variable to append the information and when you are done with that step, then set the label.text
Example:
StringBuilder msg = new StringBuilder():
Label label = new Label();
for (int i = 0; i < 10; i++) {
msg.append(Integer.toString(i));
msg.append(",\n"); //this is the new line you need
}
label.setText(msg.toString());
I am trying to get the Decimal point to display with the click of an GUI button. I using the method below, which works fine for the numbers but not the decimal point. Any advise would be great. thanks
private void displayWeightedquantity(ActionEvent event){
JButton currentButton = null;
JButton[] numberButtonsarray = {bnDecimal,bnZero,bnOne, bnTwo, bnThree, bnFour, bnFive,bnSix,bnSeven,
bnEight,bnNine};
currentButton = (JButton)event.getSource();
for (int i = 0; i <numberButtonsarray.length; i++){
if (currentButton == numberButtonsarray[i]){
lbDisplayitemQty.setText(lbDisplayitemQty.getText() + currentButton.getText());
break;
}
}
}
My virtual numberpad :
I suspect the problem is else where in your code.
The test I set up works find.
You need to provide a functional example the shows the problem, not just a code snippet.
Also, make sure that the decimal button has it's action listener attached
Here's the ActionListener portion of my code. Focus on the reset button.
public void actionPerformed( ActionEvent e) {
int i;
for (i = 0; i < 26; i++) {
if (e.getSource() == a[i]) {
consultWord(i);
}
}
if (e.getSource() == reset) {
Hangman gui = new Hangman();
System.out.print("test");
gui.go();
}
}
Obviously there is much more above it (as this is at the VERY end). Button array 1 (top if statement) works perfectly. Button 2 (bottom if statement) doesn't work at all. The test output text does not appear. Here is where I declared the variables. (They are available to all code).
JButton reset = new JButton("Reset");
private Button a[];
And if it means anything to you, here's the code for setting up the a[] buttons.
int i;
StringBuffer buffer;
a = new Button[26];
topPanel.setLayout( new GridLayout( 4,0, 10, 10) );
for (i = 0; i <26; i++) {
buffer = new StringBuffer();
buffer.append((char)(i+'a'));
a[i] = new Button(buffer.toString());
a[i].setSize(100,100);
a[i].addActionListener( this );
topPanel.add(a[i]);
}
Any ideas why my bottom button isn't doing squat? I'll paste my whole code if need be.
Maybe you simply forgot to add the ActionListener to your reset button? This is missing from your code above…
As a side note some suggestions to make your code cleaner:
The StringBuffer is not needed: Simply use String.valueOf((char)(i+'a'))
I would not use the same ActionListener for all buttons you have, as this clutters your actionPerformed method. Anonymous inner classes can be useful here.