Reputation: 14978
I am following this tutorial and setting up my GUI. What I want a single textarea
should occupy entire width (all columns) but it is not happening and screen like this:
This Cyan field should capture the width of entire screen. Below is my code:
Code
package com.company.app;
import javax.swing.*;
import java.awt.*;
public class SpamGUI {
public static void main(String[] args) {
System.out.println("Loading Program..");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Evil App");
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
//panel.setLayout(new GridLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
/**
* Training File Section Path
*/
JLabel lblTrainPath = new JLabel("Enter Training Folder Path");
lblTrainPath.setSize(100, 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTrainPath, c);
JTextField txtTrainPath = new JTextField();
txtTrainPath.setSize(100, 20);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTrainPath, c);
JButton btnTrainPath = new JButton("Browse..");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 3;
panel.add(btnTrainPath, c);
/**
* Testing File Section Path
*/
JLabel lblTestingPath = new JLabel("Enter Testing Folder Path");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTestingPath, c);
JTextField txtTestPath = new JTextField();
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTestPath, c);
JButton btnTestPath = new JButton("Browse..");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 2;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(btnTestPath, c);
/**
* Pogress Section
*/
JTextArea txtProgress = new JTextArea();
txtProgress.setBackground(Color.cyan);
txtProgress.setOpaque(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.ipadx = 50;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 2;
panel.add(txtProgress, c);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 63
Reputation: 168815
gridx
/gridy
orders were reversed.gridwidth
was randomly set to 3 too early... That's all I can remember changing.
import javax.swing.*;
import java.awt.*;
public class SpamGUI {
public static void main(String[] args) {
System.out.println("Loading Program..");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Evil App");
//frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
//panel.setLayout(new GridLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
/**
* Training File Section Path
*/
JLabel lblTrainPath = new JLabel("Enter Training Folder Path");
//lblTrainPath.setSize(100, 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTrainPath, c);
JTextField txtTrainPath = new JTextField(10);
//txtTrainPath.setSize(100, 20);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTrainPath, c);
JButton btnTrainPath = new JButton("Browse..");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.5;
//c.gridwidth = 3;
panel.add(btnTrainPath, c);
/**
* Testing File Section Path
*/
JLabel lblTestingPath = new JLabel("Enter Testing Folder Path");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(lblTestingPath, c);
JTextField txtTestPath = new JTextField(10);
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 1;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(txtTestPath, c);
JButton btnTestPath = new JButton("Browse..");
c.fill = GridBagConstraints.FIRST_LINE_END;
c.gridx = 2;
c.gridy = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(btnTestPath, c);
/**
* Pogress Section
*/
JTextArea txtProgress = new JTextArea(5,15);
txtProgress.setBackground(Color.cyan);
txtProgress.setOpaque(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.ipadx = 50;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridwidth = 3;
panel.add(txtProgress, c);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
That GUI is badly in need of some white space. Change that something like changing:
c.fill = GridBagConstraints.HORIZONTAL;
To:
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5,5,5,5);
Upvotes: 1