views:

275

answers:

3

I know that when creating buttons, like next and previous, that the code can be somewhat long to get those buttons to function.

My professor gave us this example to create the next 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();
    }
}

Though, I do not really understand how that short and simple if statement is what makes the next button function. I see that the fillTextFields(false) uses a boolean value and that you need to initialize that boolean value in the beginning of the code I believe. I had put private fillTextFields boolean = false; but this does not seem to be right...

I'm just hoping someone could explain it better. Thanks :)

+1  A: 

Well, fillTextFields(true); is a function call and when you pass in a true/false flag it does some things (you have to see the code inside the function in order to find out exactly what it does).

The field declaration private fillTextFields boolean = false; is invalid, you're supposed to provide the type before the name, e.g.: private boolean fillTextFields = false;. Aside from the invalid syntax that flag really doesn't do anything, especially if you're not using it anywhere.

I don't understand what else you expect to see in the jbtnNext_Click() method... when you declare your button and it gets clicked on the UI, then this method gets invoked. It doesn't make the button work, the button works even when you have nothing in the jbtnNext_Click() method. For example:

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

Getting a button to function depends on what you view as a functioning button. What is supposed to happen when you click next/previous?

Update:

I thought that I needed the boolean declaration to make the "fillTextFields(false)" work.

Was the fillTextFields method given to you somewhere? If it was, then you don't need to declare anything, much less a variable. If it's already provided, then you just call the method, that's all. If it's not provided then you need to declare it:

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

Otherwise what you see in that function is all you need to do in order to go to the next record in the database.

Lirik
By pressing the next button, I should be able to go ahead one record in a database. I realized I typed the syntax wrong here, sorry it was actually the correct way in my code. Though, I understand that the button always works regardless, but I thought that I needed the boolean declaration to make the "fillTextFields(false)" work...
Holly
+1  A: 

I think that the code provided is a bit short to provide a good explanation, posting the code for fillTextFields would be of more help.

What I can guess that the program is doing is that it is retrieving some data from a database. The next button allows the program to iterate through the items that have been returned.

Once that the next button is pressed, a message box is shown to let you "know" that the button has indeed been pressed.

rset.next returns true of there is another element in the list (retrieved from the database), or false if there isn't.

If it returns true, you are calling the fillTextFields methods, which I guess displays the data on screen (even though without the code I can just speculate). If there isn't anything left, a message box displaying "Not Found" is shown.

With regards to your question about

private fillTextFields boolean = false;

fillTextFields is a method, and you cannot assign values to methods. Also, in Java, when declaring both methods and variables, the type is written before the name, such as

private int number;
public float myMethod() { }
npinti
I actually do not have the code for the "fillTextFields". My professor gave us the above code as an example, but we have to incorporate that into our code to give us a step in the right direction of how to make this work, which means I will have to create/figure out the "fillTextFields" method myself. I have a database that I want to be able to navigate through via the GUI from the next/previous buttons.
Holly
A: 

The next button won't do anything unless you register an action with the button. What I mean is, wherever your next button is defined looks something like this:

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

This creates a button that has the label, 'Next'. There might be some additional code for positioning the button. In order for that button to do anything when it is clicked, it needs to have an Action set on it, or it has to have an ActionListener added to it. Many times, the class that is creating the button implements ActionListener and has a method to respond to the click, something like:

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

The actionPerformed method is called when the button is clicked, AS LONG AS you've registered the action listener on the button. Is anything like this present in the code from your professor?

Jeff