I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}
Related
My JTextField isn't showing on, only the paintComponent
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private JTextField txt;
public Painel(){
super();
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new FlowLayout());
txt = new JTextField();
txt.setBounds(400, 300, 50, 20);
}
You have to set the number of columns in your text field or give a default text to it. The following code should work for you. I have updated the previous answer to use the Gridbag layout. However still you need to set the number of columns in JTextField to render it.
public class TestFrame extends JFrame {
public static void main(String[] args) {
new TestFrame();
}
private TestFrame() throws HeadlessException {
super();
this.setLocationByPlatform(true);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 100, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JTextField textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}
Hope this helps. Happy coding !
I have 10 combo boxes and one button. Is there an easy way to make it so you set all the values of each combo box and then press this button and it stores all the values of the combo boxes?
You first have to iterate through all your JComboBox elements and get their selected state (ie: getSelectedItem() or getSelectedIndex()). Once you have determined what selection has been made, you can save the selections in a number of places to be read later, dependent on your needs:
You can save the states into a file that can be read at a later date
You can save the information in the local registry via Java Preferences API
If it is a network application, you can save the information in an SQL database
Sure! I'd propose to write yourself a new class which has both things:
class comboTrack{
private JComboBox box;
private Object value;
public comboTrack(JComboBox box){
this.box = box;
this.value = box.getSelectedItem();
}
public void update(){
this.value = box.getSelectedItem();
}
public Object getValue(){
return value;
}
}
Then, use these instead of normal Boxes. Like this:
//List of boxes (class variable):
ArrayList<comboTrack> boxes;
//Inside the button click method:
for(comboTrack box : boxes){
box.update();
}
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ComboBoxValues extends JFrame {
private JPanel contentPane;
JComboBox comboBox;
JComboBox comboBox_1;
JComboBox comboBox_2;
JComboBox comboBox_3;
JComboBox comboBox_4;
JComboBox comboBox_5;
JComboBox comboBox_6;
JComboBox comboBox_7;
JComboBox comboBox_8;
JComboBox comboBox_9;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboBoxValues frame = new ComboBoxValues();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ComboBoxValues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 0;
gbc_comboBox.gridy = 0;
contentPane.add(comboBox, gbc_comboBox);
comboBox.addItem("comboBox_0 val 1");
comboBox.addItem("comboBox_0 val 2");
comboBox_1 = new JComboBox();
GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
gbc_comboBox_1.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_1.gridx = 1;
gbc_comboBox_1.gridy = 0;
contentPane.add(comboBox_1, gbc_comboBox_1);
comboBox_1.addItem("comboBox_1 val 1");
comboBox_1.addItem("comboBox_1 val 2");
comboBox_2 = new JComboBox();
GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_2.gridx = 0;
gbc_comboBox_2.gridy = 1;
contentPane.add(comboBox_2, gbc_comboBox_2);
comboBox_2.addItem("comboBox_2 val 1");
comboBox_2.addItem("comboBox_2 val 2");
comboBox_3 = new JComboBox();
GridBagConstraints gbc_comboBox_3 = new GridBagConstraints();
gbc_comboBox_3.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_3.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_3.gridx = 1;
gbc_comboBox_3.gridy = 1;
contentPane.add(comboBox_3, gbc_comboBox_3);
comboBox_3.addItem("comboBox_3 val 1");
comboBox_3.addItem("comboBox_3 val 2");
comboBox_4 = new JComboBox();
GridBagConstraints gbc_comboBox_4 = new GridBagConstraints();
gbc_comboBox_4.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_4.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_4.gridx = 0;
gbc_comboBox_4.gridy = 2;
contentPane.add(comboBox_4, gbc_comboBox_4);
comboBox_4.addItem("comboBox_4 val 1");
comboBox_4.addItem("comboBox_4 val 2");
comboBox_5 = new JComboBox();
GridBagConstraints gbc_comboBox_5 = new GridBagConstraints();
gbc_comboBox_5.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_5.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_5.gridx = 1;
gbc_comboBox_5.gridy = 2;
contentPane.add(comboBox_5, gbc_comboBox_5);
comboBox_5.addItem("comboBox_5 val 1");
comboBox_5.addItem("comboBox_5 val 2");
comboBox_6 = new JComboBox();
GridBagConstraints gbc_comboBox_6 = new GridBagConstraints();
gbc_comboBox_6.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_6.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_6.gridx = 0;
gbc_comboBox_6.gridy = 3;
contentPane.add(comboBox_6, gbc_comboBox_6);
comboBox_6.addItem("comboBox_6 val 1");
comboBox_6.addItem("comboBox_6 val 2");
comboBox_7 = new JComboBox();
GridBagConstraints gbc_comboBox_7 = new GridBagConstraints();
gbc_comboBox_7.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_7.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_7.gridx = 1;
gbc_comboBox_7.gridy = 3;
contentPane.add(comboBox_7, gbc_comboBox_7);
comboBox_7.addItem("comboBox_7 val 1");
comboBox_7.addItem("comboBox_7 val 2");
comboBox_8 = new JComboBox();
GridBagConstraints gbc_comboBox_8 = new GridBagConstraints();
gbc_comboBox_8.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_8.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_8.gridx = 0;
gbc_comboBox_8.gridy = 4;
contentPane.add(comboBox_8, gbc_comboBox_8);
comboBox_8.addItem("comboBox_8 val 1");
comboBox_8.addItem("comboBox_8 val 2");
comboBox_9 = new JComboBox();
GridBagConstraints gbc_comboBox_9 = new GridBagConstraints();
gbc_comboBox_9.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_9.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_9.gridx = 1;
gbc_comboBox_9.gridy = 4;
contentPane.add(comboBox_9, gbc_comboBox_9);
comboBox_9.addItem("comboBox_9 val 1");
comboBox_9.addItem("comboBox_9 val 2");
JButton btnGetValues = new JButton("Get Values");
GridBagConstraints gbc_btnGetValues = new GridBagConstraints();
gbc_btnGetValues.insets = new Insets(0, 0, 5, 0);
gbc_btnGetValues.gridx = 1;
gbc_btnGetValues.gridy = 5;
contentPane.add(btnGetValues, gbc_btnGetValues);
textArea = new JTextArea();
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridheight = 2;
gbc_lblNewLabel.gridwidth = 2;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 6;
contentPane.add(textArea, gbc_lblNewLabel);
textArea.setColumns(30);
textArea.setRows(5);
pack();
btnGetValues.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText(comboBox.getSelectedItem().toString()+","+
comboBox_1.getSelectedItem().toString()+","+
comboBox_2.getSelectedItem().toString()+"\n"+
comboBox_3.getSelectedItem().toString()+","+
comboBox_4.getSelectedItem().toString()+","+
comboBox_5.getSelectedItem().toString()+"\n"+
comboBox_6.getSelectedItem().toString()+","+
comboBox_7.getSelectedItem().toString()+","+
comboBox_8.getSelectedItem().toString()+"\n"+
comboBox_9.getSelectedItem().toString());
}
});
}
}
I am trying to create a layout where I have two panels with borders around them. I would like the rectangular borders to be of equal size. I would also like the JTextField in the bottom panel to be of that smaller width.
The problem is that, whenever I fill the bottom panel horizontally (using GridBagConstraints.HORIZONTAL), the checkbox and label inside that panel become misaligned with the checkbox and label in the panel above it.
I was hoping an experienced developer could offer insight into how I could make the two JPanels and their rectangular borders equal in size with one another, while also having their checkboxes and labels aligned.
Here is what it looks like now:
Here is code to reproduce the problem:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Example
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
MyFrame frame = new MyFrame();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
class MyFrame
{
private JFrame frame;
private MyDialog dialog;
public MyFrame()
{
frame = new JFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
dialog = new MyDialog(frame);
}
}
class MyDialog
{
private JDialog dialog;
private JPanel mainPanel,
panel1,
inputPanel1,
panel2,
inputPanel2;
private JLabel titleLabel,
label1,
label2;
private JCheckBox checkBox1,
checkBox2;
private JTextField textField1,
textField2;
public MyDialog(JFrame frame)
{
dialog = new JDialog(frame, true);
buildPanel();
dialog.add(mainPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void buildPanel()
{
mainPanel = new JPanel(new GridBagLayout());
titleLabel = new JLabel("Title");
checkBox1 = new JCheckBox("checkBox1");
label1 = new JLabel("label1:");
textField1 = new JTextField(15);
inputPanel1 = new JPanel(new GridBagLayout());
inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel1.add(label1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel1.add(textField1, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1 = new JPanel(new GridBagLayout());
Border border1 = BorderFactory.createEtchedBorder();
panel1.setBorder(border1);
panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
checkBox2 = new JCheckBox("checkBox2");
label2 = new JLabel("label2:");
textField2 = new JTextField(8);
inputPanel2 = new JPanel(new GridBagLayout());
inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2 = new JPanel(new GridBagLayout());
Border border2 = BorderFactory.createEtchedBorder();
panel2.setBorder(border2);
panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
// When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
//mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));
}
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
gbc.fill = fill;
return gbc;
}
}
What I did was
Make your inputPanelX have FlowLayout with FlowLayout.LEADING instead of using GirdBagLayout for them.
inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
And the scond one I just made the same Dimension as the first one
inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
Dimension dim = new Dimension(inputPanel1.getPreferredSize());
public Dimension getPreferredSize(){
return new Dimension(dim);
}
};
That's all I changed. See full code below
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class Example
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
MyFrame frame = new MyFrame();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
class MyFrame
{
private JFrame frame;
private MyDialog dialog;
public MyFrame()
{
frame = new JFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
dialog = new MyDialog(frame);
}
}
class MyDialog
{
private JDialog dialog;
private JPanel mainPanel,
panel1,
inputPanel1,
panel2,
inputPanel2;
private JLabel titleLabel,
label1,
label2;
private JCheckBox checkBox1,
checkBox2;
private JTextField textField1,
textField2;
public MyDialog(JFrame frame)
{
dialog = new JDialog(frame, true);
buildPanel();
dialog.add(mainPanel);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void buildPanel()
{
mainPanel = new JPanel(new GridBagLayout());
titleLabel = new JLabel("Title");
checkBox1 = new JCheckBox("checkBox1");
label1 = new JLabel("label1:");
textField1 = new JTextField(15);
inputPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
inputPanel1.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel1.add(label1);
inputPanel1.add(textField1);
panel1 = new JPanel(new GridBagLayout());
Border border1 = BorderFactory.createEtchedBorder();
panel1.setBorder(border1);
panel1.add(checkBox1, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel1.add(inputPanel1, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
checkBox2 = new JCheckBox("checkBox2");
label2 = new JLabel("label2:");
textField2 = new JTextField(8);
inputPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING)){
Dimension dim = new Dimension(inputPanel1.getPreferredSize());
public Dimension getPreferredSize(){
return new Dimension(dim);
}
};
inputPanel2.setBorder(BorderFactory.createEmptyBorder(0, 17, 0, 0)); // indent the label and textfield
inputPanel2.add(label2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
inputPanel2.add(textField2, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2 = new JPanel(new GridBagLayout());
Border border2 = BorderFactory.createEtchedBorder();
panel2.setBorder(border2);
panel2.add(checkBox2, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
panel2.add(inputPanel2, getConstraints(0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(titleLabel, getConstraints(0, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE));
mainPanel.add(panel1, getConstraints(0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE));
// When I fill panel2 horizontally, the checkbox becomes misaligned with the above checkbox:
//mainPanel.add(panel2, getConstraints(0, 2, 2, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL));
}
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
gbc.fill = fill;
return gbc;
}
}
How can I position my labels with this code? It seems that gridbaglayout is not working here, especially the gridbagconstraints. Even if I change the gridx and gridy values, the labels are not moving.
I need to make it in like 3 even columns, 1 column = 1 panel
package nest;
import javax.swing.*;
import java.awt.*;
public class nested extends JFrame{
public static void main(String[] args) {
JFrame f=new JFrame("Bio Data");
JPanel p1=new JPanel(new GridBagLayout());
JPanel p2=new JPanel(new GridBagLayout());
//JPanel p3=new JPanel(new GridBagLayout());
GridBagConstraints c1=new GridBagConstraints();
GridBagConstraints c2=new GridBagConstraints();
JLabel l1=new JLabel("aa");
JLabel l2=new JLabel("bb");
c1.gridx=1;
c1.gridy=1;
p1.add(l1, c1);
c2.gridx=4;
c2.gridy=4;
p2.add(l2, c2);
f.add(p1);
f.add(p2);
f.setVisible(true);
f.setSize(800,500);
f.setResizable(false);
f.setLayout(new FlowLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Changing gridx and gridy does not change the actual position of the labels, but rather the order/format that the labels will be displayed in. If you have something in gridx 4 but nothing in gridx 1,2 or 3 then that object will be the first column.
Also, it woulld be very odd to put a new panel in each column since you can put things in different rows of the gridBagLayout. Here is a short example of a gridBagLayout inside a flowLayout with three different columns of labels:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
public class nest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
nest frame = new nest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public nest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(0, 1, 0, 0));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JPanel panel_1 = new JPanel();
panel.add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0, 27, 0};
gbl_panel_1.rowHeights = new int[]{0, 16, 0};
gbl_panel_1.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel lblNewLabel_1 = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 0;
panel_1.add(lblNewLabel_1, gbc_lblNewLabel_1);
JLabel lblNewLabel = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 0;
panel_1.add(lblNewLabel, gbc_lblNewLabel);
JLabel lblNewLabel_2 = new JLabel("New label");
GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel_2.gridx = 2;
gbc_lblNewLabel_2.gridy = 0;
panel_1.add(lblNewLabel_2, gbc_lblNewLabel_2);
JLabel lblLabel = new JLabel("New label");
GridBagConstraints gbc_lblLabel = new GridBagConstraints();
gbc_lblLabel.gridx = 2;
gbc_lblLabel.gridy = 1;
panel_1.add(lblLabel, gbc_lblLabel);
}
}
I created this with the Google WindowBuilder. Note that you don't necessarily need to set the columnWidths, rowHeights, columnWeights, or rowWeights (and it is better not to sometimes when you have dynamic content). Hopefully this helps.
MAIN PROBLEM: In a JScrollPane with JPanel which contains a JTextArea, text wraps up if GUI is expanded but text does not wrap back when GUI is contracted. See example below
Okay I am building the GUI for an app I am currently working on and I am having a bit of a problem.
The explanation: My GUI is structured as illustrated below:
And this is what it looks like.
Upon expansion the the JTextArea inside the panelWithText expands and resizes the text as such:
But the problem is what happens when you make the GUI smaller. The "problem" is that I want the text to warp back as it was before. I did a little experimenting by implementing a ComponentListener to both the JScrollPane and the panelWithText and found out that componentResized is being called for panelWithText upon expansion but not for contraction. Is there any way to implement the behavior of the text warping back in the panelWithText Component?
PS: Apparently if I switch the JScrollPane with a regular JPanel it works. But I can't do that! I have a LOT of panelWithText to show to the user.
PS PS: Sorry here is the code I am using.
JFrameExt.java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.Window.Type;
import javax.swing.ScrollPaneConstants;
import java.awt.CardLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
public class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
panel.add(panelWithText_, gbc_panelWithText_);
panelWithText panelWithText__1 = new panelWithText();
GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText__1.gridx = 0;
gbc_panelWithText__1.gridy = 1;
panel.add(panelWithText__1, gbc_panelWithText__1);
panelWithText panelWithText__2 = new panelWithText();
GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
gbc_panelWithText__2.gridx = 0;
gbc_panelWithText__2.gridy = 2;
panel.add(panelWithText__2, gbc_panelWithText__2);
panelWithText panelWithText__3 = new panelWithText();
GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
gbc_panelWithText__3.gridx = 0;
gbc_panelWithText__3.gridy = 3;
panel.add(panelWithText__3, gbc_panelWithText__3);
panelWithText panelWithText__4 = new panelWithText();
GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
gbc_panelWithText__4.gridx = 0;
gbc_panelWithText__4.gridy = 4;
panel.add(panelWithText__4, gbc_panelWithText__4);
panelWithText panelWithText__5 = new panelWithText();
GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
gbc_panelWithText__5.gridx = 0;
gbc_panelWithText__5.gridy = 5;
panel.add(panelWithText__5, gbc_panelWithText__5);
panelWithText panelWithText__6 = new panelWithText();
GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
gbc_panelWithText__6.gridx = 0;
gbc_panelWithText__6.gridy = 6;
panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300,100);
}
}
panelWithText.java
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
public class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d){
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
After a little playing around, I came up with this...
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameExt frame = new JFrameExt();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static class ScrollablePane extends JPanel implements Scrollable {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, 100);
}
#Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 64;
}
#Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
#Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
#Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
public static class JFrameExt extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public JFrameExt() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 246, 164);
contentPane = new JPanel();
contentPane.setBorder(null);
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(null);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new ScrollablePane();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
// gbl_panel.columnWidths = new int[]{0, 0};
// gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
// gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
// gbl_panel.rowWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelWithText panelWithText_ = new panelWithText();
GridBagConstraints gbc_panelWithText_ = new GridBagConstraints();
gbc_panelWithText_.anchor = GridBagConstraints.NORTH;
gbc_panelWithText_.insets = new Insets(0, 0, 5, 0);
gbc_panelWithText_.fill = GridBagConstraints.HORIZONTAL;
gbc_panelWithText_.gridx = 0;
gbc_panelWithText_.gridy = 0;
gbc_panelWithText_.weightx = 1;
panel.add(panelWithText_, gbc_panelWithText_);
// panelWithText panelWithText__1 = new panelWithText();
// GridBagConstraints gbc_panelWithText__1 = new GridBagConstraints();
// gbc_panelWithText__1.insets = new Insets(0, 0, 5, 0);
// gbc_panelWithText__1.anchor = GridBagConstraints.NORTH;
//// gbc_panelWithText__1.fill = GridBagConstraints.HORIZONTAL;
// gbc_panelWithText__1.gridx = 0;
// gbc_panelWithText__1.gridy = 1;
// panel.add(panelWithText__1, gbc_panelWithText__1);
//
// panelWithText panelWithText__2 = new panelWithText();
// GridBagConstraints gbc_panelWithText__2 = new GridBagConstraints();
// gbc_panelWithText__2.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__2.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__2.gridx = 0;
// gbc_panelWithText__2.gridy = 2;
// panel.add(panelWithText__2, gbc_panelWithText__2);
//
// panelWithText panelWithText__3 = new panelWithText();
// GridBagConstraints gbc_panelWithText__3 = new GridBagConstraints();
// gbc_panelWithText__3.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__3.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__3.gridx = 0;
// gbc_panelWithText__3.gridy = 3;
// panel.add(panelWithText__3, gbc_panelWithText__3);
//
// panelWithText panelWithText__4 = new panelWithText();
// GridBagConstraints gbc_panelWithText__4 = new GridBagConstraints();
// gbc_panelWithText__4.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__4.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__4.gridx = 0;
// gbc_panelWithText__4.gridy = 4;
// panel.add(panelWithText__4, gbc_panelWithText__4);
//
// panelWithText panelWithText__5 = new panelWithText();
// GridBagConstraints gbc_panelWithText__5 = new GridBagConstraints();
// gbc_panelWithText__5.insets = new Insets(0, 0, 5, 0);
//// gbc_panelWithText__5.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__5.gridx = 0;
// gbc_panelWithText__5.gridy = 5;
// panel.add(panelWithText__5, gbc_panelWithText__5);
//
// panelWithText panelWithText__6 = new panelWithText();
// GridBagConstraints gbc_panelWithText__6 = new GridBagConstraints();
//// gbc_panelWithText__6.fill = GridBagConstraints.BOTH;
// gbc_panelWithText__6.gridx = 0;
// gbc_panelWithText__6.gridy = 6;
// panel.add(panelWithText__6, gbc_panelWithText__6);
setSize(300, 100);
}
}
public static class panelWithText extends JPanel {
/**
* Create the panel.
*/
public void me_resized(Dimension d) {
System.out.println("CALLED..");
super.setPreferredSize(d);
}
public panelWithText() {
setBackground(Color.DARK_GRAY);
setForeground(Color.WHITE);
setLayout(new BorderLayout(0, 0));
JTextArea txtrIveBeenReading = new JTextArea();
txtrIveBeenReading.setEditable(false);
txtrIveBeenReading.setColumns(28);
txtrIveBeenReading.setFont(new Font("Tahoma", Font.PLAIN, 10));
txtrIveBeenReading.setLineWrap(true);
txtrIveBeenReading.setWrapStyleWord(true);
txtrIveBeenReading.setText("\n A bunch of really important text here... A bunch of really important text here... A bunch of really important text here... A bunch of really important text here...\n");
txtrIveBeenReading.setForeground(Color.WHITE);
txtrIveBeenReading.setBackground(Color.DARK_GRAY);
add(txtrIveBeenReading, BorderLayout.CENTER);
}
}
}
Basically, you need a container that implements the Scrollable interface. This will allow you to specify that the container should track/match the view ports width. This will cause the container to be laid out when ever the view port changes size...
As a side note, you can use a single copy of the GridBagConstraints, when you add each new component to the container, the GridBagLayout will generate a copy of it's own. This is very powerful when you want to share properties of the GridBagConstraints between components ;)