Issue
I am designing a window with GridBagLayout
. This principal panel is a scroll panel. It is composed by several panels. All of them are GridBagLayout
. In one of the panels, there are two scroll panels containing a text field.
The problem
When writing in one of these text fields the size of it stays unchanged and appears in the scroll bar. But, when I stop writing, the scroll bar disappears and the text field becomes bigger. That makes the adjacent cells bigger as well.
In the code, the scroll panes are scrollPane_1
and scrollPane_2
:
public class PruebaQ extends JPanel {
public class SignalingMessagePane extends JPanel {
// Components
private JCheckBox singMessChckBx1;
private JLabel familyLabel1;
private JComboBox<ComboItem> familyCmbBx1;
private JTextArea textTField1;
public SignalingMessagePane() {
super();
}
public void initialize() {
this.setBorder(new LineBorder(Color.LIGHT_GRAY));
GridBagLayout gbl_nst5Panel1 = new GridBagLayout();
gbl_nst5Panel1.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
gbl_nst5Panel1.rowHeights = new int[] { 0, 0, 0 };
gbl_nst5Panel1.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
gbl_nst5Panel1.rowWeights = new double[] { 0.0, 0.0, 0.0 };
this.setLayout(gbl_nst5Panel1);
singMessChckBx1 = new JCheckBox("");
singMessChckBx1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
familyCmbBx1.setEnabled(singMessChckBx1.isSelected());
textTField1.setEnabled(singMessChckBx1.isSelected());
}
});
GridBagConstraints gbc_singMessChckBx1 = new GridBagConstraints();
gbc_singMessChckBx1.anchor = GridBagConstraints.NORTHWEST;
gbc_singMessChckBx1.insets = new Insets(5, 5, 0, 0);
gbc_singMessChckBx1.gridx = 0;
gbc_singMessChckBx1.gridy = 0;
this.add(singMessChckBx1, gbc_singMessChckBx1);
familyLabel1 = new JLabel("Label");
GridBagConstraints gbc_familyLabel1 = new GridBagConstraints();
gbc_familyLabel1.insets = new Insets(0, 40, 0, 5);
gbc_familyLabel1.anchor = GridBagConstraints.EAST;
gbc_familyLabel1.gridx = 0;
gbc_familyLabel1.gridy = 1;
this.add(familyLabel1, gbc_familyLabel1);
familyCmbBx1 = new JComboBox();
GridBagConstraints gbc_familyCmbBx1 = new GridBagConstraints();
gbc_familyCmbBx1.weightx = 0.6;
gbc_familyCmbBx1.insets = new Insets(0, 0, 0, 5);
gbc_familyCmbBx1.fill = GridBagConstraints.HORIZONTAL;
gbc_familyCmbBx1.gridx = 1;
gbc_familyCmbBx1.gridy = 1;
this.add(familyCmbBx1, gbc_familyCmbBx1);
familyCmbBx1.setEnabled(false);
textTField1 = new JTextArea();
GridBagConstraints gbc_textTField1 = new GridBagConstraints();
gbc_textTField1.weighty = 1.0;
gbc_textTField1.weightx = 1.0;
gbc_textTField1.gridwidth = 3;
gbc_textTField1.insets = new Insets(10, 5, 5, 0);
gbc_textTField1.fill = GridBagConstraints.BOTH;
gbc_textTField1.gridx = 0;
gbc_textTField1.gridy = 2;
this.add(textTField1, gbc_textTField1);
textTField1.setEnabled(false);
}
}
private JScrollPane scrollPane_2;
private JTextArea txtrDetectionZone;
private JScrollPane scrollPane_1;
private JTextArea txtrRelevanceZone;
private JPanel panel;
private JPanel nestedPanel2;
private JPanel nst2RefPositionPanel;
private JScrollPane scrollPane;
private JLabel latitudeLabel;
private JTextField latitudeTxt;
private JLabel longitudeLabel;
private JTextField longitudeTxt;
private JLabel zoomLabel;
private JTextField zoomTxt;
private JPanel nestedPanel3;
private JLabel detectionZoneLabel;
private JLabel relevanceZoneLabel;
private JPanel nestedPanel5;
private SignalingMessagePane[] nstPanel;
/**
* Create the panel.
*/
public PruebaQ() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{0};
gridBagLayout.columnWeights = new double[]{1.0};
gridBagLayout.rowWeights = new double[]{1.0};
setLayout(gridBagLayout);
panel = new JPanel();
scrollPane = new JScrollPane(panel);
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.weighty = 1.0;
gbc_scrollPane.weightx = 1.0;
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
add(scrollPane, gbc_scrollPane);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0};
gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
nestedPanel2 = new JPanel();
GridBagConstraints gbc_nestedPanel2 = new GridBagConstraints();
gbc_nestedPanel2.weighty = 1.0;
gbc_nestedPanel2.weightx = 1.0;
gbc_nestedPanel2.fill = GridBagConstraints.BOTH;
gbc_nestedPanel2.insets = new Insets(20, 10, 10, 10);
gbc_nestedPanel2.gridx = 1;
gbc_nestedPanel2.gridy = 1;
panel.add(nestedPanel2, gbc_nestedPanel2);
GridBagLayout gbl_nestedPanel2 = new GridBagLayout();
gbl_nestedPanel2.columnWidths = new int[]{0};
gbl_nestedPanel2.rowHeights = new int[]{0, 0};
gbl_nestedPanel2.columnWeights = new double[]{1.0};
gbl_nestedPanel2.rowWeights = new double[]{0.0, 1.0};
nestedPanel2.setLayout(gbl_nestedPanel2);
nst2RefPositionPanel = new JPanel();
nst2RefPositionPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Position", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
GridBagConstraints gbc_nst2RefPositionPanel = new GridBagConstraints();
gbc_nst2RefPositionPanel.weighty = 1.0;
gbc_nst2RefPositionPanel.weightx = 1.0;
gbc_nst2RefPositionPanel.insets = new Insets(0, 30, 0, 30);
gbc_nst2RefPositionPanel.fill = GridBagConstraints.BOTH;
gbc_nst2RefPositionPanel.gridx = 0;
gbc_nst2RefPositionPanel.gridy = 1;
nestedPanel2.add(nst2RefPositionPanel, gbc_nst2RefPositionPanel);
GridBagLayout gbl_nst2RefPositionPanel = new GridBagLayout();
gbl_nst2RefPositionPanel.columnWidths = new int[]{0, 0};
gbl_nst2RefPositionPanel.rowHeights = new int[]{0, 0, 0};
gbl_nst2RefPositionPanel.columnWeights = new double[]{0.0, 0.0};
gbl_nst2RefPositionPanel.rowWeights = new double[]{0.0, 0.0, 0.0};
nst2RefPositionPanel.setLayout(gbl_nst2RefPositionPanel);
latitudeLabel = new JLabel("label");
GridBagConstraints gbc_latitudeLabel = new GridBagConstraints();
gbc_latitudeLabel.insets = new Insets(10, 20, 0, 10);
gbc_latitudeLabel.anchor = GridBagConstraints.WEST;
gbc_latitudeLabel.gridx = 0;
gbc_latitudeLabel.gridy = 0;
nst2RefPositionPanel.add(latitudeLabel, gbc_latitudeLabel);
latitudeTxt = new JTextField();
GridBagConstraints gbc_latitudeTxt = new GridBagConstraints();
gbc_latitudeTxt.insets = new Insets(10, 0, 0, 20);
gbc_latitudeTxt.fill = GridBagConstraints.HORIZONTAL;
gbc_latitudeTxt.gridx = 1;
gbc_latitudeTxt.gridy = 0;
nst2RefPositionPanel.add(latitudeTxt, gbc_latitudeTxt);
latitudeTxt.setColumns(10);
longitudeLabel = new JLabel("label");
GridBagConstraints gbc_longitudeLabel = new GridBagConstraints();
gbc_longitudeLabel.anchor = GridBagConstraints.WEST;
gbc_longitudeLabel.insets = new Insets(0, 20, 0, 10);
gbc_longitudeLabel.gridx = 0;
gbc_longitudeLabel.gridy = 1;
nst2RefPositionPanel.add(longitudeLabel, gbc_longitudeLabel);
longitudeTxt = new JTextField();
GridBagConstraints gbc_longitudeTxt = new GridBagConstraints();
gbc_longitudeTxt.insets = new Insets(0, 0, 0, 20);
gbc_longitudeTxt.fill = GridBagConstraints.HORIZONTAL;
gbc_longitudeTxt.gridx = 1;
gbc_longitudeTxt.gridy = 1;
nst2RefPositionPanel.add(longitudeTxt, gbc_longitudeTxt);
longitudeTxt.setColumns(10);
zoomLabel = new JLabel("label");
GridBagConstraints gbc_zoomLabel = new GridBagConstraints();
gbc_zoomLabel.anchor = GridBagConstraints.WEST;
gbc_zoomLabel.insets = new Insets(0, 20, 15, 10);
gbc_zoomLabel.gridx = 0;
gbc_zoomLabel.gridy = 2;
nst2RefPositionPanel.add(zoomLabel, gbc_zoomLabel);
zoomTxt = new JTextField();
GridBagConstraints gbc_zoomTxt = new GridBagConstraints();
gbc_zoomTxt.insets = new Insets(0, 0, 15, 20);
gbc_zoomTxt.fill = GridBagConstraints.HORIZONTAL;
gbc_zoomTxt.gridx = 1;
gbc_zoomTxt.gridy = 2;
nst2RefPositionPanel.add(zoomTxt, gbc_zoomTxt);
zoomTxt.setColumns(10);
nestedPanel3 = new JPanel();
GridBagConstraints gbc_nestedPanel3 = new GridBagConstraints();
gbc_nestedPanel3.weighty = 1.0;
gbc_nestedPanel3.weightx = 1.0;
gbc_nestedPanel3.fill = GridBagConstraints.BOTH;
gbc_nestedPanel3.insets = new Insets(20, 10, 15, 20);
gbc_nestedPanel3.gridx = 2;
gbc_nestedPanel3.gridy = 1;
panel.add(nestedPanel3, gbc_nestedPanel3);
GridBagLayout gbl_nestedPanel3 = new GridBagLayout();
gbl_nestedPanel3.columnWidths = new int[]{0};
gbl_nestedPanel3.rowHeights = new int[]{0, 0, 0, 0};
gbl_nestedPanel3.columnWeights = new double[]{0.0};
gbl_nestedPanel3.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0};
nestedPanel3.setLayout(gbl_nestedPanel3);
detectionZoneLabel = new JLabel("label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.insets = new Insets(0, 0, 1, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
nestedPanel3.add(detectionZoneLabel, gbc_lblNewLabel);
scrollPane_2 = new JScrollPane();
GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
gbc_scrollPane_2.weighty = 1.0;
gbc_scrollPane_2.weightx = 1.0;
gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
gbc_scrollPane_2.gridx = 0;
gbc_scrollPane_2.gridy = 1;
nestedPanel3.add(scrollPane_2, gbc_scrollPane_2);
txtrDetectionZone = new JTextArea();
scrollPane_2.setViewportView(txtrDetectionZone);
relevanceZoneLabel = new JLabel("label");
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel_1.insets = new Insets(5, 0, 1, 0);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 2;
nestedPanel3.add(relevanceZoneLabel, gbc_lblNewLabel_1);
scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.weighty = 1.0;
gbc_scrollPane_1.weightx = 1.0;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 0;
gbc_scrollPane_1.gridy = 3;
nestedPanel3.add(scrollPane_1, gbc_scrollPane_1);
txtrRelevanceZone = new JTextArea();
scrollPane_1.setViewportView(txtrRelevanceZone);
nestedPanel5 = new JPanel();
nestedPanel5.setBorder(new TitledBorder(null, "Messages", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagConstraints gbc_nestedPanel5 = new GridBagConstraints();
gbc_nestedPanel5.weighty = 1.0;
gbc_nestedPanel5.weightx = 1.0;
gbc_nestedPanel5.fill = GridBagConstraints.BOTH;
gbc_nestedPanel5.gridwidth = 2;
gbc_nestedPanel5.insets = new Insets(10, 10, 10, 10);
gbc_nestedPanel5.gridx = 1;
gbc_nestedPanel5.gridy = 2;
panel.add(nestedPanel5, gbc_nestedPanel5);
GridBagLayout gbl_nestedPanel5 = new GridBagLayout();
gbl_nestedPanel5.columnWidths = new int[]{0, 0};
gbl_nestedPanel5.rowHeights = new int[]{0, 0};
gbl_nestedPanel5.columnWeights = new double[]{0.0, 0.0};
gbl_nestedPanel5.rowWeights = new double[]{0.0, 0.0};
nestedPanel5.setLayout(gbl_nestedPanel5);
for(int index = 0 ; index < 4; index++) {
nstPanel[index] = new SignalingMessagePane();
nstPanel[index].initialize();
GridBagConstraints gbc_nst5Panel1 = new GridBagConstraints();
gbc_nst5Panel1.weighty = 0.5;
gbc_nst5Panel1.weightx = 0.5;
gbc_nst5Panel1.insets = new Insets(5, 5, 5, 10);
gbc_nst5Panel1.fill = GridBagConstraints.BOTH;
gbc_nst5Panel1.gridx = index % 2;
gbc_nst5Panel1.gridy = index > 1? 1:0;
nestedPanel5.add(nstPanel[index], gbc_nst5Panel1);
}
}
}
How can I avoid this behaviour? What am I doing wrong?
Solution
Change this line of the code you posted:
txtrDetectionZone = new JTextArea();
Give the JTextArea
an initial size by using the constructor that takes the number of rows and columns, e.g.
txtrDetectionZone = new JTextArea(10, 50);
Also you can set the scrollbar policy so that the scrollbars always appear, e.g.
scrollPane_2 = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
That way, the scrollbars will not appear and disappear.
Answered By - Abra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.