Example usage for java.awt Container setLayout

List of usage examples for java.awt Container setLayout

Introduction

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

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

From source file:events.FocusEventDemo.java

public void addComponentsToPane(final Container pane) {
    GridBagLayout gridbag = new GridBagLayout();
    pane.setLayout(gridbag);

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; //Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);/*  w  ww .  jav  a  2s.  c  om*/

    c.weightx = 0.1; //Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector<String> vector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector<String> listVector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); //It's easier to see the focus change
    //if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; //Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    //Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    //The setRequestFocusEnabled method prevents a
    //component from being clickable, but it can still
    //get the focus through the keyboard - this ensures
    //user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);
    setPreferredSize(new Dimension(450, 450));
    ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:UndoableToggleApp4.java

public UndoableToggleApp4() {

    // Create some toggle buttons (and subclasses).
    tog = new JToggleButton("ToggleButton");
    cb = new JCheckBox("CheckBox");
    radio = new JRadioButton("RadioButton");

    // Add our listener to the buttons.
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);/*from ww w .ja v a2  s.c o  m*/
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Lay out the buttons.
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    // Create undo, redo, start, and end buttons.
    startButton = new JButton("Start");
    endButton = new JButton("End");
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");
    startButton.setEnabled(true);
    endButton.setEnabled(false);
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    // Add a listener to the start button. It creates a new StateEdit,
    // passing in this frame as the StateEditable.
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit = new StateEdit(UndoableToggleApp4.this);
            startButton.setEnabled(false);
            endButton.setEnabled(true);
            //undoButton.setEnabled(edit.canUndo());
            //
            // NOTE: We really don't want to be able to undo until end() is
            // pressed,
            // but StateEdit does not enforce this for us!
            undoButton.setEnabled(false);
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the end button. It will call end() on the
    // StateEdit.
    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            startButton.setEnabled(true);
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add a redo listener: just like the undo listener.
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Lay out the state/end and undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(startButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(endButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Lay out the main frame.
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:net.erdfelt.android.sdkfido.ui.SdkFidoFrame.java

private Component createBody() {
    Container body = new Container();

    body.setLayout(new GridBagLayout());
    JTabbedPane tabs = new JTabbedPane();

    tabs.addTab("SDKs", createSdkPanel());
    tabs.addTab("Work Dir", createWorkDirPanel());

    body.add(tabs, new GBC().fillWide().margin(5, 5, 5, 5).endRow());
    body.add(createConsolePane(), new GBC().fillBoth().margin(0, 5, 5, 5).weightTall(1.0).endBoth());

    return body;//from  w w  w.  j  a v a 2  s .  c  o m
}

From source file:FocusEventDemo.java

public void addComponentsToPane(final Container pane) {
    GridBagLayout gridbag = new GridBagLayout();
    pane.setLayout(gridbag);

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0; // Make column as wide as possible.
    JTextField textField = new JTextField("A TextField");
    textField.setMargin(new Insets(0, 2, 0, 2));
    textField.addFocusListener(this);
    gridbag.setConstraints(textField, c);
    add(textField);//from  ww  w.j  av a 2 s  .  c  o m

    c.weightx = 0.1; // Widen every other column a bit, when possible.
    c.fill = GridBagConstraints.NONE;
    JLabel label = new JLabel("A Label");
    label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    label.addFocusListener(this);
    gridbag.setConstraints(label, c);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector<String> vector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    gridbag.setConstraints(comboBox, c);
    add(comboBox);

    c.gridwidth = GridBagConstraints.REMAINDER;
    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    gridbag.setConstraints(button, c);
    add(button);

    c.weightx = 0.0;
    c.weighty = 0.1;
    c.fill = GridBagConstraints.BOTH;
    String listPrefix = "List Item #";
    Vector<String> listVector = new Vector<String>(numItems);
    for (int i = 0; i < numItems; i++) {
        listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); // It's easier to see the focus change
    // if an item is selected.
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    gridbag.setConstraints(listScrollPane, c);
    add(listScrollPane);

    c.weighty = 1.0; // Make this row as tall as possible.
    c.gridheight = GridBagConstraints.REMAINDER;
    // Set up the area that reports focus-gained and focus-lost events.
    display = new JTextArea();
    display.setEditable(false);
    // The setRequestFocusEnabled method prevents a
    // component from being clickable, but it can still
    // get the focus through the keyboard - this ensures
    // user accessibility.
    display.setRequestFocusEnabled(false);
    display.addFocusListener(this);
    JScrollPane displayScrollPane = new JScrollPane(display);

    gridbag.setConstraints(displayScrollPane, c);
    add(displayScrollPane);
    setPreferredSize(new Dimension(450, 450));
    ((JPanel) pane).setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:sim.app.sugarscape.SugarscapeWithUIHigh.java

void addChartPanel(Controller c, Sugarscape model) {
    charts = new Charts(model);
    ChartPanel ginipanel = null;//from   ww  w.j a v a2s . c o  m
    ChartPanel wealthpanel = null;
    ChartPanel agentspanel = null;
    ChartPanel agepanel = null;
    ChartPanel evolutionpanel = null;
    ChartPanel culturetagpanel = null;
    ChartPanel tradepanel = null;

    if (model.gini_chart_on) {
        ginipanel = new ChartPanel(charts.createGiniChart());
    }
    if (model.wealth_chart_on) {
        wealthpanel = new ChartPanel(charts.createChart4());
    }
    if (model.population_chart_on) {
        agentspanel = new ChartPanel(charts.createAgentsChart());
    }
    if (model.age_chart_on) {
        agepanel = new ChartPanel(charts.createAgeHistoChart());
    }
    if (model.evolution_chart_on) {
        evolutionpanel = new ChartPanel(charts.createEvolution());
    }
    if (model.culture_tag_chart_on) {
        culturetagpanel = new ChartPanel(charts.createCultureTagChart());
    }
    if (model.trade_chart_on) {
        tradepanel = new ChartPanel(charts.createTradeChart());
    }
    if ((!model.gini_chart_on) && (!model.wealth_chart_on) && (!model.population_chart_on)
            && (!model.age_chart_on) && (!model.evolution_chart_on) && (!model.culture_tag_chart_on)
            && (!model.trade_chart_on)) {
        return;
    }
    // create the chart frame
    chartFrame = new JFrame();
    chartFrame.setResizable(true);
    Container cp = chartFrame.getContentPane();
    cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
    if (agepanel != null) {
        cp.add(agepanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (tradepanel != null) {
        cp.add(tradepanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (ginipanel != null) {
        cp.add(ginipanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (wealthpanel != null) {
        cp.add(wealthpanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (agentspanel != null) {
        cp.add(agentspanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (culturetagpanel != null) {
        cp.add(culturetagpanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }
    if (evolutionpanel != null) {
        cp.add(evolutionpanel);
        cp.add(Box.createRigidArea(new Dimension(0, 6)));
    }

    //cp.add(chartPanel2);
    //cp.add(Box.createRigidArea(new Dimension(0,6)));
    //cp.add(chartPanel3);
    chartFrame.setTitle("Live Agent Statistics");
    chartFrame.pack();
    // register the chartFrame so it appears in the "Display" list
    c.registerFrame(chartFrame);
    // make the frame visible
    chartFrame.setVisible(true);
}

From source file:fungus.UtilizationChartFrame.java

public UtilizationChartFrame(String prefix) {
    simulationCycles = Configuration.getDouble(PAR_SIMULATION_CYCLES);
    this.setTitle("MycoNet Statistics Chart");
    graph = JungGraphObserver.getGraph();

    //data.add(-1,0);
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.setAutoWidth(false);/*  www  . j a v  a  2s  . co m*/
    dataset.setIntervalWidth(simulationCycles);

    averageUtilizationData = new XYSeries("Average Utilization");
    dataset.addSeries(averageUtilizationData);
    averageStableUtilizationData = new XYSeries("Avg Stable Util");
    dataset.addSeries(averageStableUtilizationData);
    hyphaRatioData = new XYSeries("Hypha Ratio");
    dataset.addSeries(hyphaRatioData);
    stableHyphaRatioData = new XYSeries("Stable Hypha Ratio");
    dataset.addSeries(stableHyphaRatioData);

    //XYSeriesCollection dataset;

    JFreeChart utilizationChart = ChartFactory.createXYLineChart("Utilization Metrics", "Cycle",
            "Average Utilization", dataset, PlotOrientation.VERTICAL, true, false, false);
    ChartPanel utilizationChartPanel = new ChartPanel(utilizationChart);

    //chart.setBackgroundPaint(Color.white);
    //XYPlot plot = chart.getXYPlot();

    //        BufferedImage chartImage = chart.createBufferedImage(500,300);
    //        chartLabel = new JLabel();
    //chartLabel.setIcon(new ImageIcon(chartImage));

    Container contentPane = getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    JPanel labelPane = new JPanel();
    labelPane.setLayout(new GridLayout(1, 1));
    //chartPane.setPreferredSize(new java.awt.Dimension(500, 300));
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));

    ////contentPane.add(labelPane,BorderLayout.PAGE_START);
    //contentPane.add(Box.createRigidArea(new Dimension(0,5)));
    contentPane.add(utilizationChartPanel, BorderLayout.CENTER);
    //contentPane.add(Box.createRigidArea(new Dimension(0,5)));
    ////contentPane.add(buttonPane, BorderLayout.PAGE_END);

    //data = node.getHyphaData();
    //link = node.getHyphaLink();
    //mycocast = node.getMycoCast();

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    //chartPanel.add(chartLabel);

    /*JButton updateButton = new JButton("Refresh");
      ActionListener updater = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      refreshData();
      }
      };
      updateButton.addActionListener(updater);
            
      JButton closeButton = new JButton("Close");
      ActionListener closer = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      closeFrame();
      }
      };
      closeButton.addActionListener(closer);
            
      buttonPane.add(Box.createHorizontalGlue());
      buttonPane.add(updateButton);
      buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
      buttonPane.add(closeButton);
      refreshData();
    */

    //JungGraphObserver.addChangeListener(this);

    this.pack();
    this.setVisible(true);
}

From source file:kenh.xscript.elements.Debug.java

private void initial(Container c) {
    c.setLayout(new BorderLayout());

    // Add variable list

    DefaultListModel<String> model = new DefaultListModel();

    if (this.getEnvironment() != null) {
        java.util.Set<String> keys = this.getEnvironment().getVariables().keySet();
        for (String key : keys) {
            model.addElement(key);/*w ww  .j a  v a 2 s  . c o m*/
        }
    } else {
        for (int i = 1; i < 10; i++) {
            model.addElement("Variable " + i);
        }
        model.addElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }

    JList list = new JList(model);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane listPane = new JScrollPane();
    listPane.setViewportView(list);
    listPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    c.add(listPane, BorderLayout.EAST);

    list.setPreferredSize(new Dimension(150, list.getPreferredSize().height));

    // 

    JTextField quote = new JTextField();
    quote.requestFocus();

    //JButton button = new JButton(">>");

    JPanel quotePanel = new JPanel();
    quotePanel.setLayout(new BorderLayout());
    quotePanel.add(quote, BorderLayout.CENTER);
    //quotePanel.add(button, BorderLayout.EAST);

    JTextArea result = new JTextArea();
    result.setEditable(false);

    JScrollPane resultPane = new JScrollPane();
    resultPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    resultPane.setViewportView(result);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(quotePanel, BorderLayout.NORTH);
    panel.add(resultPane, BorderLayout.CENTER);

    c.add(panel, BorderLayout.CENTER);

    list.addListSelectionListener(this);
    //button.addActionListener(this);
    quote.addKeyListener(this);

    this.result = result;
}

From source file:Accounts.java

private void buildGUI() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    accountNumberList = new JList();
    loadAccounts();//from  ww  w.  j  a  va  2s  .  c o m
    accountNumberList.setVisibleRowCount(2);
    JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList);

    //Do Get Account Button
    getAccountButton = new JButton("Get Account");
    getAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(
                        "SELECT * FROM acc_acc WHERE acc_id = " + accountNumberList.getSelectedValue());
                if (rs.next()) {
                    accountIDText.setText(rs.getString("acc_id"));
                    usernameText.setText(rs.getString("username"));
                    passwordText.setText(rs.getString("password"));
                    tsText.setText(rs.getString("ts"));
                    activeTSText.setText(rs.getString("act_ts"));
                }
            } catch (SQLException selectException) {
                displaySQLErrors(selectException);
            }
        }
    });

    //Do Insert Account Button
    insertAccountButton = new JButton("Insert Account");
    insertAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Statement statement = connection.createStatement();
                int i = statement.executeUpdate("INSERT INTO acc_acc VALUES(" + accountIDText.getText() + ", "
                        + "'" + usernameText.getText() + "', " + "'" + passwordText.getText() + "', " + "0"
                        + ", " + "now())");
                errorText.append("Inserted " + i + " rows successfully");
                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            }
        }
    });

    //Do Delete Account Button
    deleteAccountButton = new JButton("Delete Account");
    deleteAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Statement statement = connection.createStatement();
                int i = statement.executeUpdate(
                        "DELETE FROM acc_acc WHERE acc_id = " + accountNumberList.getSelectedValue());
                errorText.append("Deleted " + i + " rows successfully");
                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            }
        }
    });

    //Do Update Account Button
    updateAccountButton = new JButton("Update Account");
    updateAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Statement statement = connection.createStatement();
                int i = statement.executeUpdate("UPDATE acc_acc " + "SET username='" + usernameText.getText()
                        + "', " + "password='" + passwordText.getText() + "', " + "act_ts = now() "
                        + "WHERE acc_id = " + accountNumberList.getSelectedValue());
                errorText.append("Updated " + i + " rows successfully");
                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            }
        }
    });

    JPanel first = new JPanel(new GridLayout(5, 1));
    first.add(accountNumberListScrollPane);
    first.add(getAccountButton);
    first.add(insertAccountButton);
    first.add(deleteAccountButton);
    first.add(updateAccountButton);

    accountIDText = new JTextField(15);
    usernameText = new JTextField(15);
    passwordText = new JTextField(15);
    tsText = new JTextField(15);
    activeTSText = new JTextField(15);
    errorText = new JTextArea(5, 15);
    errorText.setEditable(false);

    JPanel second = new JPanel();
    second.setLayout(new GridLayout(6, 1));
    second.add(accountIDText);
    second.add(usernameText);
    second.add(passwordText);
    second.add(tsText);
    second.add(activeTSText);

    JPanel third = new JPanel();
    third.add(new JScrollPane(errorText));

    c.add(first);
    c.add(second);
    c.add(third);
    setSize(500, 500);
    show();
}

From source file:AppletViewer.java

/** Construct the GUI for an Applet Viewer */
AppletViewer(String appName) {/*from  w  ww .j  a  v  a 2s .  c  o m*/
    super();

    this.appName = appName;

    f = new JFrame("AppletViewer");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            f.setVisible(false);
            f.dispose();
            System.exit(0);
        }
    });
    Container cp = f.getContentPane();
    cp.setLayout(new BorderLayout());

    // Instantiate the AppletAdapter which gives us
    // AppletStub and AppletContext.
    if (aa == null)
        aa = new AppletAdapter();

    // The AppletAdapter also gives us showStatus.
    // Therefore, must add() it very early on, since the Applet's
    // Constructor or its init() may use showStatus()
    cp.add(BorderLayout.SOUTH, aa);

    showStatus("Loading Applet " + appName);

    loadApplet(appName, WIDTH, HEIGHT); // sets ac and ai
    if (ai == null)
        return;

    // Now right away, tell the Applet how to find showStatus et al.
    ai.setStub(aa);

    // Connect the Applet to the Frame.
    cp.add(BorderLayout.CENTER, ai);

    Dimension d = ai.getSize();
    d.height += aa.getSize().height;
    f.setSize(d);
    f.setVisible(true); // make the Frame and all in it appear

    showStatus("Applet " + appName + " loaded");

    // Here we pretend to be a browser!
    ai.init();
    ai.start();
}

From source file:com.anrisoftware.prefdialog.core.AbstractTitleField.java

private void setupContainer() {
    Container container = getContainer();
    container.removeAll();//www. java  2  s  .c om
    container.setLayout(layout);
    container.add(titleLabel, "0, 0");
    container.add(getComponent(), "0, 1");
}