The easiest way to create java next / previous buttons

I know that when creating buttons, like the next and previous, the code may be somewhat long to make these buttons function.

My professor gave us this example to create the following button:

    private void jbtnNext_Click() {
        JOptionPane.showMessageDialog(null, "Next" ,"Button Pressed", 
            JOptionPane.INFORMATION_MESSAGE);        

    try {
        if (rset.next()) {
            fillTextFields(false);
        }else{
            //Display result in a dialog box
            JOptionPane.showMessageDialog(null, "Not found");
    }
}
    catch (SQLException ex) {
        ex.printStackTrace();
    }
}

Although, I really don't understand how this short and simple if statement is what the next button function does. I see that it is fillTextFields(false)using a boolean and you need to initialize that boolean at the beginning of the code that I am counting. I put it private fillTextFields boolean = false;, but that doesn't seem right ...

I just hope someone can explain this better. Thank:)

+3
source share
3 answers

, fillTextFields(true); - , true/false, ( , , ).

private fillTextFields boolean = false; , , : private boolean fillTextFields = false;. , , .

, jbtnNext_Click()... , , . , , jbtnNext_Click() . :

private void jbtnNext_Click() {
    // The button will still work, but it simply won't do anything
}

, . /?

:

, , "fillTextFields (false)" .

fillTextFields -? , , . , , . , :

private void fillTextFields(bool shouldFill)
{
    if(shouldFill)
    {
        // fill the text fields
    }
    // possibly have an else statement if you need to do something else here
}

, , .

+1

, , , fillTextFields .

, , , . .

, .

rset.next true, ( ) false, .

true, fillTextFields, , , ( , ). , "Not Found".

private fillTextFields boolean = false;

fillTextFields - , . , Java ,

private int number;
public float myMethod() { }
+1

, . , , , :

private JButton nextButton = new JButton("Next");

This creates a button that says "Next." Perhaps there is an additional code for positioning the button. In order for this button to be able to do something when it was pressed, an action must be set on it, or an ActionListener must be added to it. Many times, the class creating the button implements the ActionListener and has a click response method, something like:

nextButton.addActionListener(this);
...
...
...
public void actionPerformed(ActionEvent e) {
    // some method implementation
}

The actionPerformed method is called when the button is pressed, AS LONG AS you registered the action listener on the button. Is something like this in your professor's code?

0
source

Source: https://habr.com/ru/post/1745517/


All Articles