I am writing the GUI for a chat program. I can't seem to get the scroller
to fill the frame horizontally and vertically and the messageInput
to fill the frame horizontally. This is how it looks:
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame{
private JPanel panel;
private JEditorPane content;
private JTextField messageInput;
private JScrollPane scroller;
private JMenu options;
private JMenuBar mb;
private JMenuItem item;
public GUI(){
/** This is the frame**/
this.setPreferredSize(new Dimension(380,600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
/** This is where the context shows up **/
content = new JEditorPane();
content.setEditable(false);
/** Scroller that shows up in the context JEditorPane **/
scroller = new JScrollPane(content);
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
panel.add(scroller, c);
/** This is where you type your message **/
messageInput = new JTextField();
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
c.weighty = 0.5;
panel.add(messageInput, c);
mb = new JMenuBar();
options = new JMenu("Options");
mb.add(options);
this.setJMenuBar(mb);
this.add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
Insets insets. Controls padding between the component and neighboring components. To make a set of constraints for a component or components, create a new instance of GridBagConstraints and set these public variables to the appropriate values.
GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns.
weightx and weighty specify how to distribute space among columns (weightx) and rows (weighty), which is important for specifying resizing behavior. GridBagLayout calculates the weight of a column to be the maximum weightx of all the components in a column.
gridwidth is like span in html's table , it describes the number of columns that the component can span over/occupy. – MadProgrammer.
Java GridBagLayout. The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline. The components may not be of same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells.
How to Use GridBagLayout. Click the Launch button to run GridBagLayoutDemo using Java™ Web Start ( download JDK 7 or later ). Alternatively, to compile and run the example yourself, consult the example index. The code for GridBagDemo is in GridBagLayoutDemo.java. GridBagLayout is one of the most flexible — and complex — layout managers...
The new horizontal space is split evenly among all the columns. This resizing behavior is based on weights the program assigns to individual components in the GridBagLayout. You will also notice that each component takes up all the available horizontal space — but not (as you can see with button 5) all the available vertical space.
Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of constraints object we arrange component's display area on the grid.
get the
scroller
to fill the frame horizontally and vertically and themessageInput
to fill the frame horizontally.
You want to fill in both directions, so set
c.fill = GridBagConstraints.BOTH; // not HORIZONTAL
The next part is to fix the weights, which will tell how much space to allocate for each component (relatively):
scroller = new JScrollPane(content);
c.weightx = 0.5;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
panel.add(scroller, c);
messageInput = new JTextField();
c.weightx = 0.5;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
panel.add(messageInput, c);
weightx
should be a non-zero value to allow the components to stretch horizontally. weighty
should be non-zero for the editor, but not for the text field so that it won't take extra vertical space (in this case you don't need to set c.fill = GridBagConstraints.HORIZONTAL
for it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With