Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:37:28+00:00 2026-05-31T13:37:28+00:00

So I have a MainFrame class which has a JTable in it, listing all

  • 0

So I have a MainFrame class which has a JTable in it, listing all Products stored in DB. The JButton with the help of listeners will open AddProduct (another class, and another window/frame) in which I can add product in the DB. Unfortunately, I’m not exactly sure how can I update/revalidate JTable in MainFrame once AddProduct adds new product and autocloses.
Could some please give me some idea as how can I easily resolve this?

Since program is rather large, here are relevant parts of it:
From MainFrame.java

public JPanel tabProducts() {
    JPanel panel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"));

    /** Labels **/
    JLabel label = new JLabel("List of all available products");

    /** Buttons **/
    JButton add = new JButton("Add product");
    add.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new AddProduct();
        }
    });
    JButton update = new JButton("Update product");
    update.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new UpdateProduct(ps.getProductByID(15));
        }
    });

    /** TABLE: Products **/
    String[] tableTitle = new String[] {"ID", "Name", "Type", "Price", "In stock"};
    String[][] tableData = null;
    DefaultTableModel model = new DefaultTableModel(tableData, tableTitle);
    JTable table = null;
    /** Disable editing of the cell **/
    table = new JTable(model){
        public boolean isCellEditable(int r, int c) {
            return false;
        }
    };
    /** Load the products from DB **/
    List<Product> listInv = ps.getProductsByAtt(new ArrayList<String>());
    for (int i = 0; i < listInv.size(); i++) {
        model.insertRow(i, new Object[] {
                listInv.get(i).getID(),
                listInv.get(i).getName(),
                listInv.get(i).getType(),
                listInv.get(i).getPrice(),
                listInv.get(i).getQuantity()
        });
    }
    /** Add scroll pane **/
    JScrollPane scrollpane = new JScrollPane(table);

    /** Add everything to the panel **/
    panel.add(label, "wrap, span");
    panel.add(scrollpane, "wrap, span");
    panel.add(add);
    panel.add(update);

    return panel;
}

And AddProduct.java

public class AddProduct {

    private JFrame frame;
    private JButton add, cancel;
    private JRadioButton food, beverage;
    private JTextField name, price, quantity;
    private IProductService ps = new ProductService();
    private ButtonGroup group = new ButtonGroup();
    private Product p;
    private String type = "";

    public AddProduct() {
        /** Frame options **/
        frame = new JFrame("Add new product");
        frame.setSize(400, 280);
        frame.setMinimumSize(new Dimension(400, 280));
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        /** Default panel **/
        final JPanel panel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"));

        /** Radio Buttons to choose between the food and the beverages **/
        food = new JRadioButton("Food");
        beverage = new JRadioButton("Beverage");
        group.add(food);
        group.add(beverage);
        food.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                type = "Food";
                frame.validate();
            }
        });
        beverage.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                type = "Beverage";
                frame.validate();
            }
        });

        /** Add everything to the panel **/
        panel.add(new JLabel("Product ID"));
        panel.add(new JLabel(Integer.toString(ps.getProductNr()+1)), "wrap, span 2");
        panel.add(new JLabel("Name"));
        panel.add(name = new JTextField(""), "wrap, span 2");
        panel.add(new JLabel("Type"));
        panel.add(food);
        panel.add(beverage, "wrap");
        panel.add(new JLabel("Price"));
        panel.add(price = new JTextField(""), "wrap, span 2");
        panel.add(new JLabel("Quantity"));
        panel.add(quantity = new JTextField(""), "wrap, span 2");


        /** Button: ADD **/
        add = new JButton("Add product");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if ( !type.equals("Food") && !type.equals("Beverage")) {
                    JOptionPane.showMessageDialog(panel, "Please choose the type of this product.");
                } else if (name.getText().equals("")) {
                    JOptionPane.showMessageDialog(panel, "Please type a name for this product.");
                } else if (price.getText().equals("")) {
                    JOptionPane.showMessageDialog(panel, "Please enter the price for this product.");
                } else if (quantity.getText().equals("")) {
                    JOptionPane.showMessageDialog(panel, "Please enter the available amount of this product in stock.");
                } else {
                    try {
                        p = new Product(ps.getProductNr()+1, name.getText(), type, Double.parseDouble(price.getText()), Integer.parseInt(quantity.getText()));
                        if (ps.addProduct(p)) {
                            JOptionPane.showMessageDialog(panel, "Product successfully added!");
                            frame.validate();
                            frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
                        }
                    } catch (Exception ex) {
                        addFinalError();
                    }
                }
            }
        });

        /** Button: CANCEL **/
        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
            }
        });

        /** Add buttons to the panel **/
        panel.add(cancel);
        panel.add(add, "span 2");

        /** Add panel to frame and make it visible **/
        frame.add(panel);
        frame.setVisible(true);

    }

    /**
     * In case more then one error is encountered
     */
    private void addFinalError(){
        JOptionPane.showMessageDialog(frame, "An error occured while adding the product. Please make sure the following is correct:\n\n" +
                " Name  : Can contain letters and numbers\n" +
                " Price  : Must be a number\n" +
                " Quantity  : Must be a whole number\n");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:37:28+00:00Added an answer on May 31, 2026 at 1:37 pm

    maybe a static Method in the AddProduct class that returns the created Product will solve your problem. Take a look at the JOptionPane API for example static String showInputDialog(Object message)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called MainGame, which is defined like this in my .h:
We have a Japanese client that has source code in COBOL on an mainframe.
I have an application which uses Hibernate for database transactions, I have mapped all
I'm having small problem (I guess) of showing the JTable panel. I have class
I've created a frame (mainframe) for my program in the main class which I
I have created a class which extends JDialog, where I have some checkboxes and
I have a JFrame which holds and JScrollPane. The JScrollPane itself holds a class
Hi I work with netbeans. I have written a code which has two classes
I have a SDI MFC application which CMainFrame class is derived from CFrameWndEx. The
I have a class MainFrame (subclass of wxFrame ) Now this class does most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.