0

I’m a new Java programmer and I have some code in which I’m trying to improve UI by adding buttons. Basically, right now, the code takes an array list and allows the user to do many things with it. The user may add items to the array list, remove items from the array list, set an item at a certain index in the array list, print the items in the array list, and get the item at a certain index at the array. Pretty much it allows the user to interact with the array list in some basic ways using basic array list functions (.add, .remove, etc.)

Right now, the user has to enter a number in order to do one of these things (i.e. enter 1 to print the elements in the array), but I’m trying to make it so the functions will work with buttons. I've been looking up how buttons and Action Listeners work for the past hour or so and I thought I had a good grasp on it, but I have run into a problem. I run my code and my buttons will not show up, so I am confused as to why this is happening.

Here is the most important part of my code:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test                                                                                                                                                                                                                                                                                                                                     
{
    public static void main (String arg[]) throws IOException {

        // * Main Variable Declaration (with any Initialization)
        //
        ArrayList<String> List = new ArrayList<String>();        
        boolean            continueThisApp = true;       
        int                userInput_MenuChoice;                    
        Scanner            scanner = new Scanner(System.in);                 

        // * Main Code
        //        
        while( continueThisApp )
        {
            // * Menu of Choices
            //
            JFrame frame = new JFrame();
            JButton b1 = new JButton();
            JButton b2 = new JButton();
            JButton b3 = new JButton();
            JButton b4 = new JButton();
            JButton b5 = new JButton();
            JButton b6 = new JButton();           
            b1.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    print(items_ObsInArrLst);             
                }
            });
            b2.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    add(items_ObsInArrLst);             
                }
            });
            b3.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    remove(items_ObsInArrLst);             
                }
            });
            b4.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    get(items_ObsInArrLst);             
                }
            });
            b5.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    set(items_ObsInArrLst);             
                }
            });
            b6.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) {
                    System.out.println("*** Thanks for Trying our App. :)");
                }
            });
            frame.setSize(1000, 1000);
            //b1
            b1.setVisible(true);
            b1.setText("Print elements");
            frame.setLayout(new FlowLayout());
            frame.add(b1);
            //b2
            b2.setVisible(true);
            b2.setText("Add elements");
            frame.add(b2);
            //b3
            b3.setVisible(true);
            b3.setText("Remove elements");
            frame.add(b3);
            //b4
            b4.setVisible(true);
            b4.setText("Get elements");
            frame.add(b4);
            //b5
            b5.setVisible(true);
            b5.setText("Set elements");
            frame.add(b5);
            //b6
            b6.setVisible(true);
            b6.setText("Quit");
            frame.add(b6);

            // NOTE THAT THIS PART (THE OLD WAY WE DID IT) IS COMMENTED OUT
            /*if( userInput_MenuChoice == 1 ){
                print_Items_ObsInArrLst_Mth(items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 2 ){
                scannerInputTo_AddLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 3 ){
                scannerInputTo_RemoveLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 4 ){
                scannerInputTo_GetLst_Mth (items_ObsInArrLst);             
            }            
            else if( userInput_MenuChoice == 5 ){
                scannerInputTo_SetLst_Mth (items_ObsInArrLst);             
            }
            else if( userInput_MenuChoice == 9 ){
                continueThisApp_Bool = false;
                System.out.println("*** Thanks for Trying our App. :)");
            }
            else{
                System.out.println("*** Invalid Menu Choice. Retry.");
            }  */

        }  
    }

The functions inside the action listeners work as they should, but the buttons themselves don't appear for me. I am asking if anyone knows why the buttons are not appearing. I'm sure there's other problems with my code, but I can probably fix those if I know why the buttons aren't appearing. If this helps, I am currently using BlueJ.

tl;dr - I implemented buttons into my code but they are not appearing when I run the code.

3
  • You shouldn't be making buttons or any GUI elements within a while loop... Commented Dec 9, 2018 at 14:54
  • You might want to focus on a single button and action at a time. That'll help us and you work out a minimal reproducible example Commented Dec 9, 2018 at 14:56
  • JFrame content pane uses BorderLayout by default. By repeatedly using frame.add(bX); you add multiple components to BorderLayout center position, which can take only one component. For testing try setting frame.setLayout(( new GridLayout(0,1))); (and remove the while loop of course) Commented Dec 9, 2018 at 15:09

1 Answer 1

1

Right now, the user has to enter a number in order to do one of these things (i.e. enter 1 to print the elements in the array), but I’m trying to make it so the functions will work with buttons.

A GUI app works different that a text based app. For one thing you don't use a while loop to listen for user input.

Basically, right now, the code takes an array list

In a GUI app you also don't work directly with ArrayLists to display the data. Instead you use a Swing component and that component will use a Model to hold the data. So the "model" replaces the ArrayList. That is all add/removes are done to the model.

So I would suggest you start over and redesign the structure of your app from the beginning to work with a GUI, instead of trying to fit your existing code into the GUI structure.

Start by reading the section from the Swing tutorial on How to Use Lists. The ListDemo example shows you everything you need to create a GUI that uses buttons to add/remove items from the ListModel of the JList.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.