Hey guys. I am new to Java programming and I am having a lot of difficulty figuring out how to get the proper functionality out of my first, last, next, and previous buttons. First, I was wondering if my button setup is correct:

  // helper method that creates panel for buttons
  private JPanel createButtonPanel() // returns a Jpanel
  {
        ActionListener btnListen = new ButtonListener(); // create my listerner

        // Create button objects
        firstButton = new JButton("First"); // create button object
        firstButton.setActionCommand("First"); // add actionCommand
        firstButton.addActionListener(btnListen); // add Listener command

        nextButton = new JButton("Next"); // create button object
        nextButton.setActionCommand("Next"); // add actionCommand
        nextButton.addActionListener(btnListen); // add Listener command


        previousButton = new JButton("Previous"); // create button object
        previousButton.setActionCommand("Previous"); // add actionCommand
        previousButton.addActionListener(btnListen); // add Listener command


        lastButton = new JButton("Last"); // create button object
        lastButton.setActionCommand("Last"); // add actionCommand
        lastButton.addActionListener(btnListen); // add Listener command


        // Create panel object

        JPanel panel = new JPanel(); // instantiate a new JPanel

        panel.add(firstButton); // add firt button to panel
        panel.add(nextButton);   // add next button to panel
        panel.add(previousButton); // add previous button to panel
        panel.add(lastButton);   // add last button to panel

        return panel; // return the panel

  }

If so, I am sure that there needs to be embedded if statments that perform the looping for the next and previous buttons. The first and last buttons just need to be assigned to the first and last object in my array, so that is not a problem.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Option A) Create general action listener for all of them

// helper method that creates panel for buttons
private JPanel createButtonPanel() // returns a Jpanel
{
    // Create button objects
    firstButton = new JButton("First"); // create button object
    firstButton.setActionCommand("First"); // add actionCommand
    firstButton.addActionListener(this); // add Listener command

    nextButton = new JButton("Next"); // create button object
    nextButton.setActionCommand("Next"); // add actionCommand
    nextButton.addActionListener(this); // add Listener command


    previousButton = new JButton("Previous"); // create button object
    previousButton.setActionCommand("Previous"); // add actionCommand
    previousButton.addActionListener(this); // add Listener command


    lastButton = new JButton("Last"); // create button object
    lastButton.setActionCommand("Last"); // add actionCommand
    lastButton.addActionListener(this); // add Listener command


    // Create panel object

    JPanel panel = new JPanel(); // instantiate a new JPanel

    panel.add(firstButton); // add firt button to panel
    panel.add(nextButton); // add next button to panel
    panel.add(previousButton); // add previous button to panel
    panel.add(lastButton); // add last button to panel

    return panel; // return the panel

}

public void actionPerformed(ActionEvent e) {
    if ("First".equals(e.getActionCommand())) {
        //Action for first button here
    }
    else if ("Next".equals(e.getActionCommand())) {
        //Action for next button here
    }
    else if ("Previous".equals(e.getActionCommand())) {
        //Action for previous button here
    }
    else if ("Last".equals(e.getActionCommand())) {
        //Action for last button here
    }
[/code]

Option B) Provide each button with its own listener
[code=Java]
// helper method that creates panel for buttons
private JPanel createButtonPanel() // returns a Jpanel
{
    // Create button objects
    firstButton = new JButton("First"); // create button object
    firstButton.setActionCommand("First"); // add actionCommand
    firstButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            //Action for first button here
        }
    });

    nextButton = new JButton("Next"); // create button object
    nextButton.setActionCommand("Next"); // add actionCommand
    nextButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            //Action for next button here
        }
    });

//REST OF CODE

Option A positive - shorter code
Option A negative - as actionPerformed can be used by numerous components it may become later on difficult to maintain and easier to create errors and confusion

Option B positive - easier to maintain as you actionPerformed is associated with only specific button
Option B negative - code is longer

PS: In the future if you post code,use code tags.

Edited 3 Years Ago by happygeek: fixed formatting

Hey peter_budo thanks. Sorry for the improper formatting. New to site. Yeah I have the listener coded similar to the first example that you have. The problem has been properly coding the action itself for the next and previous buttons. As I had said, the first and last buttons were easy.

// nested ButtonListener class
       class ButtonListener implements ActionListener // ActionListerner allows the use of the button events such as the button click event.
      {
             public void actionPerformed(ActionEvent e)
            {
                   //add button functions
                   if (e.getActionCommand() == "First") // First is the caption (label) on my button. If the button I click is First, the following statements will execute.
                  {
                        itemTypeField.setText(dvd[0].getItemType()); // get the item of the first item in my array and display it in the item type text box.
                        itemNumberField.setText(String.valueOf(dvd[0].getItemNumber())); // get the item number of the first item in my array and display it in the item number text box.
                        priceField.setText(String.valueOf(dvd[0].getPrice())); // get the price of the first dvd in my array and display it in the price text box.
                        itemQuantityField.setText(String.valueOf(dvd[0].getItemQuantity())); // get the item quantity of my first dvd in my DVD array object and assign it to the item quntity field.
                        itemValueField.setText(String.valueOf(dvd[0].getItemValue())); // get the item value of my first dvd in my DVD array object and assign it to the item value field.
                        inventoryValueField.setText(String.valueOf(dvd[0].getItemQuantity() * dvd[0].getItemValue())); // get the value of the total inventory.
                        playlengthField.setText(String.valueOf(dvd[0].getPlaylength())); // get the play length of the first dvd in my DVD array object and assign it to the play length field.
                        restockingfeeField.setText("5%"); // quote the dvd restocking fee as 5%.
                        adjustedValueField.setText(String.valueOf(dvd[0].getAdjustedValue())); // get the adjusted value of my first dvd in my DVD array object and assign it to the adjusted value field.

                  } // end if

                    if (e.getActionCommand() == "Previous") // Previous is the caption (label) on my button. If the button I click is Previous, the following statements will execute.
                    {
                        

                        itemTypeField.setText(dvd[1].getItemType()); // get the item of the first item in my array and display it in the item type text box.
                        itemNumberField.setText(String.valueOf(dvd[1].getItemNumber())); // get the item number of the first item in my array and display it in the item number text box.
                        priceField.setText(String.valueOf(dvd[1].getPrice())); // get the price of the first dvd in my array and display it in the price text box.
                        itemQuantityField.setText(String.valueOf(dvd[1].getItemQuantity())); // get the item quantity of my first dvd in my DVD array object and assign it to the item quntity field.
                        itemValueField.setText(String.valueOf(dvd[1].getItemValue())); // get the item value of my first dvd in my DVD array object and assign it to the item value field.
                        inventoryValueField.setText(String.valueOf(dvd[1].getItemQuantity() * dvd[1].getItemValue())); // get the value of the total inventory.
                        playlengthField.setText(String.valueOf(dvd[1].getPlaylength())); // get the play length of the first dvd in my DVD array object and assign it to the play length field.
                        restockingfeeField.setText("5%"); // quote the dvd restocking fee as 5%.
                        adjustedValueField.setText(String.valueOf(dvd[1].getAdjustedValue())); // get the adjusted value of my first dvd in my DVD array object and assign it to the adjusted value field.
                        
                  } // end if

                    if (e.getActionCommand() == "Next") // Next is the caption (label) on my button. If the button I click is Next, the following statements will execute.
                  {
                        itemTypeField.setText(dvd[2].getItemType()); // get the item of the first item in my array and display it in the item type text box.
                        itemNumberField.setText(String.valueOf(dvd[2].getItemNumber())); // get the item number of the first item in my array and display it in the item number text box.
                        priceField.setText(String.valueOf(dvd[2].getPrice())); // get the price of the first dvd in my array and display it in the price text box.
                        itemQuantityField.setText(String.valueOf(dvd[2].getItemQuantity())); // get the item quantity of my first dvd in my DVD array object and assign it to the item quntity field.
                        itemValueField.setText(String.valueOf(dvd[2].getItemValue())); // get the item value of my first dvd in my DVD array object and assign it to the item value field.
                        inventoryValueField.setText(String.valueOf(dvd[2].getItemQuantity() * dvd[2].getItemValue())); // get the value of the total inventory.
                        playlengthField.setText(String.valueOf(dvd[2].getPlaylength())); // get the play length of the first dvd in my DVD array object and assign it to the play length field.
                        restockingfeeField.setText("5%"); // quote the dvd restocking fee as 5%.
                        adjustedValueField.setText(String.valueOf(dvd[2].getAdjustedValue())); // get the adjusted value of my first dvd in my DVD array object and assign it to the adjusted value field.

                  } // end if




                  // I did not implement code for the Previous and Next button. If I had, I would do so here.
                  if (e.getActionCommand() == "Last") // Last is the caption (label) on my button. If the button I click is labeled Last, the following statements will execute.
                  
                        itemTypeField.setText(dvd[3].getItemType()); // get the item of the first item in my array and display it in the item type text box.
                        itemNumberField.setText(String.valueOf(dvd[3].getItemNumber())); // get the item number of the first item in my array and display it in the item number text box.
                        priceField.setText(String.valueOf(dvd[3].getPrice())); // get the price of the first dvd in my array and display it in the price text box.
                        itemQuantityField.setText(String.valueOf(dvd[3].getItemQuantity())); // get the item quantity of my first dvd in my DVD array object and assign it to the item quntity field.
                        itemValueField.setText(String.valueOf(dvd[3].getItemValue())); // get the item value of my first dvd in my DVD array object and assign it to the item value field.
                        inventoryValueField.setText(String.valueOf(dvd[3].getItemQuantity() * dvd[3].getItemValue())); // get the value of the total inventory.
                        playlengthField.setText(String.valueOf(dvd[3].getPlaylength())); // get the play length of the first dvd in my DVD array object and assign it to the play length field.
                        restockingfeeField.setText("5%"); // quote the dvd restocking fee as 5%.
                        adjustedValueField.setText(String.valueOf(dvd[3].getAdjustedValue())); // get the adjusted value of my first dvd in my DVD array object and assign it to the adjusted value field.

            } // end if
      } // end ButtonListener
 } // end FrameHelper Class

Now I am aware that the coding for the next and previous button is incorrect here, but my question is how would I properly code the if/else statements to get the buttons to perform properly?

In the above example, would it be best to set the Next and Previous buttons coding to loop through a two dimensional array using the ItemNumber as the integer? That way it would simply be a matter of counting +1 or -1 wouldn't it?

terence - I apologize for not having comments on your code, but I just don't have time to wade through it right now. But keep in mind that comments should be used for clarifying the code when it is necessary to do so, or for explaining what a line (or group of lines) of code are doing when it is not immediately obvious, not to explain exactly what the syntax of every line of code is doing.

Also, I'd help out now but you never mentioned what the first, last, and next buttons are supposed to do. What do you want them to do? What are they doing right now? Do you know what lines of code (in the code tags) are incorrect? Do you know of any problem areas or can you not figure out what is wrong? Etc.

Oh, I just edited as you posted. Since you're in here right now, if you tell me what you want your buttons to do (what their purpose is), I'll help you out.

Basically the only problem I am having is the functionality of the Next and Previous buttons right now. I need them to react accordingly depending on which DVD is currently being displayed.

You need a variable to keep track of currently selected product so you can effectively loop through whatever Collection class you use. Then once button pressed it will read current index position, depending on selected action you should check if requested move is possible and adjust current index position and as last call method to refresh GUI fields.

PS: I'm still half sleep, been just woken by phone :yawn:

So basically do something like set dvd to a variable. Then use a counter to step 1 forward or backward to the proper dvd?

Yes, in doing so you basically remove the repeating code from all button action and have only one that does whole job with only need of integer as parameter.

I thinnk I see where you are going peter. Thanks so much. I will give this a try and let you guys know the results.

This article has been dead for over six months. Start a new discussion instead.