Reputation: 15
sorry if this has been asked before but I have spent ages searching with no success.
I have a panel in which I'm trying to add some components using GridBagLayout.
The Problem is that for some reason I can't figure out - the JTextArea I am trying to add seems to ignore the width being assigned to it - it seems to accept the height no problem.
All components are added fine except for the text area - which should have a width of 3, but displays with a width of 1.
I cannot understand why this is, can anyone help please?
Thanks in advance for any help.
Code: (EDIT)
public class Main {
public static void main(String[] args) {
Content master = new Content();
master.createAndShowGui();
}
}
package test;
//import declarations
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Content
{
//declare class variables
private JDialog schoolFrame = new JDialog();
private JPanel schoolPanel = new JPanel(new GridBagLayout());
private JTextArea text = new JTextArea();
private JButton exit = new JButton("Quit");
private JButton portView = new JButton("View Portfolio");
private JButton payView = new JButton("View Payment Info");
private GridBagConstraints c = new GridBagConstraints();
private JLabel payAmount = new JLabel("",SwingConstants.CENTER);
private JButton subPayment = new JButton("Submit Payment");
private JLabel subLabels[] = new JLabel[5];
private JTextField subFields[] = new JTextField[5];
private JButton subGradeChange = new JButton("Submit Changes");
JLabel tester = new JLabel();
JLabel tester2 = new JLabel("jjjjjjj");
public Content(){
}
private void setOptions(JComponent b,int weightx,int weighty,int x,int y,int width,int height)
{
c.fill = GridBagConstraints.BOTH;
c.weightx = weightx;
c.weighty = weighty;
c.gridy = y;
c.gridx = x;
c.gridwidth = width;
c.gridheight = height;
schoolPanel.add(b, c);
}
//method to create and display the GUI
public JDialog createAndShowGui()
{
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0,0,0,0);
schoolFrame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
schoolFrame.setContentPane(schoolPanel);
schoolFrame.setMinimumSize(new Dimension(600,600));
schoolFrame.setMaximumSize(new Dimension(600,600));
text.setEditable(false);
schoolFrame.setModal(true);
schoolFrame.setTitle("Simple School System: House Head");
setOptions(tester, 1, 1, 2, 0, 1, 1);
setOptions(portView,1,1,0,0,2,1);
setOptions(text,1,1,2,0,3,7);
for(int i=0;i<subLabels.length;i++){
subLabels[i] = new JLabel();
subFields[i] = new JTextField();
subLabels[i].setText("Subject: ");
subFields[i].setText("Enter Grade");
setOptions(subLabels[i],1,1,0,i+1,1,1);
setOptions(subFields[i],1,1,1,i+1,1,1);
}
setOptions(subGradeChange,1,1,1,6,1,1);
setOptions(exit,1,1,0,6,1,1);
exit.addActionListener
(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int quit = JOptionPane.showConfirmDialog(null,"Exit","Are you sure",JOptionPane.YES_NO_OPTION);
if(quit==0)
{
System.exit(0);
}
else
{
schoolFrame.dispose();
hideUpdate();
}
}
});
//pack components before displaying frame
schoolFrame.pack();
text.setPreferredSize(text.getPreferredSize());
schoolFrame.pack();
schoolFrame.setLocationRelativeTo(null);
schoolFrame.setVisible(true);
return schoolFrame;
}
//method to update the contents of the frame
private void hideUpdate()
{
schoolFrame.setVisible(false);
schoolFrame.pack();
schoolFrame.repaint();
schoolFrame.pack();
schoolFrame.setVisible(true);
}
}
Upvotes: 1
Views: 428
Reputation: 57421
To calculate 3 columns width there should be something in column 2 and 3.
So in fact you add all your components in one column. LayoutManager can't figure out width of column 2 and 3 because there is no components in the columns. So their widths are 0 and your textarea's width =1st column widht + 0 + 0
Upvotes: 2