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.
JFramecontent pane uses BorderLayout by default. By repeatedly usingframe.add(bX);you add multiple components toBorderLayoutcenter position, which can take only one component. For testing try settingframe.setLayout(( new GridLayout(0,1)));(and remove the while loop of course)