Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java GUI

Options
  • 16-02-2017 1:59pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi im making a GUI that allows a user to pick 4 numbers from 1 to 28. You must pick by clicking the buttons. There is 28 buttons labelled 1 to 28. I am taking input as follows..
     private void no1InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            no1InputButton.setText("1");
        }                                              
    
        private void no2InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            no2InputButton.setText("2");
        }                                              
    
        private void no3InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
             no3InputButton.setText("3");
        }             
    

    Just wondering is there a bettter way to do this as making one of these for all 28 buttons makes me think that there is a better way to do it? Thanks!


Comments

  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    Hi im making a GUI that allows a user to pick 4 numbers from 1 to 28. You must pick by clicking the buttons. There is 28 buttons labelled 1 to 28. I am taking input as follows..
     private void no1InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            no1InputButton.setText("1");
        }                                              
    
        private void no2InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
            no2InputButton.setText("2");
        }                                              
    
        private void no3InputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
             no3InputButton.setText("3");
        }             
    

    Just wondering is there a bettter way to do this as making one of these for all 28 buttons makes me think that there is a better way to do it? Thanks!

    Can you not assign all the buttons to the same click listener? The listener method can then get the value of the button object that was clicked and know what number it was
    I've done it in XAML for WPF, and I'm sure that Swing would be similar enough


  • Registered Users Posts: 895 ✭✭✭Dubba


    You can use lists and an enhanced for loop. This is an example in JavaFX:
            Button btn1 = new Button();
            Button btn2 = new Button();
            Button btn3 = new Button();
            Button btn4 = new Button();
    
            List<String> buttonText = Arrays.asList("1", "2", "3", "4");
            List<Button> buttonList = Arrays.asList(btn1, btn2, btn3, btn4);
    
            int i = 0;
            for (Button button : buttonList) {
                i++;
                button.setText(buttonText.get(i - 1));
            }
    

    I'm not sure if this is good coding practice tho, as I'm a beginner as well.
    ___________________

    Edit - Probably a better way of iterating:
            for (int i = 0; i < buttonList.size(); i++) {
                buttonList.get(i).setText(buttonText.get(i));
            }
    
            // listener
            for (Button button : buttonList) {
                button.setOnAction(event -> System.out.println(button.getText()));
            }
    


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Thanks for answers I'll check out what both of ye said!


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Can someone explain why this is not working. This is to add the random numbers into array
    private void quickPickButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
           for (int i=0; i<numberSelection.length;i++){
               numberSelection[i] = (int)(Math.random()*28) + 1;
           }
           numberSelectionList.removeAll();
           for (int i=0; i<numberSelection.length;i++){
               int value = numberSelection[i];
              numberSelectionList.add(Integer.toString(value));
           
           }       
        }  
    

    Then this adds into the winning numbers

    private void drawButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            for (int i=0; i<winningNumbers.length;i++){
                winningNumbers[i] = (int)(Math.random()*28) + 1;
            }
            winningNumbersList.removeAll();
            for (int i=0; i<winningNumbers.length;i++){
                int value = winningNumbers[i];
                winningNumbersList.add(Integer.toString(value));
             }
            
            for (int i=0; i<winningNumbers.length;i++){
                for (int j =0; j <numberSelection.length; j++){
                     if (winningNumbers == numberSelection){
                        winLossLabel.setText("Congratulations you won!");
                }
                     else if (winningNumbers != numberSelection){
                         winLossLabel.setText("Unlucky! Try again");
                    }
                }
            }
              
        }           
    

    Can someone explain why"Unlucky! Try again" is displayed even if numbers match.


  • Registered Users Posts: 2,501 ✭✭✭PabloAndRoy


    Can someone explain why this is not working. This is to add the random numbers into array
    private void quickPickButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
           for (int i=0; i<numberSelection.length;i++){
               numberSelection[i] = (int)(Math.random()*28) + 1;
           }
           numberSelectionList.removeAll();
           for (int i=0; i<numberSelection.length;i++){
               int value = numberSelection[i];
              numberSelectionList.add(Integer.toString(value));
           
           }       
        }  
    

    Then this adds into the winning numbers

    private void drawButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            for (int i=0; i<winningNumbers.length;i++){
                winningNumbers[i] = (int)(Math.random()*28) + 1;
            }
            winningNumbersList.removeAll();
            for (int i=0; i<winningNumbers.length;i++){
                int value = winningNumbers[i];
                winningNumbersList.add(Integer.toString(value));
             }
            
            for (int i=0; i<winningNumbers.length;i++){
                for (int j =0; j <numberSelection.length; j++){
                     [B]if (winningNumbers[i] == numberSelection[j][/B]){
                        winLossLabel.setText("Congratulations you won!");
                }
                     else [S]if (winningNumbers != numberSelection)[/S]{
                         winLossLabel.setText("Unlucky! Try again");
                    }
                }
            }
              
        }           
    

    Can someone explain why"Unlucky! Try again" is displayed even if numbers match.

    your code is basically asking if the arrays are the same object, which will always fail.


  • Advertisement
  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    How can i compare the numbers in each array?


  • Registered Users Posts: 2,501 ✭✭✭PabloAndRoy


    How can i compare the numbers in each array?

    walk through the arrays like you are doing and use booleans to manage the status of win/lose.


    Back in my day, we had to figure out our assignments without boards.ie to write code for us.


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    already figured out before you repplied


Advertisement