thanks, for all your patient help so far,
it was really useful for me.
Dear Folks,
here another question, in the example below are two buttons,
within a GridBagLayout, it seams to me as if they are centered
within their containing JPanel. How can I make them move further
left or west. So as if they line up "left to right", using all the vertical
space (Y-height) of the containing JPanel. But not using all the space
right of them (X-width). In other words there should be some free space
right or east of them, because not all space is needed.
JToggleButton + JToggleButton + FreeSpace
How can I do this?
Thanks in advance,
Karl
- Code: Select all
package Swing.Designer.Forum.com;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class FillVertically extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FillVertically frame = new FillVertically();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public FillVertically() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel);
final JPanel panel_2 = new JPanel();
panel_2.setPreferredSize(new Dimension(0, 55));
panel.add(panel_2, BorderLayout.NORTH);
final JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
panel.add(panel_1);
final Component component = Box.createRigidArea(new Dimension(24, 0));
panel_1.add(component);
final JPanel panel_4 = new JPanel();
panel_4.setLayout(new GridBagLayout());
panel_1.add(panel_4);
final JToggleButton moveMeLeftToggleButton = new JToggleButton();
moveMeLeftToggleButton.setText("move me left 1");
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.weightx = 0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.anchor = GridBagConstraints.WEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
panel_4.add(moveMeLeftToggleButton, gridBagConstraints);
final JToggleButton moveMeLeftToggleButton_1 = new JToggleButton();
moveMeLeftToggleButton_1.setText("move me left 2");
final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridy = 0;
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.anchor = GridBagConstraints.WEST;
gridBagConstraints_1.fill = GridBagConstraints.VERTICAL;
panel_4.add(moveMeLeftToggleButton_1, gridBagConstraints_1);
final Component component_1 = Box.createRigidArea(new Dimension(24, 0));
panel_1.add(component_1);
final JPanel panel_3 = new JPanel();
panel_3.setPreferredSize(new Dimension(0, 80));
panel.add(panel_3, BorderLayout.SOUTH);
//
}
}