Example usage for java.awt GridBagConstraints GridBagConstraints

List of usage examples for java.awt GridBagConstraints GridBagConstraints

Introduction

In this page you can find the example usage for java.awt GridBagConstraints GridBagConstraints.

Prototype

public GridBagConstraints() 

Source Link

Document

Creates a GridBagConstraint object with all of its fields set to their default value.

Usage

From source file:Main.java

/**
 * @param scrollPane/*from  w  w w  . j a  v a2 s  .  c o m*/
 * @return
 */
public static GridBagConstraints initScrollPane(JScrollPane scrollPane) {
    scrollPane.setLayout(new ScrollPaneLayout());
    scrollPane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    GridBagConstraints gb = new GridBagConstraints();
    gb.gridx = 0;
    gb.gridy = 0;
    gb.insets = new Insets(2, 2, 2, 2);
    return gb;
}

From source file:MainClass.java

public MainClass() {
    super("Clipboard Test");
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);//from   w w w  . jav a2s .  c  o m

    srcText = new TextArea(8, 32);
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.CENTER;
    gridbag.setConstraints(srcText, c);
    add(srcText);

    copyButton = new Button("Copy Above");
    copyButton.setActionCommand("copy");
    copyButton.addActionListener(this);
    c.gridy = 1;
    c.gridwidth = 1;
    gridbag.setConstraints(copyButton, c);
    add(copyButton);

    pasteButton = new Button("Paste Below");
    pasteButton.setActionCommand("paste");
    pasteButton.addActionListener(this);
    pasteButton.setEnabled(false);
    c.gridx = 1;
    gridbag.setConstraints(pasteButton, c);
    add(pasteButton);

    dstText = new TextArea(8, 32);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    gridbag.setConstraints(dstText, c);
    add(dstText);

    pack();
}

From source file:Main.java

/**
 * helper method to create a GridBagConstrained configured according to the parameters
 * @param x the gridx value// www.j  a v a 2  s . co  m
 * @param y the gridy value
 * @param gridwidth the gridwidth value
 * @param gridheight the gridheight value
 * @param anchor the value of the anchor property
 * @param insets the insets to be used by this constraint
 * @return a GridBagConstraint object
 */
public static GridBagConstraints getGridBagConstraint(int x, int y, int gridwidth, int gridheight, int anchor,
        Insets insets) {

    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = x;
    gridBagConstraints.gridy = y;
    gridBagConstraints.gridwidth = gridwidth;
    gridBagConstraints.gridheight = gridheight;
    gridBagConstraints.anchor = anchor;
    gridBagConstraints.insets = insets;
    return gridBagConstraints;
}

From source file:Main.java

public MyPanel() {
    JTextField labelA = new JTextField("Your A component");
    JTextField labelB = new JTextField("Your B component");
    JTextField labelC = new JTextField("Your C component");
    JTextField labelD = new JTextField("Top Right D");

    JPanel north = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_END;
    gbc.weightx = 1;// www .  j a va  2  s  . co m
    gbc.insets = new Insets(10, 10, 10, 10);
    north.add(labelD, gbc);

    JPanel south = new JPanel(new GridBagLayout());
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridy = 0;
    south.add(labelA, gbc);
    gbc.gridy = 1;
    south.add(labelB, gbc);
    gbc.gridy = 2;
    south.add(labelC, gbc);

    setLayout(new BorderLayout());
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.CENTER);
}

From source file:Main.java

public LoginPanel() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.anchor = GridBagConstraints.CENTER;
    gbc.weightx = 1;//w ww .j  a v  a 2s .c o m
    gbc.gridx = 2;
    gbc.anchor = GridBagConstraints.EAST;
    JLabel label = new JLabel("Username: ");
    add(label, gbc);

    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 3;
    gbc.gridwidth = 2;
    add(userfield, gbc);

    gbc.gridy = 1;
    add(passfield, gbc);

    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridx = 2;
    label = new JLabel("Password: ");
    add(label, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridy = 2;
    gbc.gridx = 1;
    gbc.gridwidth = 5;
    add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane = new JPanel();

    setContentPane(contentPane);/*ww w  . j  ava2s  .  c  o  m*/
    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[] { 0, 0, 0, 0 };
    gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
    gbl_panel.columnWeights = new double[] { 0.0, 0.1, 0.0, Double.MIN_VALUE };
    gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };

    contentPane.setLayout(gbl_panel);

    JButton btn_1 = new JButton("B1");
    GridBagConstraints gbc_comboBox = new GridBagConstraints();
    gbc_comboBox.insets = new Insets(0, 0, 5, 20);
    gbc_comboBox.gridx = 2;
    gbc_comboBox.gridy = 0;
    gbc_comboBox.weightx = 0.0;
    contentPane.add(btn_1, gbc_comboBox);

    JButton btn_2 = new JButton("B2");
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    gbc_btnNewButton.insets = new Insets(0, 0, 5, 20);
    gbc_btnNewButton.gridx = 2;
    gbc_btnNewButton.gridy = 1;
    contentPane.add(btn_2, gbc_btnNewButton);
    setSize(200, 300);
}

From source file:EditorPaneExample3.java

public EditorPaneExample3() {
    super("JEditorPane Example 3");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;//ww  w. j  a va  2  s. co m
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    textField = new JTextField(32);
    panel.add(textField, c);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();
            try {
                // Try to display the page
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);
                pane.setPage(url);

                loadingState.setText("Loaded");
                loadedType.setText(pane.getContentType());
            } catch (IOException e) {
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
            }
        }
    });
}

From source file:EditorPaneExample10A.java

public EditorPaneExample10A() {
    super("JEditorPane Example 10 - using getIterator");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;// w  w  w .  j  a  va 2s.  co  m
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);
    c.gridy = 3;
    panel.add(new JLabel(LOAD_TIME), c);

    c.gridy = 4;
    c.gridwidth = 2;
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.WEST;
    onlineLoad = new JCheckBox("Online Load");
    panel.add(onlineLoad, c);
    onlineLoad.setSelected(true);
    onlineLoad.setForeground(typeLabel.getForeground());

    c.gridx = 1;
    c.gridy = 0;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.HORIZONTAL;

    urlCombo = new JComboBox();
    panel.add(urlCombo, c);
    urlCombo.setEditable(true);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);
    timeLabel = new JLabel("");
    c.gridy = 3;
    panel.add(timeLabel, c);

    getContentPane().add(panel, "South");

    // Change page based on combo selection
    urlCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (populatingCombo == true) {
                return;
            }
            Object selection = urlCombo.getSelectedItem();
            try {
                // Check if the new page and the old
                // page are the same.
                URL url;
                if (selection instanceof URL) {
                    url = (URL) selection;
                } else {
                    url = new URL((String) selection);
                }

                URL loadedURL = pane.getPage();
                if (loadedURL != null && loadedURL.sameFile(url)) {
                    return;
                }

                // Try to display the page
                urlCombo.setEnabled(false); // Disable input
                urlCombo.paintImmediately(0, 0, urlCombo.getSize().width, urlCombo.getSize().height);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                // Busy cursor
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);

                timeLabel.setText("");
                timeLabel.paintImmediately(0, 0, timeLabel.getSize().width, timeLabel.getSize().height);

                startTime = System.currentTimeMillis();

                // Choose the loading method
                if (onlineLoad.isSelected()) {
                    // Usual load via setPage
                    pane.setPage(url);
                    loadedType.setText(pane.getContentType());
                } else {
                    pane.setContentType("text/html");
                    loadedType.setText(pane.getContentType());
                    if (loader == null) {
                        loader = new HTMLDocumentLoader();
                    }
                    HTMLDocument doc = loader.loadDocument(url);
                    loadComplete();
                    pane.setDocument(doc);
                    displayLoadTime();
                    populateCombo(findLinks(doc, null));
                    enableInput();
                }
            } catch (Exception e) {
                System.out.println(e);
                JOptionPane.showMessageDialog(pane,
                        new String[] { "Unable to open file", selection.toString() }, "File Open Error",
                        JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
                enableInput();
            }
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadComplete();
                displayLoadTime();
                populateCombo(findLinks(pane.getDocument(), null));
                enableInput();
            }
        }
    });
}

From source file:EditorPaneExample4.java

public EditorPaneExample4() {
    super("JEditorPane Example 4");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;//from  w  w w.  j  ava 2 s  .com
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    textField = new JTextField(32);
    panel.add(textField, c);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();

            try {
                // Check if the new page and the old
                // page are the same.
                URL newURL = new URL(url);
                URL loadedURL = pane.getPage();
                if (loadedURL != null && loadedURL.sameFile(newURL)) {
                    return;
                }

                // Try to display the page
                textField.setEnabled(false); // Disable input
                textField.paintImmediately(0, 0, textField.getSize().width, textField.getSize().height);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                // Busy cursor
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);
                pane.setPage(url);

                loadedType.setText(pane.getContentType());
            } catch (Exception e) {
                System.out.println(e);
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
                textField.setEnabled(true);
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadingState.setText("Page loaded.");
                textField.setEnabled(true); // Allow entry of new URL
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });
}

From source file:layout.GridBagLayoutDemo.java

public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }/*from   w  ww  . j  ava 2 s  .  co  m*/

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
        //natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    button = new JButton("Button 1");
    if (shouldWeightX) {
        c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 40; //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0; //reset to default
    c.weighty = 1.0; //request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; //bottom of space
    c.insets = new Insets(10, 0, 0, 0); //top padding
    c.gridx = 1; //aligned with button 2
    c.gridwidth = 2; //2 columns wide
    c.gridy = 2; //third row
    pane.add(button, c);
}