OldMcDonald OldMcDonald - 5 months ago 12
Java Question

How do I create the following GUI in Java Swing?

I want to create the following GUI with Java Swing.

GUI I want to have

Since I'm not experienced enough with Java Swing, I'm not sure how to exactly recreate that GUI.

I've tried using GridLayout which looks like this:

GridLayout

I've tried other LayoutManagers but due to my inexperience, I couldn't get anything even remotely resembling the GUI I want to achieve.

I probably have to use GridBagLayout but I've tried it and simply wasn't able to get anything done.
I'm not sure how to exactly use GridBagLayout, especially since there is a variance of the amount of colums needed (2, 2 and then 3).

Here is the code used for creating the second GUI:

import java.awt.*;
import javax.swing.*;

public class GUITest extends JFrame {

public GUITest() {
super("Testing Title");
Container pane = getContentPane();

pane.setLayout(new GridLayout(3,1));

pane.add(getHeader());
pane.add(getTextArea());
pane.add(getButtonPanel());

}

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 inner = new JPanel();
inner.setLayout(new FlowLayout((FlowLayout.CENTER),0,100));
inner.add(new JButton("Do something"));
inner.add(new JButton("Do something different"));
inner.add(new JButton("Do something even more different"));
return inner;
}

public static void main(String[] args) {
GUITest e = new GUITest();
e.setSize(700, 500);
e.setVisible(true);
e.setResizable(false);
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.setLocationRelativeTo(null);
}
}


I'm thankful for any kind of support!

Answer

Here is your code with just some little changes :)

     import java.awt.*;  
     import javax.swing.*;  

     public class GUITest extends JFrame {

         public GUITest() {

              super("Testing Title");  
              Container pane = getContentPane();  
              pane.setLayout(new BorderLayout());//Modified Layout to BorderLayout  
              pane.add(getHeader(),BorderLayout.NORTH); //BorderLayout.NORTH
              pane.add(getTextArea(),BorderLayout.CENTER);//BorderLayout.CENTER
              pane.add(getButtonPanel(),BorderLayout.SOUTH);//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 inner = new JPanel();
          inner.setLayout(new FlowLayout());//Modified to standard FlowLayout  
          inner.add(new JButton("Do something"));  
          inner.add(new JButton("Do something different"));  
          inner.add(new JButton("Do something even more different"));  
          return inner;  

     }

     public static void main(String[] args) {  

          GUITest e = new GUITest();  
          e.pack(); //Modified setSize(700,500) to pack()  
          e.setVisible(true);  
          e.setResizable(false);  
          e.setDefaultCloseOperation(EXIT_ON_CLOSE);  
          e.setLocationRelativeTo(null);  
     }  
}  
Comments