I have a problem with a class that extends JPanel. In my game which is based on a JFrame, I have a several JPanels that I swap while program is running (login panel, new acc form panel, etc... all in one JFrame). I want to switch to Panel with GBL, but unfortunately it doesn't display the content well... It only shows ChatBox (JTextArea) and typing field (JTextField) in the center of the screen, but I want to put them next to other.
public class InGameMenu extends JPanel
private JTextArea chatBox;
private JPanel buttonPanel;
private TextField chatTextField;
private JList playersList;
private DefaultListModel listModel;
private ConnectionToServer conn;
private JFrame mainFrame;
public InGameMenu (ConnectionToServer conn, JFrame mainFrame)
{
this.setBackground(Color.CYAN);
this.conn = conn;
this.mainFrame = mainFrame;
mainFrame.setTitle("Game menu");
super.setLayout(new GridBagLayout());
chatBox = new JTextArea("This is player's chat.\n");
GridBagConstraints gbc = new GridBagConstraints();
// chat box
gbc.gridheight = 10;
gbc.gridwidth = 10;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(chatBox, gbc);
// chat text field
chatTextField = new TextField("dsadasda");
chatTextField.setBackground(Color.GRAY);
gbc.gridheight = 1;
gbc.gridy = 1;
this.add (chatTextField, gbc);
// lista graczy
listModel = new DefaultListModel();
playersList = new JList(listModel);
gbc.gridheight = 15;
gbc.gridwidth = 4;
gbc.gridx = 1;
gbc.gridy = 0;
this.add (playersList, gbc);
// panel przyciskow
buttonPanel = new JPanel(new FlowLayout());
gbc.gridheight = 1;
gbc.gridy = 1;
this.add (buttonPanel, gbc);
}
}
Your use of gridHeight and gridWidth constants seems to be off, and seems to be expecting abilities that GridBagLayout doesn't have. You don't define column size and row size of a JTextArea this way for instance. Instead, set the row size and column sizes of your text components and the prototype size of your JList by calling the appropriate constructor or method. For example:
import java.awt.*;
import javax.swing.*;
public class InGameMenu extends JPanel {
private static final String[] EXAMPLE_TEXT = {"One", "Two", "Three"};
private static final int COLUMNS = 40;
private static final int ROWS = 20;
private JTextArea chatBox;
private JPanel buttonPanel;
private TextField chatTextField;
private JList playersList;
private DefaultListModel listModel;
// private ConnectionToServer conn;
private JFrame mainFrame;
// public InGameMenu (ConnectionToServer conn, JFrame mainFrame)
public InGameMenu(JFrame mainFrame) {
this.setBackground(Color.CYAN);
// this.conn = conn;
this.mainFrame = mainFrame;
mainFrame.setTitle("Game menu");
super.setLayout(new GridBagLayout());
// chatBox = new JTextArea("This is player's chat.\n");
chatBox = new JTextArea("This is player's chat.\n", ROWS, COLUMNS);
GridBagConstraints gbc = new GridBagConstraints();
// chat box
// gbc.gridheight = 10;
// gbc.gridwidth = 10;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
// this.add(chatBox, gbc);
chatBox.setWrapStyleWord(true);
chatBox.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(chatBox);
this.add(scrollPane, gbc);
// chat text field
chatTextField = new TextField("dsadasda", COLUMNS);
chatTextField.setBackground(Color.GRAY);
// gbc.gridheight = 1;
gbc.gridy = 1;
this.add(chatTextField, gbc);
// lista graczy
listModel = new DefaultListModel();
for (int i = 0; i < EXAMPLE_TEXT.length; i++) {
listModel.addElement(EXAMPLE_TEXT[i]);
}
playersList = new JList(listModel);
playersList.setPrototypeCellValue(String.format("%40s", " "));
// gbc.gridheight = 15;
// gbc.gridwidth = 4;
gbc.gridx = 1;
gbc.gridy = 0;
this.add(playersList, gbc);
// panel przyciskow
buttonPanel = new JPanel(new FlowLayout());
// gbc.gridheight = 1;
gbc.gridy = 1;
this.add(buttonPanel, gbc);
}
public InGameMenu() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
JFrame frame = new JFrame("In Game Menu");
InGameMenu mainPanel = new InGameMenu(frame);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Now if you want your components to fill the view, then you'll want to set the weightx and weighty GridBagConstraints to non-zero values.