Search code examples
javaswingmultiple-instances

Pass data to desired window when multiple instances of same window are opened Java


Somebody please help me

I am working with one java application to make sale invoice. I have reproduced a working sample of part of my code hereunder. This application contains a main window with menu item and on clicking the menu item, a "Sale" window will open with a combo box, to select Item. On selecting Item will Open a Popup to select Price and which in turn opens another popup to select Qty. On selecting the Qty, the table in the sale Bill Window will be filled with Item,Price and Qty.

When multiple Sale Windows are opened data from Qty window are going to the last opened Sale window. Is there any way to pass data, to an older sale window, which is brought to front by mouse click?

import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

 public class Main extends JFrame {
    private static JPanel contentPane;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Main() {
        setTitle("Menu");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(500, 500, 500, 425);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setLayout(null);
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        JMenu mnSales = new JMenu("Sales");
        menuBar.add(mnSales);
        
        JMenuItem mntmProcessSale = new JMenuItem("Generate Sales Invoice");
        mntmProcessSale.addActionListener( e -> {
            Sale frame = new Sale(this);
            frame.setVisible(true);
        });
        mnSales.add(mntmProcessSale);
    }
    
    public static class Sale extends JDialog {
        private JComboBox combobox;
        private JLabel lbl;
        private static JTable table;

        public Sale(JFrame sale) {
            super(sale);
            setTitle("Sale");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setBounds(500, 500, 400, 400);
            setLocationRelativeTo(null);
            contentPane = new JPanel();
            contentPane.setLayout(null);
            setContentPane(contentPane);
            
            lbl=new JLabel("Select Item");
            lbl.setBounds(30, 30, 100, 20);
            contentPane.add (lbl);
          
            String [] item = {"A","B","C","D"};
            combobox=new JComboBox(item);
            combobox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(e.getStateChange() == ItemEvent.SELECTED) {
                        String s= (String) combobox.getSelectedItem();
                        Price.Item(s);
                        combobox.setSelectedIndex(-1);
                        new Price(sale).setVisible(true);
                    }
                }
            });
            combobox.setBounds(30, 50, 200, 20);
            combobox.setSelectedIndex(-1);
            contentPane.add (combobox);
            
            JScrollPane scrollPane= new JScrollPane();
            scrollPane.setBounds(20, 80, 250, 150);
            contentPane.add(scrollPane);
            
            table=new JTable();
            table.setModel(new DefaultTableModel(
                    new Object[][] {
                    },
                    new String[] {
                        "Item","Price", "Qty"
                    }
                ));
            scrollPane.setViewportView(table);
           
            
        }
        
        public static void filltable(Object[] row) {
        
            DefaultTableModel tm1 = (DefaultTableModel)table.getModel();
            tm1.addRow(row);
            System.out.println(row);
        }   
    }
    
    public static class Price extends JDialog {
        private JComboBox combobox;
        private JLabel lbl;
        private JPanel contentPane1;
        static String Item =null;

        public Price(JFrame price) {
            super(price);
            setTitle("Price");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setBounds(500, 500, 300, 300);
            contentPane1 = new JPanel();
            contentPane1.setLayout(null);
            setLocationRelativeTo(null);
            setContentPane(contentPane1);
            
            lbl=new JLabel("Select Price");
            lbl.setBounds(30, 30, 100, 20);
            contentPane1.add (lbl);
          
            String [] Price = {"5","10","20","30"};
            combobox=new JComboBox(Price);
            combobox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(e.getStateChange() == ItemEvent.SELECTED) {
                        String s= (String) combobox.getSelectedItem();
                        Qty.Price(Item,s);
                        combobox.setSelectedIndex(-1);
                        new Qty(price).setVisible(true);
                        dispose();
                    }
                }
            });
            combobox.setBounds(30, 50, 200, 20);
            combobox.setSelectedIndex(-1);
            contentPane1.add (combobox);   
        }
        public static void Item(String item) {
            Item=item;
        }
    }
    
    public static class Qty extends JDialog {
        private JComboBox combobox;
        private JLabel lbl;
        private JPanel contentPane2;
        static String Item =null;
        static String Price =null;

        public Qty(JFrame qty) {
            super(qty);
            setTitle("Qty");
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setBounds(500, 500, 300, 300);
            setLocationRelativeTo(null);
            contentPane2 = new JPanel();
            contentPane2.setLayout(null);
            setContentPane(contentPane2);
            
            lbl=new JLabel("Select Qty");
            lbl.setBounds(30, 30, 100, 20);
            contentPane2.add (lbl);
          
            String [] Qty = {"1","2","3","4"};
            combobox=new JComboBox(Qty);
            combobox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(e.getStateChange() == ItemEvent.SELECTED) {
                        
                        String qty= (String) combobox.getSelectedItem();
                        
                        Sale.filltable(new Object[] {Item,Price,qty});
                        
                        dispose();
                    }
                }
            });
            combobox.setBounds(30, 50, 200, 20);
            combobox.setSelectedIndex(-1);
            contentPane2.add (combobox);   
        }
        public static  void Price(String item, String price) {
            Item=item; Price=price;
        }
    }
    
    }

 

Solution

    • Every man and his dog names their class Main. I changed the name to OthrMain.
    • Don't create another Sale instance every time you click on the "Sales" menu item. I added a member variable named salesDlg to class OthrMain.
    • HIDE_ON_CLOSE is the default value for JDialog so no need to explicitly set it.
    • There is no need to explicitly set the JDialog content pane so don't do it.
    • Don't use static variables to pass data from one object to another. If you want to pass a value from the "Sales" JDialog to the "Price" JDialog then add a method to the "Price" JDialog that the "Sales" JDialog can call.
    • Don't use null layout. Use appropriate layout managers
    • The owner of each JDialog should be the JDialog that opened it. For example, the owner of the "Price" JDialog should be the "Sales" JDialog.

    I believe the following code does what you want.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    public class OthrMain extends JFrame {
        private Sale  saleDlg;
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        OthrMain frame = new OthrMain();
                        frame.setVisible(true);
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public OthrMain() {
            setTitle("Menu");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 425);
            setLocationByPlatform(true);
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);
            JMenu mnSales = new JMenu("Sales");
            menuBar.add(mnSales);
            JMenuItem mntmProcessSale = new JMenuItem("Generate Sales Invoice");
            mntmProcessSale.addActionListener(e -> {
                if (saleDlg == null) {
                    saleDlg = new Sale(this);
                }
                saleDlg.setVisible(true);
            });
            mnSales.add(mntmProcessSale);
        }
    
        public static class Sale extends JDialog {
            private JComboBox<String> combobox;
            private JLabel lbl;
            private final JTable table;
            private Price  priceDlg;
    
            public Sale(JFrame sale) {
                super(sale);
                setTitle("Sale");
                setSize(400, 400);
                setLocationRelativeTo(sale);
                JPanel topPanel = new JPanel();
                lbl = new JLabel("Select Item");
                topPanel.add(lbl);
    
                String[] item = {"A", "B", "C", "D"};
                combobox = new JComboBox<>(item);
                combobox.addItemListener(e -> {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        if (priceDlg == null) {
                            priceDlg = new Price(Sale.this);
                        }
                        priceDlg.setVisible(true);
                    }
                });
                combobox.setSelectedIndex(-1);
                topPanel.add(combobox);
                add(topPanel, BorderLayout.PAGE_START);
    
                JScrollPane scrollPane = new JScrollPane();
                add(scrollPane, BorderLayout.CENTER);
    
                table = new JTable();
                table.setModel(new DefaultTableModel(new Object[][]{},
                                                     new String[]{"Item", "Price", "Qty"}));
                scrollPane.setViewportView(table);
            }
    
            public void filltable(Object price, Object qty) {
                Object[] row = new Object[3];
                row[0] = combobox.getSelectedItem();
                combobox.setSelectedIndex(-1);
                row[1] = price;
                row[2] = qty;
                DefaultTableModel tm1 = (DefaultTableModel) table.getModel();
                tm1.addRow(row);
            }
        }
    
        public static class Price extends JDialog {
            private JComboBox<String> combobox;
            private JLabel lbl;
            private Qty  qtyDlg;
    
            public Price(JDialog price) {
                super(price);
                setTitle("Price");
                setSize(300, 300);
                setLocationRelativeTo(price);
                JPanel panel = new JPanel();
                lbl = new JLabel("Select Price");
                panel.add(lbl);
    
                String[] prices = {"5", "10", "20", "30"};
                combobox = new JComboBox<>(prices);
                combobox.addItemListener(new ItemListener() {
                    @Override
                    public void itemStateChanged(ItemEvent e) {
                        if (e.getStateChange() == ItemEvent.SELECTED) {
                            if (qtyDlg == null) {
                                qtyDlg = new Qty(Price.this);
                            }
                            qtyDlg.setVisible(true);
                        }
                    }
                });
                combobox.setSelectedIndex(-1);
                panel.add(combobox);
                add(panel, BorderLayout.PAGE_START);
            }
    
            public void clearPrice() {
                combobox.setSelectedIndex(-1);
            }
    
            public Object getPrice() {
                return combobox.getSelectedItem();
            }
        }
    
        public static class Qty extends JDialog {
            private JComboBox<String> combobox;
            private JLabel lbl;
    
            public Qty(JDialog qty) {
                super(qty);
                setTitle("Qty");
                setSize(300, 300);
                setLocationRelativeTo(qty);
                JPanel contentPane2 = new JPanel();
    
                lbl = new JLabel("Select Qty");
                contentPane2.add(lbl);
    
                String[] Qty = {"1", "2", "3", "4"};
                combobox = new JComboBox<>(Qty);
                combobox.addItemListener(this::fillTable);
                combobox.setSelectedIndex(-1);
                contentPane2.add(combobox);
                add(contentPane2);
            }
    
            public void fillTable(ItemEvent event) {
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    Object qty = combobox.getSelectedItem();
                    combobox.setSelectedIndex(-1);
                    Object price = ((Price) getOwner()).getPrice();
                    ((Price) getOwner()).clearPrice();
                    ((Sale) getOwner().getOwner()).filltable(price, qty);
                    setVisible(false);
                    getOwner().setVisible(false);
                }
            }
        }
    }