Example usage for java.awt Container add

List of usage examples for java.awt Container add

Introduction

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

Prototype

public void add(Component comp, Object constraints) 

Source Link

Document

Adds the specified component to the end of this container.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box left = Box.createVerticalBox();
    left.add(Box.createVerticalStrut(30));
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;//from  ww  w . j  ava  2  s . c  o  m
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);

    left.add(Box.createGlue());

    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Color"));
    leftPanel.add(left, BorderLayout.CENTER);

    Box right = Box.createVerticalBox();
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Dashed"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Thick"));
    right.add(Box.createVerticalStrut(30));
    right.add(new JCheckBox("Rounded"));

    right.add(Box.createGlue());

    JPanel rightPanel = new JPanel(new BorderLayout());
    rightPanel.setBorder(new TitledBorder(new EtchedBorder(), "Line Properties"));
    rightPanel.add(right, BorderLayout.CENTER);

    Box top = Box.createHorizontalBox();
    top.add(leftPanel);
    top.add(Box.createHorizontalStrut(5));
    top.add(rightPanel);

    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);

    BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);

    content.setLayout(box);
    content.add(top);

    aWindow.setVisible(true);
}

From source file:InternalFrameEventTest.java

public static void main(String[] a) {

    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    JLayeredPane desktop = new JDesktopPane();
    desktop.setOpaque(false);//from w w  w .ja va2  s  . co m
    contentPane.add(desktop, BorderLayout.CENTER);
    desktop.add(createLayer("One"), JLayeredPane.POPUP_LAYER);
    desktop.add(createLayer("Two"), JLayeredPane.DEFAULT_LAYER);
    desktop.add(createLayer("Three"), JLayeredPane.PALETTE_LAYER);

    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:OverlaySample.java

public static void main(String args[]) {

    ActionListener generalActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JComponent comp = (JComponent) actionEvent.getSource();
            System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds());
        }//from ww w.ja  v a2  s  .com
    };

    ActionListener sizingActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setupButtons(actionEvent.getActionCommand());
        }
    };

    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    Object settings[][] = { { "Small", new Dimension(25, 25), Color.white },
            { "Medium", new Dimension(50, 50), Color.gray },
            { "Large", new Dimension(100, 100), Color.black } };
    JButton buttons[] = { smallButton, mediumButton, largeButton };

    for (int i = 0, n = settings.length; i < n; i++) {
        JButton button = buttons[i];
        button.addActionListener(generalActionListener);
        button.setActionCommand((String) settings[i][0]);
        button.setMaximumSize((Dimension) settings[i][1]);
        button.setBackground((Color) settings[i][2]);
        panel.add(button);
    }

    setupButtons(SET_CENTRAL);

    JPanel actionPanel = new JPanel();
    actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment"));
    String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED };
    for (int i = 0, n = actionSettings.length; i < n; i++) {
        JButton button = new JButton(actionSettings[i]);
        button.addActionListener(sizingActionListener);
        actionPanel.add(button);
    }

    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(actionPanel, BorderLayout.SOUTH);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:CaretSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    content.add(scrollPane, BorderLayout.CENTER);

    final JTextField dot = new JTextField();
    dot.setEditable(false);//www.  j a  v  a  2s  .c o  m
    JPanel dotPanel = new JPanel(new BorderLayout());
    dotPanel.add(new JLabel("Dot: "), BorderLayout.WEST);
    dotPanel.add(dot, BorderLayout.CENTER);
    content.add(dotPanel, BorderLayout.NORTH);

    final JTextField mark = new JTextField();
    mark.setEditable(false);
    JPanel markPanel = new JPanel(new BorderLayout());
    markPanel.add(new JLabel("Mark: "), BorderLayout.WEST);
    markPanel.add(mark, BorderLayout.CENTER);
    content.add(markPanel, BorderLayout.SOUTH);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            dot.setText("" + caretEvent.getDot());
            mark.setText("" + caretEvent.getMark());
        }
    };

    textArea.addCaretListener(listener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:MovingIconTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    JButton b;//from   w w  w  .jav a2s  . c  om
    Icon icon = new PieIcon(Color.red);
    b = new JButton("Default", icon);
    contentPane.add(b, BorderLayout.NORTH);
    b = new JButton("Text Left", icon);
    b.setHorizontalTextPosition(JButton.LEFT);
    contentPane.add(b, BorderLayout.SOUTH);
    b = new JButton("Text Up", icon);
    b.setHorizontalTextPosition(JButton.CENTER);
    b.setVerticalTextPosition(JButton.TOP);
    contentPane.add(b, BorderLayout.EAST);
    b = new JButton("Text Down", icon);
    b.setHorizontalTextPosition(JButton.CENTER);
    b.setVerticalTextPosition(JButton.BOTTOM);
    contentPane.add(b, BorderLayout.WEST);
    b = new JButton("Align Bottom Left", icon);
    b.setHorizontalAlignment(JButton.LEFT);
    b.setVerticalAlignment(JButton.BOTTOM);
    contentPane.add(b, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.show();
}

From source file:LoadSave.java

public static void main(String args[]) {
    final String filename = "text.out";
    JFrame frame = new JFrame("Loading/Saving Example");
    Container content = frame.getContentPane();

    final JTextField textField = new JTextField();
    content.add(textField, BorderLayout.NORTH);

    JPanel panel = new JPanel();

    Action loadAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            try {
                doLoadCommand(textField, filename);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();//w w  w .  ja va 2s.com
            }
        }
    };
    loadAction.putValue(Action.NAME, "Load");
    JButton loadButton = new JButton(loadAction);
    panel.add(loadButton);

    Action saveAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            try {
                doSaveCommand(textField, filename);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    };
    saveAction.putValue(Action.NAME, "Save");
    JButton saveButton = new JButton(saveAction);
    panel.add(saveButton);

    Action clearAction = new AbstractAction() {
        {
            putValue(Action.NAME, "Clear");
        }

        public void actionPerformed(ActionEvent e) {
            textField.setText("");
        }
    };
    JButton clearButton = new JButton(clearAction);
    panel.add(clearButton);

    content.add(panel, BorderLayout.SOUTH);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("CardLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel buttonPanel = new JPanel();
    JButton nextButton = new JButton("Next");
    buttonPanel.add(nextButton);/* w  ww .j a  v  a 2 s. co  m*/
    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    final JPanel cardPanel = new JPanel();
    final CardLayout cardLayout = new CardLayout();
    cardPanel.setLayout(cardLayout);

    for (int i = 1; i <= 5; i++) {
        JButton card = new JButton("Card " + i);
        card.setPreferredSize(new Dimension(200, 200));
        String cardName = "card" + 1;
        cardPanel.add(card, cardName);
    }
    contentPane.add(cardPanel, BorderLayout.CENTER);
    nextButton.addActionListener(e -> cardLayout.next(cardPanel));

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

From source file:InvokeExample.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();
    p.add(good);//from  w  w  w .j  av a 2  s  .  com
    p.add(bad);
    p.add(bad2);

    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(p, BorderLayout.CENTER);
    c.add(resultLabel, BorderLayout.SOUTH);

    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            Thread worker = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                    }

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            resultLabel.setText("Ready");
                            setEnabled(true);
                        }
                    });
                }
            };
            worker.start();
        }
    });

    bad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
            }
            resultLabel.setText("Ready");
            setEnabled(true);
        }
    });

    bad2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . . ");
            setEnabled(false);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(5000); // Dispatch thread is starving!
                    } catch (InterruptedException ex) {
                    }

                    resultLabel.setText("Ready");
                    setEnabled(true);
                }
            });
        }
    });

    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:EditComboBox.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JFrame frame = new JFrame("Editable JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JComboBox comboBox = new JComboBox(labels);
    comboBox.setMaximumRowCount(5);//from w  w  w  . j  a  v  a2 s  . c o m
    comboBox.setEditable(true);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            textArea.append("Selected: " + comboBox.getSelectedItem());
            textArea.append(", Position: " + comboBox.getSelectedIndex());
            textArea.append(System.getProperty("line.separator"));
        }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:GridBagLayoutFill.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;/*w ww .  j  a v a2  s  .  c o  m*/
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("This button's preferred width " + "is large because its text is long"), gbc);
    pane.add(new JButton("Small centered button"), gbc);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    pane.add(new JButton("Expands to fill column width"), gbc);
    f.setSize(400, 300);
    f.setVisible(true);
}