2

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 waitenter image description here

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!

OldMcDonald
  • 594
  • 13
  • 36
  • 2
    You have to change your way of thinking. GUI-Applications work Event-Driven. That is: In principle it is "waiting" constantly. A pressed button will raise an event for which you can register a handler. That handler's code will be executed as soon as the respective event occurrs. – Fildor Jul 04 '16 at 12:50

1 Answers1

3

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.

Then you will want to read a bit more about JOptionPane, since it can certainly display all these animals and more. Understand that the Object parameter of the JOptionPane.showXxx(...) method can take any Swing component as its value. This means that the JOptionPane can accept and display a JPanel that is filled with multiple components including other JPanels, that in fact can contain an entire complex GUI. For example, please look at this link.

The other option is to create your own JDialog, make it modal and display your halting GUI within this; either would work.


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

Here you may be mistaken in how event-driven programs work. There's no need to "wait" for anything here, but rather you react to events, such as button pushes, when they occur. The key is to use your listeners to react to button pushes, and alter the behavior of this reaction depending on the state of your program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for the reply. You say that JOptionPane can accept any JPanel. So it would be possible to put the GUI I already have into a JOptionPane? And if yes, which one do I use? showOptionDialog? – OldMcDonald Jul 05 '16 at 08:55
  • Also, for your edit: I've maybe worded it incorrectly but there is certaintly a need in "waiting". Basically I want the program (which does something else) to display a small GUI / Dialog and let the user choose what he wants to do and according to that, the program progresses further. It waits for user input. – OldMcDonald Jul 05 '16 at 09:08
  • JDialog waits for user input, which is exactly what I want. How do I "convert" the above mentioned code into a JDialog which looks exactly the same? Thanks. – OldMcDonald Jul 05 '16 at 09:22
  • @OldMcDonald: I avoid creating classes that extend top level windows such as JFrame or JDialog, since that paints you into a corner. Instead have the class extend or create a JPanel, get all JFrame code out of it, and then put it into a JOptionPane, or create a JDialog on the fly and place it into that. – Hovercraft Full Of Eels Jul 05 '16 at 14:48