Example usage for javax.swing JPanel add

List of usage examples for javax.swing JPanel add

Introduction

In this page you can find the example usage for javax.swing JPanel add.

Prototype

public Component add(String name, Component comp) 

Source Link

Document

Adds the specified component to this container.

Usage

From source file:GridBagWithAnchor.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of anchor constraints");
    JPanel p = new JPanel(new GridBagLayout());

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;/*from w  w  w.jav  a  2s .c o m*/
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.anchor = GridBagConstraints.NORTH; // place component on the North
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.SOUTHWEST;
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.CENTER; // remember to rest to center
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support !!!"), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();

    JButton button = new JButton();

    Main f = null;/*www  .j  a va  2 s  .  c  o m*/
    f = new Main();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(700, 400);

    panel1.setLayout(new BorderLayout());
    panel1.setForeground(Color.white);
    button.setText("Convert");
    panel1.add(button, BorderLayout.SOUTH);

    f.setContentPane(panel1);
    f.setVisible(true);

    f1 = new Main();

    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setSize(457, 100);
    f1.setTitle("Conversion Progress");
    f1.setLocationRelativeTo(null);

    panel2.setLayout(new BorderLayout());
    panel2.setForeground(Color.white);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setValue(35);
    progressBar.setStringPainted(true);

    panel2.add(progressBar, BorderLayout.SOUTH);

    f1.setContentPane(panel2);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            f1.setVisible(true);
        }
    });
}

From source file:JTextFieldSample.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "TextField Listener Example" : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();

    JPanel namePanel = new JPanel(new BorderLayout());
    JLabel nameLabel = new JLabel("Name: ");
    nameLabel.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField nameTextField = new JTextField();
    nameLabel.setLabelFor(nameTextField);
    namePanel.add(nameLabel, BorderLayout.WEST);
    namePanel.add(nameTextField, BorderLayout.CENTER);
    content.add(namePanel, BorderLayout.NORTH);

    JPanel cityPanel = new JPanel(new BorderLayout());
    JLabel cityLabel = new JLabel("City: ");
    cityLabel.setDisplayedMnemonic(KeyEvent.VK_C);
    JTextField cityTextField = new JTextField();
    cityLabel.setLabelFor(cityTextField);
    cityPanel.add(cityLabel, BorderLayout.WEST);
    cityPanel.add(cityTextField, BorderLayout.CENTER);
    content.add(cityPanel, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
        }/*  ww w  .  java2  s  . com*/
    };
    nameTextField.setActionCommand("Yo");
    nameTextField.addActionListener(actionListener);
    cityTextField.addActionListener(actionListener);

    KeyListener keyListener = new KeyListener() {
        public void keyPressed(KeyEvent keyEvent) {
            printIt("Pressed", keyEvent);
        }

        public void keyReleased(KeyEvent keyEvent) {
            printIt("Released", keyEvent);
        }

        public void keyTyped(KeyEvent keyEvent) {
            printIt("Typed", keyEvent);
        }

        private void printIt(String title, KeyEvent keyEvent) {
            int keyCode = keyEvent.getKeyCode();
            String keyText = KeyEvent.getKeyText(keyCode);
            System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar());
        }
    };
    nameTextField.addKeyListener(keyListener);
    cityTextField.addKeyListener(keyListener);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                Runnable runnable = new Runnable() {
                    public void run() {
                        JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog",
                                JOptionPane.ERROR_MESSAGE);
                    }
                };
                SwingUtilities.invokeLater(runnable);
                return false;
            } else {
                return true;
            }
        }
    };
    nameTextField.setInputVerifier(verifier);
    cityTextField.setInputVerifier(verifier);

    DocumentListener documentListener = new DocumentListener() {
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            if (type.equals(DocumentEvent.EventType.CHANGE)) {
                typeString = "Change";
            } else if (type.equals(DocumentEvent.EventType.INSERT)) {
                typeString = "Insert";
            } else if (type.equals(DocumentEvent.EventType.REMOVE)) {
                typeString = "Remove";
            }
            System.out.print("Type  :   " + typeString + " / ");
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            try {
                System.out.println("Contents: " + source.getText(0, length));
            } catch (BadLocationException badLocationException) {
                System.out.println("Contents: Unknown");
            }
        }
    };
    nameTextField.getDocument().addDocumentListener(documentListener);
    cityTextField.getDocument().addDocumentListener(documentListener);

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

From source file:GridBagWithContaints.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;/*  www .jav a 2  s. co  m*/
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    Integer[][] board = { { 1, 1, 1, 1, 2, 0, 0, 0, 0 }, { 0, 0, 5, 0, 1, 0, 0, 2, 4 },
            { 1, 0, 0, 4, 0, 0, 0, 3, 8 }, { 0, 0, 0, 6, 1, 0, 0, 3, 7 }, { 0, 0, 4, 5, 3, 8, 9, 4, 0 },
            { 8, 0, 0, 0, 1, 7, 0, 4, 0 }, { 7, 4, 0, 0, 1, 6, 0, 3, 1 }, { 6, 1, 0, 0, 1, 0, 3, 3, 0 },
            { 0, 0, 0, 0, 1, 1, 1, 1, 0 } };

    String col[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    JTable table = new JTable(board, col);
    panel.add(table, BorderLayout.CENTER);

    frame.add(panel);//from ww w.  java  2  s  . c o m
    frame.setSize(800, 500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:brainflow.app.presentation.controls.FileObjectGroupSelector.java

public static void main(String[] args) {
    try {/*from   ww  w .  j  a  va 2 s . c  o m*/
        com.jidesoft.utils.Lm.verifyLicense("UIN", "BrainFlow", "S5XiLlHH0VReaWDo84sDmzPxpMJvjP3");
        //com.jidesoft.plaf.LookAndFeelFactory.installDefaultLookAndFeel();
        //LookAndFeelFactory.installJideExtension(LookAndFeelFactory.OFFICE2007_STYLE);
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
        JFrame jf = new JFrame();
        FileObjectGroupSelector selector = new FileObjectGroupSelector(
                VFS.getManager().resolveFile("c:/javacode"));

        ButtonPanel buttonPanel = new ButtonPanel(SwingConstants.RIGHT);
        buttonPanel.setSizeConstraint(ButtonPanel.NO_LESS_THAN);

        JButton okButton = new JButton("OK");
        JButton resetButton = new JButton("Cancel");

        buttonPanel.addButton(okButton, ButtonPanel.AFFIRMATIVE_BUTTON);
        buttonPanel.addButton(resetButton, ButtonPanel.CANCEL_BUTTON);

        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 8, 8, 8));

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(selector, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        panel.setMinimumSize(new Dimension(800, 100));
        jf.add(panel, BorderLayout.CENTER);
        jf.pack();
        jf.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();
    JTextField tf = new JTextField("is");
    String word = "";
    Highlighter highlighter = new UnderlineHighlighter(null);

    textPane.setHighlighter(highlighter);
    textPane.setText("This is a test");
    panel.setLayout(new BorderLayout());
    panel.add(new JLabel("Enter word, then press ENTER key: "), "West");
    panel.add(tf, "Center");

    final WordSearcher searcher = new WordSearcher(textPane);
    tf.addActionListener(e -> {/*  www .j av a2s.c  o m*/
        String w = tf.getText().trim();
        int offset = searcher.search(w);
        if (offset == -1) {
            return;
        }
        try {
            textPane.scrollRectToVisible(textPane.modelToView(offset));
        } catch (BadLocationException ex) {
        }

    });
    textPane.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void removeUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void changedUpdate(DocumentEvent evt) {
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(panel, "South");
    f.add(new JScrollPane(textPane), "Center");
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:ca.sqlpower.wabit.swingui.enterprise.UserPanel.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                WabitWorkspace p = new WabitWorkspace();
                p.setUUID("system");

                // Add data sources to workspace
                DataSourceCollection<SPDataSource> plini = new PlDotIni();
                plini.read(new File(System.getProperty("user.home"), "pl.ini"));
                List<SPDataSource> dataSources = plini.getConnections();
                for (int i = 0; i < 10 && i < dataSources.size(); i++) {
                    p.addDataSource(new WabitDataSource(dataSources.get(i)));
                }/*from w w  w. jav  a 2s. c o  m*/

                // Add layouts to workspace
                Report layout = new Report("Example Layout");
                p.addReport(layout);
                Page page = layout.getPage();
                page.addContentBox(new ContentBox());
                page.addGuide(new Guide(Axis.HORIZONTAL, 123));
                page.addContentBox(new ContentBox());

                // dd a report task
                ReportTask task = new ReportTask();
                task.setReport(layout);
                p.addReportTask(task);

                User user = new User("admin", "admin");
                user.setParent(p);
                Group group = new Group("Admins");
                group.setParent(p);
                group.addMember(new GroupMember(user));

                Group group2 = new Group("Other Group");
                group2.setParent(p);

                p.addUser(user);
                p.addGroup(group);
                p.addGroup(group2);

                UserPanel panel = new UserPanel(user);

                UserPanel panel2 = new UserPanel(user);

                JFrame f = new JFrame("TEST PANEL");
                JPanel outerPanel = new JPanel(new BorderLayout());
                outerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                outerPanel.add(panel.getPanel(), BorderLayout.CENTER);
                f.setContentPane(outerPanel);
                f.pack();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);

                JFrame f2 = new JFrame("TEST PANEL");
                JPanel outerPanel2 = new JPanel(new BorderLayout());
                outerPanel2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                outerPanel2.add(panel2.getPanel(), BorderLayout.CENTER);
                f2.setContentPane(outerPanel2);
                f2.pack();
                f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f2.setVisible(true);

            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:Main.java

public static void main(String... args) {
    JPanel contentPane;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    JPanel centerPanel = new JPanel();
    centerPanel.setOpaque(true);//from   www.j  av a 2  s.c o m
    centerPanel.setBackground(Color.CYAN);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.weightx = 1.0;
    gbc.weighty = 0.9;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.BOTH;

    contentPane.add(centerPanel, gbc);

    JButton leftButton = new JButton("Left");
    JButton rightButton = new JButton("Right");
    gbc.gridwidth = 1;
    gbc.gridy = 1;
    gbc.weightx = 0.3;
    gbc.weighty = 0.1;

    contentPane.add(leftButton, gbc);

    gbc.gridx = 1;
    gbc.weightx = 0.7;
    gbc.weighty = 0.1;

    contentPane.add(rightButton, gbc);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}

From source file:DateTimeEditor.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(new BorderLayout());
    JTextField field = new JTextField(20);
    Spinner spinner = new Spinner();

    panel.add(field, "Center");
    panel.add(spinner, "East");

    Dimension dim = frame.getToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2 - frame.getHeight() / 2);
    frame.pack();/*from  w ww  . j  ava2 s  . co  m*/
    frame.show();
}