Example usage for javax.swing JPanel JPanel

List of usage examples for javax.swing JPanel JPanel

Introduction

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

Prototype

public JPanel(boolean isDoubleBuffered) 

Source Link

Document

Creates a new JPanel with FlowLayout and the specified buffering strategy.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    textField.setHorizontalAlignment(JTextField.CENTER);

    label.setLabelFor(textField);//from w  w w. ja v  a2 s  .  co m
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String arg[]) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());

    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 1;//  w w w .  j a va  2s  .  co  m

    gb.add(content, gbc); // gbc is containing the GridBagConstraints
    frame.add(gb, BorderLayout.CENTER);

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 300);/* w w  w .  j a v a2 s.c om*/

    JPanel panel = new JPanel(new GridLayout(3, 1));
    JLabel label = new JLabel();
    JTextField tf = new JTextField();
    JButton b = new JButton("calc sting width");
    b.addActionListener(e -> {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        String text = tf.getText();
        int textWidth = fm.stringWidth(text);
        label.setText("text width for \"" + text + "\": " + textWidth);
    });
    panel.add(label);
    panel.add(tf);
    panel.add(b);
    frame.setContentPane(panel);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    String HTML = "<html>" + "<head>" + "<style type=text/css>" + "body {"
            + "  background-image: http://www.java2s.com/style/download.png;" + "  background-repeat:no-repeat;"
            + "  background-position:left top;" + "  background-attachment: scroll;" + "  color: #BBBBBB;" + "}"
            + "</style>" + "</head>" + "<body>" + "<h1>Heading 1</h1>";
    String PARAGRAPH = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";

    gui.setPreferredSize(new Dimension(400, 100));

    StringBuilder sb = new StringBuilder();
    sb.append(HTML);//from  w  w w .j a v  a 2  s.c o m
    for (int ii = 0; ii < 10; ii++) {
        sb.append("<h2>Header 2</h2>");
        sb.append(PARAGRAPH);
    }
    JEditorPane jep = new JEditorPane();
    jep.setOpaque(false);
    jep.setContentType("text/html");
    jep.setText(sb.toString());
    JScrollPane jsp = new JScrollPane(jep) {
        BufferedImage bg = new BufferedImage(350, 50, BufferedImage.TYPE_INT_RGB);

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bg, 0, 0, this);
        }
    };
    jsp.getViewport().setOpaque(false);
    gui.add(jsp);

    Main bih = new Main();
    JFrame f = new JFrame();
    f.add(gui);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);
    JTableHeader header = table.getTableHeader();

    JPanel container = new JPanel(new BorderLayout());

    // Add header in NORTH slot
    container.add(header, BorderLayout.NORTH);

    // Add table itself to CENTER slot
    container.add(table, BorderLayout.CENTER);
}

From source file:SwingHtmlLabel.java

public static void main(String argv[]) {
    JPanel p = new JPanel(new java.awt.GridLayout(0, 1));
    p.add(new JLabel(markup));
    p.add(new java.awt.Label(markup));

    JFrame f = new JFrame("SwingHtmlLabel");
    f.setContentPane(p);/*from  w w  w .j a  va  2s .co  m*/
    f.setSize(600, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

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

    JPanel buttonPanel = new JPanel(true);
    buttonPanel.addContainerListener(new ContainerListener() {
        public void componentAdded(ContainerEvent e) {
            displayMessage(" added to ", e);
        }//from  w  w w. j a  v  a2 s  .  co m

        public void componentRemoved(ContainerEvent e) {
            displayMessage(" removed from ", e);
        }

        void displayMessage(String action, ContainerEvent e) {
            System.out.println(((JButton) e.getChild()).getText() + " was" + action
                    + e.getContainer().getClass().getName());
        }
    });
    buttonPanel.add(new JButton("A"));

    frame.add(buttonPanel);

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

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout(2, 3));

    JPanel buttonConstrsint = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton getQuotesButton = new JButton("Load");
    buttonConstrsint.add(getQuotesButton);
    gui.add(buttonConstrsint, BorderLayout.NORTH);

    getQuotesButton.addActionListener(e -> {
        String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

        Object[][] data = { { "A", "B", "Snowboarding", new Integer(5), new Boolean(false) },
                { "C", "D", "Pool", new Integer(10), new Boolean(false) } };

        JTable table = new JTable(data, columnNames);

        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);

        gui.add(scrollPane, BorderLayout.CENTER);
        gui.revalidate();/*ww  w.j  ava 2 s .  c  om*/
        gui.repaint();
    });

    JOptionPane jOptionPane = new JOptionPane(gui);

    JDialog dialog = jOptionPane.createDialog(new JFrame(), "title");
    dialog.setSize(200, 200);
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("Hello");
    label.setOpaque(true);//from www.  ja v a2s .  co m
    label.setBackground(Color.red);

    JPanel bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.add(label, BorderLayout.LINE_END);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
    mainPanel.setPreferredSize(new Dimension(400, 400));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] arguments) {
    JPanel panel = new JPanel(new BorderLayout());
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);//from www  .  j av a2s .  c  o  m
    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);
    progressBar.setVisible(false);
    JButton loadButton = new JButton("Load memberlist");
    loadButton.setEnabled(true);
    loadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setVisible(true);
                    // do my stuff here...
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    progressBar.setVisible(false);
                }
            }).start();
        }
    });
    JPanel container = new JPanel(new FlowLayout());
    container.add(loadButton);
    container.add(progressBar);
    panel.add(container);
}