Java Code Examples for java.awt.GridBagLayout#getConstraints()

The following examples show how to use java.awt.GridBagLayout#getConstraints() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericDialogPlus.java    From ij-ridgedetection with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the directory or file field.
 *
 * @param label
 *            the label
 * @param defaultPath
 *            the default path
 * @param columns
 *            the columns
 */
public void addDirectoryOrFileField(String label, String defaultPath, int columns) {
	addStringField(label, defaultPath, columns);
	if (isHeadless())
		return;

	TextField text = (TextField) stringField.lastElement();
	GridBagLayout layout = (GridBagLayout) getLayout();
	GridBagConstraints constraints = layout.getConstraints(text);

	Button button = new Button("Browse...");
	DirectoryListener listener = new DirectoryListener("Browse for " + label, text,
			JFileChooser.FILES_AND_DIRECTORIES);
	button.addActionListener(listener);
	button.addKeyListener(this);

	Panel panel = new Panel();
	panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
	panel.add(text);
	panel.add(button);

	layout.setConstraints(panel, constraints);
	add(panel);
}
 
Example 2
Source File: GenericDialogPlus.java    From ij-ridgedetection with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the directory field.
 *
 * @param label
 *            the label
 * @param defaultPath
 *            the default path
 * @param columns
 *            the columns
 */
public void addDirectoryField(String label, String defaultPath, int columns) {
	addStringField(label, defaultPath, columns);
	if (isHeadless())
		return;

	TextField text = (TextField) stringField.lastElement();
	GridBagLayout layout = (GridBagLayout) getLayout();
	GridBagConstraints constraints = layout.getConstraints(text);

	Button button = new Button("Browse...");
	DirectoryListener listener = new DirectoryListener("Browse for " + label, text);
	button.addActionListener(listener);
	button.addKeyListener(this);

	Panel panel = new Panel();
	panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
	panel.add(text);
	panel.add(button);

	layout.setConstraints(panel, constraints);
	add(panel);
}
 
Example 3
Source File: GenericDialogPlus.java    From ij-ridgedetection with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the file field.
 *
 * @param label
 *            the label
 * @param defaultPath
 *            the default path
 * @param columns
 *            the columns
 */
public void addFileField(String label, String defaultPath, int columns) {
	addStringField(label, defaultPath, columns);
	if (isHeadless())
		return;

	TextField text = (TextField) stringField.lastElement();
	GridBagLayout layout = (GridBagLayout) getLayout();
	GridBagConstraints constraints = layout.getConstraints(text);

	Button button = new Button("Browse...");
	FileListener listener = new FileListener("Browse for " + label, text);
	button.addActionListener(listener);
	button.addKeyListener(this);

	Panel panel = new Panel();
	panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
	panel.add(text);
	panel.add(button);

	layout.setConstraints(panel, constraints);
	add(panel);
}
 
Example 4
Source File: GUIElementFactoryTest.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public void testGridBagPanel_fourParams() {
    GridBagLayout gbl = new GridBagLayout();
    JPanel panel = new JPanel(gbl);
    JLabel comp = new JLabel("text");

    final GridBagConstraints gbconstr = GridBagUtils.createDefaultConstraints();
    GridBagUtils.setAttributes(gbconstr, "gridx=2, gridy=4");
    GridBagUtils.addToPanel(panel, comp, gbconstr);

    GridBagConstraints gbc = gbl.getConstraints(comp);
    assertEquals(GridBagConstraints.WEST, gbc.anchor);
    assertEquals(0, gbc.insets.top);
    assertEquals(3, gbc.insets.left);
    assertEquals(0, gbc.insets.bottom);
    assertEquals(3, gbc.insets.right);
    assertEquals(2, gbc.gridx);
    assertEquals(4, gbc.gridy);
    assertEquals(0, gbc.weightx, 0.01);
    assertEquals(0, gbc.weighty, 0.01);
    assertEquals(0, gbc.ipadx);
    assertEquals(0, gbc.ipady);
    assertEquals(1, gbc.gridheight);
    assertEquals(1, gbc.gridwidth);
}
 
Example 5
Source File: GUIElementFactoryTest.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public void testGridBagPanel_sevenParams() {
    GridBagLayout gbl = new GridBagLayout();
    JPanel panel = new JPanel(gbl);
    JLabel comp = new JLabel("text");


    final GridBagConstraints gbconstraints = GridBagUtils.createDefaultConstraints();
    GridBagUtils.setAttributes(gbconstraints, "gridx=2, gridy=4, anchor=SOUTHEAST, weighty=1.3, insets.top=5");
    GridBagUtils.addToPanel(panel, comp, gbconstraints);

    GridBagConstraints gbc = gbl.getConstraints(comp);
    assertEquals(GridBagConstraints.SOUTHEAST, gbc.anchor);
    assertEquals(5, gbc.insets.top);
    assertEquals(3, gbc.insets.left);
    assertEquals(0, gbc.insets.bottom);
    assertEquals(3, gbc.insets.right);
    assertEquals(2, gbc.gridx);
    assertEquals(4, gbc.gridy);
    assertEquals(0, gbc.weightx, 0.01);
    assertEquals(1.3, gbc.weighty, 0.01);
    assertEquals(0, gbc.ipadx);
    assertEquals(0, gbc.ipady);
    assertEquals(1, gbc.gridheight);
    assertEquals(1, gbc.gridwidth);
}
 
Example 6
Source File: GUIElementFactoryTest.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public void testGridBagPanel_eightParams() {
    GridBagLayout gbl = new GridBagLayout();
    JPanel panel = new JPanel(gbl);
    JLabel comp = new JLabel("text");

    final GridBagConstraints gbconstraints = GridBagUtils.createDefaultConstraints();
    GridBagUtils.setAttributes(gbconstraints,
                               "gridx=2, gridy=4, anchor=SOUTHEAST, weighty=1.3, insets.top=5, gridwidth=3");
    GridBagUtils.addToPanel(panel, comp, gbconstraints);

    GridBagConstraints gbc = gbl.getConstraints(comp);
    assertEquals(GridBagConstraints.SOUTHEAST, gbc.anchor);
    assertEquals(5, gbc.insets.top);
    assertEquals(3, gbc.insets.left);
    assertEquals(0, gbc.insets.bottom);
    assertEquals(3, gbc.insets.right);
    assertEquals(2, gbc.gridx);
    assertEquals(4, gbc.gridy);
    assertEquals(0, gbc.weightx, 0.01);
    assertEquals(1.3, gbc.weighty, 0.01);
    assertEquals(0, gbc.ipadx);
    assertEquals(0, gbc.ipady);
    assertEquals(1, gbc.gridheight);
    assertEquals(3, gbc.gridwidth);
}
 
Example 7
Source File: PluginHelper.java    From SPIM_Registration with GNU General Public License v2.0 6 votes vote down vote up
public static void addSaveAsFileField( final GenericDialogPlus dialog, final String label, final String defaultPath, final int columns) {
	dialog.addStringField( label, defaultPath, columns );

	final TextField text = ( TextField ) dialog.getStringFields().lastElement();
	final GridBagLayout layout = ( GridBagLayout ) dialog.getLayout();
	final GridBagConstraints constraints = layout.getConstraints( text );

	final Button button = new Button( "Browse..." );
	final ChooseXmlFileListener listener = new ChooseXmlFileListener( text );
	button.addActionListener( listener );
	button.addKeyListener( dialog );

	final Panel panel = new Panel();
	panel.setLayout( new FlowLayout( FlowLayout.LEFT, 0, 0 ) );
	panel.add( text );
	panel.add( button );

	layout.setConstraints( panel, constraints );
	dialog.add( panel );
}
 
Example 8
Source File: MultipleParameterPanel.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
public synchronized void removeParameter(int index){
    GridBagLayout layout=(GridBagLayout)parametersPanel.getLayout();
    Row row=rows.get(index);
    row.panel.disconnect();
    
    parametersPanel.remove(row.panel);
    parametersPanel.remove(row.removeButton);
    rows.remove(index);
    for(int i=index;i<rows.size();i++){
        row=rows.get(i);
        GridBagConstraints gbc=layout.getConstraints(row.panel);
        gbc.gridy--;
        layout.setConstraints(row.panel, gbc);
        gbc=layout.getConstraints(row.removeButton);
        gbc.gridy--;
        layout.setConstraints(row.removeButton, gbc);
        if(this.orderingHint!=null) row.panel.setOrderingHint(this.orderingHint+i);
    }
    
    this.revalidate();
    parametersPanel.repaint();
}
 
Example 9
Source File: DirectiveEditor.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
public void removeAddon(AddonPanel addonPanel){
    synchronized(addonPanels){
        int index=addonPanels.indexOf(addonPanel);
        if(index<0) return;
        addonPanel.disconnect();
        GridBagLayout gbl=(GridBagLayout)addonPanelContainer.getLayout();
        for(int i=index+1;i<addonPanels.size();i++){
            AddonPanel p=addonPanels.get(i);
            GridBagConstraints gbc=gbl.getConstraints(p);
            gbc.gridy--;
            gbl.setConstraints(p, gbc);
        }
        addonPanels.remove(index);
        addonPanelContainer.remove(addonPanel);
    }
    this.revalidate();
    addonPanelContainer.repaint();        
}
 
Example 10
Source File: FlatHeaderUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
private void scaleLayout( JXHeader header ) {
	if( UIScale.getUserScaleFactor() == 1f )
		return;

	LayoutManager layout = header.getLayout();
	if( !(layout instanceof GridBagLayout) )
		return;

	GridBagLayout gbl = (GridBagLayout) layout;
	for( Component c : header.getComponents() ) {
		GridBagConstraints cons = gbl.getConstraints( c );
		cons.insets = UIScale.scale( cons.insets );
		gbl.setConstraints( c, cons );
	}
}
 
Example 11
Source File: GenericDialogPlus.java    From ij-ridgedetection with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the constraints.
 *
 * @return the constraints
 */
// right away)
protected GridBagConstraints getConstraints() {
	GridBagLayout layout = (GridBagLayout) getLayout();
	Panel panel = new Panel();
	addPanel(panel);
	GridBagConstraints constraints = layout.getConstraints(panel);
	remove(panel);
	return constraints;
}
 
Example 12
Source File: GUIHelper.java    From SPIM_Registration with GNU General Public License v2.0 4 votes vote down vote up
/**
	 * A copy of Curtis's method
	 * 
	 * https://github.com/openmicroscopy/bioformats/blob/v4.4.8/components/loci-plugins/src/loci/plugins/util/WindowTools.java#L72
	 * 
	 *
	 * @param pane
	 */
	public static void addScrollBars(Container pane) {
//        * <dependency>
//        * <groupId>${bio-formats.groupId}</groupId>
//        * <artifactId>loci_plugins</artifactId>
//        * <version>${bio-formats.version}</version>
//        * </dependency>

		GridBagLayout layout = (GridBagLayout) pane.getLayout();

		// extract components
		int count = pane.getComponentCount();
		Component[] c = new Component[count];
		GridBagConstraints[] gbc = new GridBagConstraints[count];
		for (int i = 0; i < count; i++) {
			c[i] = pane.getComponent(i);
			gbc[i] = layout.getConstraints(c[i]);
		}

		// clear components
		pane.removeAll();
		layout.invalidateLayout(pane);

		// create new container panel
		Panel newPane = new Panel();
		GridBagLayout newLayout = new GridBagLayout();
		newPane.setLayout(newLayout);
		for (int i = 0; i < count; i++) {
			newLayout.setConstraints(c[i], gbc[i]);
			newPane.add(c[i]);
		}

		// HACK - get preferred size for container panel
		// NB: don't know a better way:
		// - newPane.getPreferredSize() doesn't work
		// - newLayout.preferredLayoutSize(newPane) doesn't work
		Frame f = new Frame();
		f.setLayout(new BorderLayout());
		f.add(newPane, BorderLayout.CENTER);
		f.pack();
		final Dimension size = newPane.getSize();
		f.remove(newPane);
		f.dispose();

		// compute best size for scrollable viewport
		size.width += 25;
		size.height += 15;
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int maxWidth = 7 * screen.width / 8;
		int maxHeight = 3 * screen.height / 4;
		if (size.width > maxWidth)
			size.width = maxWidth;
		if (size.height > maxHeight)
			size.height = maxHeight;

		// create scroll pane
		ScrollPane scroll = new ScrollPane() {
			private static final long serialVersionUID = 1L;

			public Dimension getPreferredSize() {
				return size;
			}
		};
		scroll.add(newPane);

		// add scroll pane to original container
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		layout.setConstraints(scroll, constraints);
		pane.add(scroll);
	}