• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to align components in GridBagLayout in JAVA swing

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.*;
import static java.awt.GridBagConstraints.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Try extends JPanel {

//GB arguments:
private int gridx, gridy, gridwidth, gridheight, fill, anchor, ipadx, ipady;
private double weightx, weighty;
private Insets insets;
// GB Insets:
private int top, left, bottom, right;
private final Insets insetsTop = new Insets(top = 0, left = 0, bottom = 15, right = 0);
private final Insets insetsLabel = new Insets(top = 0, left = 10, bottom = 6, right = 5);
private final Insets insetsText = new Insets(top = 0, left = 0, bottom = 6, right = 10);
private final Insets insetsBottom = new Insets(top = 10, left = 0, bottom = 10, right = 0);
//input fields:
private JComboBox circuit_name;
private JComboBox customer_name;
private JComboBox traffic_capacity;
private JRadioButton uni_direction;
private JRadioButton bi_direction;
private JSpinner number_of_links;
private JSpinner number_of_copies;
private JComboBox protection_type;
private JCheckBox map_view ;
private JComboBox entry_point;



public Try() {
setLayout(new GridBagLayout());
example();
}

private void example() {



//customer_name row (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Customer name"), gridx = 1, gridy = 2,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsLabel);

//Customer name row (combobox):
customer_name = new JComboBox();
customer_name.setEditable(true);
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(customer_name, gridx = 2, gridy = 2,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsText);


//Traffic Capacity row (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Traffic Capacity"), gridx = 1, gridy = 3,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsLabel);

//Traffic Capacity row (combobox):
traffic_capacity = new JComboBox();
traffic_capacity.setEditable(true);
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(traffic_capacity, gridx = 2, gridy = 3,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsText);

//Number Of Links row (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Number Of Links "), gridx = 1, gridy = 4,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsLabel);

//number_of_links row (text area):
number_of_links = new JSpinner();
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(number_of_links, gridx = 2, gridy = 4,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsText);


// number_of_copies row (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Number Of Copies"), gridx = 1, gridy = 5,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsLabel);

// number_of_copies row (text area):
number_of_copies = new JSpinner();
number_of_copies.setEnabled(true);
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(number_of_copies, gridx = 2, gridy = 5,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsText);


//Protection Type(label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Protection Type"), gridx = 1, gridy = 6,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsLabel);

//protection_type row (text area):
protection_type= new JComboBox();
protection_type.setEditable(true);
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(protection_type, gridx = 2, gridy = 6,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsText);


//Map View (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Map View"), gridx = 1, gridy = 7,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsLabel);

//Map View row (CheckBox):
map_view= new JCheckBox();

setDefaultValuesGB();// default values for all GridBagConstraints
addGB(map_view, gridx = 2, gridy = 7,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsText);


//Entry point (label):
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(new JLabel("Entry Point"), gridx = 1, gridy = 8,
gridwidth, gridheight, fill,
weightx, weighty, anchor = FIRST_LINE_START,
insets = insetsLabel);

//Entry point (comboBox):
entry_point = new JComboBox();
entry_point.setEditable(true);
setDefaultValuesGB();// default values for all GridBagConstraints
addGB(entry_point, gridx = 2, gridy = 8,
gridwidth, gridheight, fill,
weightx, weighty, anchor =FIRST_LINE_START,
insets = insetsText);


}

// Convenience method, used to add components without internal padding:
private void addGB(final Component component, final int gridx, final int gridy,
final int gridwidth, final int gridheight,
final int fill, final double weightx, final double weighty,
final int anchor, final Insets insets) {
addGB(component, gridx, gridy, gridwidth, gridheight, fill, weightx, weighty, anchor, insets, ipadx, ipady);
}

private void addGB(final Component component, final int gridx, final int gridy,
final int gridwidth, final int gridheight,
final int fill, final double weightx, final double weighty,
final int anchor, final Insets insets,
final int ipadx, final int ipady) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.fill = fill;
constraints.weightx = weightx;
constraints.weighty = weighty;
constraints.anchor = anchor;
constraints.insets = insets;
constraints.ipadx = ipadx;
constraints.ipady = ipady;
add(component, constraints);
}

private void setDefaultValuesGB() {
// This method sets the default values for all GridBagConstraints,
// so we don't have to specify them with each addGB(...)
gridx = RELATIVE;
gridy = RELATIVE;
gridwidth = 1;
gridheight = 1;
fill = NONE;
weightx = 0.0;
weighty = 0.0;
anchor = FIRST_LINE_START;
insets = new Insets(0, 0, 0, 0);
ipadx = 0;
ipady = 0;
}

public static void main(final String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 350);
frame.setLocationRelativeTo(null);
frame.setContentPane(new Try());
frame.setVisible(true);
}
});
}
}[/code]

1.The code displays the component at center , i want it to display at top left corner. how to achieve that??
thanks in advance.
2. how to add Jpanel to GridBagLayout?
eg: i want to add the below Jpanel to gridbagLayout



 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've asked this before, but would you please UseCodeTags from now on?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic