0

I'm working on an EPOS system, and as part of the program, I am generating a GridLayout of all items that are stored in an ArrayList. Within the items ArrayList, all the objects are stored with their necessary member variables, such as name, barcode and price. I have currently made it so that the grid is populated, however the buttons are action-less, and I am quite unsure how to handle the data from the classes, is there perhaps some way to assign the values of the current item object that is being iterated over onto the button being made? As each button is made by my code, and is not "hand made". The relevant code is as below:

public class gridCreator extends JFrame {
    ObjectCreator obj = new ObjectCreator();
    GridLayout itemGrid = new GridLayout();
    JFrame frame = new JFrame("pls work");
    static gridCreator instance;

public static void main(String[] args) throws FileNotFoundException {
    instance = new gridCreator();
    instance.createGrids();
    instance.createAndShowGUI();
}
public void createGrids() throws FileNotFoundException{
    obj.loadItems();
    itemGrid.setColumns(20);
    itemGrid.setRows(4);
    for (ObjectCreator.Item item : obj.items){
        addComponentsToPane(item);
    }
}
private void addComponentsToPane(ObjectCreator.Item item) {
    JButton button = new JButton(item.getName());
    frame.add(button);
}

As a side note, the ObjectCreator class is where the objects themselves are made.

1 Answer 1

3

You can make the class implement ActionListener, and assign all the buttons that actionListener:

public class gridCreator extends JFrame implements ActionListener{
    ObjectCreator obj = new ObjectCreator();
    GridLayout itemGrid = new GridLayout();
    JFrame frame = new JFrame("pls work");
    static gridCreator instance;

public static void main(String[] args) throws FileNotFoundException {
    instance = new gridCreator();
    instance.createGrids();
    instance.createAndShowGUI();
}
public void createGrids() throws FileNotFoundException{
    obj.loadItems();
    itemGrid.setColumns(20);
    itemGrid.setRows(4);
    for (ObjectCreator.Item item : obj.items){
        addComponentsToPane(item);
    }
}
private void addComponentsToPane(ObjectCreator.Item item) {
    JButton button = new JButton(item.getName());
    frame.add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
    //do actions here   
}

Then, in the action listener of the JFrame, you can make a case specific action.

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

2 Comments

Many thanks, this is very helpful, however I am still unsure how to make sure that each button will be able to access the values of the class? I have attempted to map each button to the current object that is iterated over but I am unsure of where to go from there?
@TooLateTheHero - I'm not sure I quite understand

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.