I created a GUI with a Jframe, JPanel, JLabel and a JButton.
// JFrame Jframe f = new JFrame(""); // JButton JButton b = new JButton("button1"); // JLabel JLabel l = new JLabel("panel label"); // JPanel JPanel p = new JPanel();
I added the button and the label to the panel. I two actionListeners for my button.
b.addActionListener(e -> { //code }); b.addActionListener(e -> { //code });
I want to execute my first action listener. Then execute the other one. Basically, I have some text that I want to output in my Jlabel, sequentially. I want for it to show “Hello” then “Goodbye” on the Panel. The problem that it is giving me, is that it only shows the text from my second ActionListener “Goodbye”.
Answer
You can convert second one to the below sample. The reason it only show the second one because both runs immediately and you see the last one as label.
b.addActionListener(new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { Thread t = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); // set your label as Goodbye here // add any other business logic } catch (InterruptedException e1) { e1.printStackTrace(); } } }); t.start(); } });