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:Main.java

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

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3, 0));

    for (int i = 1; i <= 9; i++) {
        buttonPanel.add(new JButton("Button  " + i));
    }/*  w w  w  .  j  a  v a 2 s .c o  m*/

    contentPane.add(buttonPanel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

From source file:SwingToolBarSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand());
        }/*from  w w  w .ja  v a 2  s  .c  o  m*/
    };

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

    JToolBar toolbar = new JToolBar();
    toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);

    for (int i = 0, n = buttonColors.length; i < n; i++) {
        Object color[] = buttonColors[i];
        if (color == null) {
            toolbar.addSeparator();
        } else {
            Icon icon = new DiamondIcon((Color) color[COLOR_POSITION], true, 20, 20);
            JButton button = new JButton(icon);
            button.setActionCommand((String) color[STRING_POSITION]);
            button.addActionListener(actionListener);
            toolbar.add(button);
        }
    }

    Action action = new ActionMenuSample.ShowAction(frame);
    toolbar.add(action);

    Container contentPane = frame.getContentPane();
    contentPane.add(toolbar, BorderLayout.NORTH);
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

From source file:CutPasteSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Cut/Paste Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();

    JTextField textField = new JTextField();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);

    content.add(textField, BorderLayout.NORTH);
    content.add(scrollPane, BorderLayout.CENTER);

    Action actions[] = textField.getActions();

    Action cutAction = TextUtilities.findAction(actions, DefaultEditorKit.cutAction);
    Action copyAction = TextUtilities.findAction(actions, DefaultEditorKit.copyAction);
    Action pasteAction = TextUtilities.findAction(actions, DefaultEditorKit.pasteAction);

    JPanel panel = new JPanel();
    content.add(panel, BorderLayout.SOUTH);

    JButton cutButton = new JButton(cutAction);
    cutButton.setText("Cut");
    panel.add(cutButton);/*from  w  w w.ja  v a  2  s  .co m*/

    JButton copyButton = new JButton(copyAction);
    copyButton.setText("Copy");
    panel.add(copyButton);

    JButton pasteButton = new JButton(pasteAction);
    pasteButton.setText("Paste");
    panel.add(pasteButton);

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

From source file:ActionsMenuBar.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("TextAction Usage");
    Container contentPane = frame.getContentPane();
    final JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    final JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);//from www . j a  va 2s  . co m

    ActionListener actionListener = new ActionListener() {
        JTextComponent component;

        public void actionPerformed(ActionEvent actionEvent) {
            // Determine which component selected
            String command = actionEvent.getActionCommand();
            if (command.equals("JTextField")) {
                component = new JTextField();
            } else if (command.equals("JPasswordField")) {
                component = new JPasswordField();
            } else if (command.equals("JTextArea")) {
                component = new JTextArea();
            } else if (command.equals("JTextPane")) {
                component = new JTextPane();
            } else {
                component = new JEditorPane();
            }
            scrollPane.setViewportView(component);
            // Process action list
            Action actions[] = component.getActions();
            menuBar.removeAll();
            menuBar.revalidate();
            JMenu menu = null;
            for (int i = 0, n = actions.length; i < n; i++) {
                if ((i % 10) == 0) {
                    menu = new JMenu("From " + i);
                    menuBar.add(menu);
                }
                menu.add(actions[i]);
            }
            menuBar.revalidate();
        }
    };

    String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" };
    final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components,
            "Pick to List Actions", actionListener);
    contentPane.add(componentsContainer, BorderLayout.WEST);

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

From source file:CustomBorderSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JFrame frame = new JFrame("Custom Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    JList jlist = new JList(labels);
    ListCellRenderer renderer = new FocusedTitleListCellRenderer();
    jlist.setCellRenderer(renderer);/*from w w w.  j a v a2s . c o m*/
    JScrollPane sp = new JScrollPane(jlist);
    contentPane.add(sp, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:GroupActionRadio.java

public static void main(String args[]) {

    String title = (args.length == 0 ? "Grouping Example" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Slice Parts
    ActionListener sliceActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            System.out.println("Selected: " + aButton.getText());
        }//from  w  w  w .ja  va 2  s  . c o m
    };
    Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(sliceOptions, "Slice Count",
            sliceActionListener);

    // Crust Parts
    ActionListener crustActionListener = new ActionListener() {
        String lastSelected;

        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            String label = aButton.getText();
            String msgStart;
            if (label.equals(lastSelected)) {
                msgStart = "Reselected: ";
            } else {
                msgStart = "Selected: ";
            }
            lastSelected = label;
            System.out.println(msgStart + label);
        }
    };
    ItemListener itemListener = new ItemListener() {
        String lastSelected;

        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String label = aButton.getText();
            String msgStart;
            if (state == ItemEvent.SELECTED) {
                if (label.equals(lastSelected)) {
                    msgStart = "Reselected -> ";
                } else {
                    msgStart = "Selected -> ";
                }
                lastSelected = label;
            } else {
                msgStart = "Deselected -> ";
            }
            System.out.println(msgStart + label);
        }
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changEvent) {
            AbstractButton aButton = (AbstractButton) changEvent.getSource();
            ButtonModel aModel = aButton.getModel();
            boolean armed = aModel.isArmed();
            boolean pressed = aModel.isPressed();
            boolean selected = aModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    final Container crustContainer = RadioButtonUtils.createRadioButtonGrouping(crustOptions, "Crust Type",
            crustActionListener, itemListener, changeListener);

    // Button Parts
    ActionListener buttonActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Enumeration selected = RadioButtonUtils.getSelectedElements(crustContainer);
            while (selected.hasMoreElements()) {
                System.out.println("Selected -> " + selected.nextElement());
            }
        }
    };
    JButton button = new JButton("Order Pizza");
    button.addActionListener(buttonActionListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(sliceContainer, BorderLayout.WEST);
    contentPane.add(crustContainer, BorderLayout.EAST);
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:LoadSync.java

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

    final JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);//from   w  ww .j a  v a  2 s.  c o  m
    JScrollPane scrollPane = new JScrollPane(editorPane);
    content.add(scrollPane, BorderLayout.CENTER);

    editorPane.setEditorKit(new HTMLEditorKit());

    JPanel panel = new JPanel();

    // Setup actions
    Action loadAction = new AbstractAction() {
        {
            putValue(Action.NAME, "Load");
        }

        public void actionPerformed(ActionEvent e) {
            doLoadCommand(editorPane, filename);
        }
    };
    JButton loadButton = new JButton(loadAction);
    panel.add(loadButton);

    content.add(panel, BorderLayout.SOUTH);

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

From source file:MessagePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Message Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Pop it");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();

            /*//from w w  w . j  a  v a 2  s.  c o m
             * // String msg = "this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message"; String
             * msg = " <html>this is a really long message <br> this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message";
             * JOptionPane.showMessageDialog(source, msg); JOptionPane
             * optionPane = OptionPaneUtils.getNarrowOptionPane(72);
             * optionPane.setMessage(msg);
             * optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
             * JDialog dialog = optionPane.createDialog(source, "Width 72");
             * dialog.show(); int selection =
             * OptionPaneUtils.getSelection(optionPane);
             * JOptionPane.showMessageDialog(source, msg); String
             * multiLineMsg[] = {"Hello", "World"};
             * JOptionPane.showMessageDialog(source, multiLineMsg);
             * 
             * Object complexMsg[] = {"Above Message", new
             * DiamondIcon(Color.red), new JButton ("Hello"), new JSlider(),
             * new DiamondIcon(Color.blue), "Below Message"};
             * JOptionPane.showInputDialog(source, complexMsg); JOptionPane
             * optionPane = new JOptionPane(); JSlider slider =
             * OptionPaneUtils.getSlider(optionPane);
             * optionPane.setMessage(new Object[] {"Select a value: " ,
             * slider});
             * optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
             * optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
             * JDialog dialog = optionPane.createDialog(source, "My
             * Slider"); dialog.show(); System.out.println ("Input: " +
             * optionPane.getInputValue());
             */
            JOptionPane optionPane = new JOptionPane();
            optionPane.setMessage("I got an icon and a text label");
            optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
            Icon icon = new DiamondIcon(Color.blue);
            JButton jButton = OptionPaneUtils.getButton(optionPane, "OK", icon);
            optionPane.setOptions(new Object[] { jButton });
            JDialog dialog = optionPane.createDialog(source, "Icon/Text Button");
            dialog.show();
        }
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:ListenerSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Offset Example");
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    final Document document = textArea.getDocument();
    document.addDocumentListener(new MyListener());

    content.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(250, 150);/*from   w  w  w  .ja  v a2  s.  co m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JEditorPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JEditorPane editor = new JEditorPane("text/html",
            "<H3>Help</H3><center><IMG src=file:///c:/a.jpg></center><li>One<li><i>Two</i><li><u>Three</u>");
    editor.setEditable(false);/*from   w w  w.  j  a  v  a2  s .  c o  m*/
    JScrollPane scrollPane = new JScrollPane(editor);
    content.add(scrollPane, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}