thanks to the helpful replies here, I've got my GUI the way I wanted it to be. --> How do I create the following GUI in Java Swing?
The problem I'm currently having is, how do I "interrupt" the execution of my code and tell my code to wait
That is the GUI and the this is the current code with an added ActionListener:
import java.awt.*;
import javax.swing.*;
public class GUITest extends JFrame {
JButton testButton1 = new JButton("Do something");
JButton testButton2 = new JButton("Do something different");
JButton testButton3 = new JButton("Do something even more different");
public GUITest() {
super("Testing Title");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(getHeader(),BorderLayout.NORTH);
pane.add(getTextArea(),BorderLayout.CENTER);
pane.add(getButtonPanel(),BorderLayout.SOUTH);
}
public JComponent getHeader() {
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(1,2));
labelPanel.setSize(getPreferredSize());
JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER);
JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER);
labelPanel.add(labelLocal);
labelPanel.add(labelDB);
return labelPanel;
}
public JComponent getTextArea() {
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(1,2,5,0));
JTextArea testTextArea = new JTextArea();
testTextArea.setEditable(false);
JScrollPane sp1 = new JScrollPane(testTextArea);
JTextArea testTextArea2 = new JTextArea();
JScrollPane sp2 = new JScrollPane(testTextArea2);
testTextArea2.setEditable(false);
testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni");
testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123");
textPanel.add(sp1);
textPanel.add(sp2);
return textPanel;
}
public JComponent getButtonPanel() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
testButton1.addActionListener(this);
testButton2.addActionListener(this);
testButton3.addActionListener(this);
buttonPanel.add(testButton1);
buttonPanel.add(testButton2);
buttonPanel.add(testButton3);
return buttonPanel;
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == this.testButton1){
System.out.println("testButton1!");
}
if(event.getSource() == this.testButton2){
System.out.println("testButton2!");
}
if(event.getSource() == this.testButton3){
System.out.println("testButton3!");
}
}
public static void main(String[] args) {
GUITest e = new GUITest();
e.setVisible(true);
e.setMinimumSize(new Dimension(650, 200));
e.pack();
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.setLocationRelativeTo(null);
}
}
I've read something about using JOptionPane but apparently that does not give me the flexiblity that I want, since I not only want buttons but two labels and text areas as well.
Basically, what I want to do is this:
//...
//other code that comes before
GUITest.openGUI();
if(button1 == pressed)
{
//do something and hide GUI
}
else if(button2 == pressed)
{
//do something else and hide GUI
}
//afterwards, there is more code, but it only gets executed after a button press
I want the execution to stop until I have chosen to press either Button1 or Button2. I don't want to do wait() since that would probably be bad performance wise.
Thanks for any help!