0

I'm a CS undergrad, and I'm working on a group project for class. We're building an inventory/register system for a store. Essentially, there is a main screen, where the user will see the current transaction and its items, as well as buttons to see/search the store's inventory, or add a new item to the inventory.

Of course, this is a group project at college, so the work has been shoddy and haphazard. It's basically been one guy so far with me helping out, but now I'm attempting to take his work and organize it.

Here's his code for the main screen (the data is just dummy info he wanted to test with):

` public GUI() implements ActionListener {

    frame = new JFrame();
    inventory = new JButton("Inventory");
    additem = new JButton("Add Item");



    inventory.setPreferredSize(new Dimension(100,100));
    additem.setPreferredSize(new Dimension(100,100));
    inventory.setAlignmentX(50);
    inventory.setAlignmentY(50);
    inventory.addActionListener(this);
    additem.addActionListener(this);

    String[] column = {"Qty.", "Item", "Price"};
    String[][] data = {{"1", "brewski", "1.08"}, {"1", "water", "10.73"}, {"2", "doritos", "6.75"}, {"1", "Guitar", "1000"}, {"2", "hat", "40"}, {"1", "lighter", "1.75"}};
    ItemList = new JTable(data, column);

    ItemList.setPreferredScrollableViewportSize(new Dimension(1680, 1280));
    ItemList.setFillsViewportHeight(false);

    jps = new JScrollPane(ItemList);


    panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(175, 250, 200,250));

    panel.add(inventory);
    panel.add(additem);
    panel.add(jps);

    frame.add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Presley Register v1.0");
    frame.pack();
    frame.setVisible(true);
}



public static void main(String[] args) {


}

public void actionPerformed(ActionEvent e) { //performs action when inventory button is clicked
    if(e.getSource()==inventory) {
        JFrame f = new JFrame("Inventory");

        try {
            f.add(new SearchInJtable());
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        f.setSize(800, 200);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
    else if(e.getSource()==additem){
        try {
            new NewItem();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

`

Some things I noticed I wanted to fix:

  • The class implements actionListener. I think it would be better practice to use anonymous listeners for each button, though I'm not sure.
  • Obviously the data is dummy, but I wasn't sure where this would actually be written. It's the model, so it should be separated from the view, but with how this is written, I'm not sure how to even go about that.
  • The two classes created from the actionListener, NewItem and SearchInJtable are custom classes he wrote that contain the functionalities of searching inventory or adding a new item, each in a separate Frame.
  • The code just isn't modular at all

I know this is a very vague question, but I wanted to at least reach out and ask. Thanks!

  • 1
    *"I think it would be better practice to use anonymous listeners for each button"* - Yes and no. If you can, you're better of using the [`Action`s API](https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html), as it's self configurable and re-usable, but anonymous listeners are definitely better then implementing a class level `ActionListener` – MadProgrammer Apr 02 '22 at 23:31
  • *"but I wasn't sure where this would actually be written. It's the model, so it should be separated from the view"* - a number of options. First, focus on "dependency injection" and make use of `interface`s, use observer patterns to monitor changes to the model and delegation for managing the business logic. Remember, you're not stuck to a single model. – MadProgrammer Apr 02 '22 at 23:33
  • 2
    *"The code just isn't modular at all*" - This is part of working through the decoupling process. Make use `interface`s to describe the high level contracts, try and think about how changing one of the system will effect other parts (could you change data stores, to local/remote SQL, a web service, flat files (JSON/XML)?). Avoid extending from `JFrame`. Base your UI of `JPanel`s, these can then be added to any container you want. A navigation system is a hard think to implement, but I would be focusing on using some kind of dedicated controller – MadProgrammer Apr 02 '22 at 23:37
  • Remember, Swing is already based on MVC (you don't tend to see the controller, but each control tends to have one). Breaking a UI down into a MVC is a balancing act, but to my mind, the controller doesn't care "how" the UI is implemented, it only cares that the agreed upon contract is upheld. Also, a view could act as controller for another view. The point of MVC is to decouple responsibility as well as provide for a more modular solution - the model doesn't care how the controller or view is implemented, the controller doesn't care about the view or model and the same for the view – MadProgrammer Apr 02 '22 at 23:41
  • For [example](https://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274); [example](https://stackoverflow.com/questions/31576623/how-mvc-work-with-java-swing-gui/31576899#31576899); [example](https://stackoverflow.com/questions/27663306/open-a-jpanel-after-pressing-a-button-in-a-jframe/27663749#27663749); [example](https://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) – MadProgrammer Apr 02 '22 at 23:45
  • [example](https://stackoverflow.com/questions/35025678/how-to-update-the-graphics/35028914#35028914); [example](https://stackoverflow.com/questions/35032048/jtextfield-input-fails-to-update-output-in-textview-in-mvc/35033580#35033580); [example](https://stackoverflow.com/questions/29571722/java-application-with-multiple-scenes/29639672#29639672); [example](https://stackoverflow.com/questions/35782887/cant-make-jcombobox-update-jframe/35783598#35783598) – MadProgrammer Apr 02 '22 at 23:46
  • Now if you really want a head ache, you could have a look at [SimpleSwingNavigationController](https://github.com/RustyKnight/SimpleSwingNavigationController) and [SwingNavigationControllerPane](https://github.com/RustyKnight/SwingNavigationControllerPane) (this is just showing off ) – MadProgrammer Apr 02 '22 at 23:48
  • Oh, and you can "filter" a `JTable` directly, for [example](https://stackoverflow.com/questions/26031383/how-to-filter-jtable-with-respect-to-a-specific-column/26031896#26031896) and see [Sorting and Filtering](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting) for more details – MadProgrammer Apr 03 '22 at 00:10

0 Answers0