Home > Net >  how to use do while loop inside the GUI?
how to use do while loop inside the GUI?

Time:02-24

I'm new to java and I just want to ask if it is possible to perform the do...while loop inside the GUI. I got errors and it seems like I can't find a way to fix it in my current knowledge. How to stop the do while loop when the JFrame.EXIT_ON_CLOSE is triggered?

    private static JFrame frame = new JFrame();
private static JPanel panel = new JPanel();
private static JLabel label = new JLabel();
private static JTextField txtf = new JTextField();
private static JComboBox box = new JComboBox();
private static JButton button = new JButton();
private static double amount, itemprice;
private static String itemnametxt;
private static String itemlist;
//private static int items;


//ArrayList <String> itemlist = new ArrayList<String>(); 


public static void main(String[] args) {
    new test();
    {
        do {
        
        new JFrame();
        frame.setSize(800,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        
        new JPanel();
        frame.add(panel);
        panel.setLayout(null);
        
        JLabel myorders = new JLabel("MY ORDERS");
        myorders.setBounds(300, 20, 350, 25);
        myorders.setFont(new Font ("Serif" , Font.PLAIN, 35));
        //frame.add(label);
        //myorders.setSize(350,350);
        panel.add(myorders);
        
        JLabel total = new JLabel();
        total.setText("TOTAL:");
        total.setFont(new Font ("Serif" , Font.PLAIN , 15));
        total.setBounds(470,60,70,25);
        panel.add(total);
        
        JLabel itemprice = new JLabel();
        itemprice.setText("ITEM NAME"); // NOTE: list here!!!
        itemprice.setFont(new Font ("Serif" , Font.PLAIN, 20));
        //itemprice.getText(itemnametxt);
        itemprice.setBounds(90,90,350,25);

        panel.add(itemprice);
        
        JLabel price = new JLabel();
        price.setText("PRICE");
        price.setFont(new Font ("Serif" , Font.PLAIN , 20));
        price.setBounds(320,90,350,25);
        panel.add(price);
        
        JLabel operation = new JLabel();
        operation.setText("OPERATION");
        operation.setFont(new Font ("Serif", Font.PLAIN , 20));
        operation.setBounds(550,90,350,25);
        panel.add(operation);
        
        
        
        
        
        //===================================
        JLabel itemnametxt = new JLabel("Enter Item Name:");
        itemnametxt.setBounds(550, 120, 350, 25);
        panel.add(itemnametxt);
        
        JTextField itemnamefield = new JTextField();
        //itemnamefield.setText("");
        itemnamefield.setBounds(550, 140, 150, 25);
        panel.add(itemnamefield);
        String x = itemnamefield.getText();
        
        
        //-----------------------------------
        JLabel itempricetxt = new JLabel("Enter Item Price:");
        itempricetxt.setBounds(550, 180, 350, 25);
        panel.add(itempricetxt);
        
        JTextField itempricefield = new JTextField();
        //itemnamefield.setText("");
        itempricefield.setBounds(550, 200, 150, 25);
        panel.add(itempricefield);
        
        //===================================
        
        JButton additem = new JButton("ADD NEW ITEM");
        //additem.setText("ADD NEW ITEM");
        additem.setFont(new Font ("Serif" , Font.PLAIN , 15));
        additem.setBounds(550, 235, 150, 25);
        additem.setVisible(true);
        additem.addActionListener((ActionListener) itemlist);
        panel.add(additem);
        
        //***********************************************//
        
        JLabel itemselector = new JLabel();
        itemselector.setText("Select Item:");
        itemselector.setBounds(550,350,150,25);
        panel.add(itemselector);
        
        JComboBox combobox = new JComboBox();
        combobox.setBounds(550, 380, 170, 25);
        panel.add(combobox);
        
        JButton delete = new JButton("DELETE ITEM");
        //additem.setText("ADD NEW ITEM");
        delete.setFont(new Font ("Serif" , Font.PLAIN , 15));
        delete.setBounds(550, 415, 170, 25);
        delete.setVisible(true);
        delete.addActionListener(null);
        panel.add(delete);
        
        frame.setVisible(true);
            }
while(JFrame.EXIT_ON_CLOSE);
    }       
}

}

in the while part it got errors and i know why but i can't find any ways to stop or close the do while loop when the frame gets closed.

CodePudding user response:

  1. JFrame.EXIT_ON_CLOSE is a constant integer that can be passed as the argument of JFrame.setDefaultCloseOperation(), you can't use it as the condition of a do-while loop.
  2. If you want to exit the loop when the JFrame is closed, you can use the following code:
class Main {
    private static volatile boolean isFrameClosed = false;
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                // This will be executed when the JFrame is closed.
                isFrameClosed = true;
                System.out.println("JFrame closed.");
            }
        });
        frame.setVisible(true);
        do {
            // do something
        } while(!isFrameClosed);
    }
}
  1. Please note that you are repeatedly creating JFrames and components in the loop, which is probably undesirable.

CodePudding user response:

If you want to prevent the main thread from exiting, you don't need to. Your app won't exit unless you have UI.

In Java, the condition in while must be of boolean type, JFrame.EXIT_ON_CLOSE is an int therefore it can't be used in the while.

Using JFrame.EXIT_ON_CLOSE as the close operation means the handler for the close button of the JFrame will call System.exit(0) to terminate your application.

If you need to synchronize your main thread or another thread to an action in the GUI, consider using CountDownLatch.


Swing is not thread-safe, you must create and access GUI components on the Event Dispatch Thread only:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createUI());
    }

    private static void createUI() {
        JFrame frame = new JFrame();
        // Other code
        frame.setVisible(true);
    }

This program will continue to run until the frame is disposed of.

  • Related