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(Component comp) 

Source Link

Document

Appends 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();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton(new AbstractAction("Update") {
        @Override/*from  w  ww .java  2 s  .c  om*/
        public void actionPerformed(ActionEvent ae) {

            StyledDocument doc = (StyledDocument) textPane.getDocument();
            SimpleAttributeSet style = new SimpleAttributeSet();
            StyleConstants.setFontFamily(style, "Serif");
            StyleConstants.setFontSize(style, size++);
            try {
                doc.insertString(doc.getLength(), " one two three", style);
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            frame.pack();
        }

    }));
    frame.add(buttonPanel, BorderLayout.NORTH);
    textPane.setText("this is a test.");
    textPane.setBorder(new LineBorder(Color.BLACK));

    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int SIZE = 14;
    String FONT = "Dialog";

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane tp = new JTextPane();
    tp.setFont(new Font(FONT, Font.PLAIN, SIZE));
    tp.setPreferredSize(new Dimension(400, 300));
    StyledDocument doc = tp.getStyledDocument();
    Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style boldStyle = doc.addStyle("bold", defaultStyle);
    StyleConstants.setBold(boldStyle, true);
    String boldText = "this is bold test";
    String plainText = "this is plain.";

    doc.insertString(doc.getLength(), boldText, boldStyle);
    doc.insertString(doc.getLength(), plainText, defaultStyle);

    JPanel panel = new JPanel();
    panel.add(tp);

    JComboBox<String> zoomCombo = new JComboBox<String>(
            new String[] { "0.75", "1.00", "1.50", "1.75", "2.00" });
    zoomCombo.addActionListener(e -> {
        String s = (String) zoomCombo.getSelectedItem();
        double scale = new Double(s).doubleValue();
        int size = (int) (SIZE * scale);
        tp.setFont(new Font(FONT, Font.PLAIN, size));
    });//  w w w .  j  a  va2 s .  c o  m
    zoomCombo.setSelectedItem("1.00");
    JPanel optionsPanel = new JPanel();
    optionsPanel.add(zoomCombo);
    panel.setBackground(Color.WHITE);
    frame.add(panel, BorderLayout.CENTER);
    frame.add(optionsPanel, BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:TextForm.java

public static void main(String[] args) {
    String[] labels = { "First Name", "Middle Initial", "Last Name", "Age" };
    char[] mnemonics = { 'F', 'M', 'L', 'A' };
    int[] widths = { 15, 1, 15, 3 };
    String[] descs = { "First Name", "Middle Initial", "Last Name", "Age" };

    final TextForm form = new TextForm(labels, mnemonics, widths, descs);

    JButton submit = new JButton("Submit Form");

    submit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(form.getText(0) + " " + form.getText(1) + ". " + form.getText(2) + ", age "
                    + form.getText(3));/*from w w  w .j a  va2s .  c om*/
        }
    });

    JFrame f = new JFrame("Text Form Example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(form, BorderLayout.NORTH);
    JPanel p = new JPanel();
    p.add(submit);
    f.getContentPane().add(p, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setBorder(new TitledBorder("Border Layout"));

    JPanel labels = new JPanel();
    labels.setBorder(new TitledBorder("Flow Layout"));
    labels.add(new JLabel("Label 1"));
    labels.add(new JLabel("Label 2"));

    gui.add(labels, BorderLayout.NORTH);
    gui.add(new JButton("Button"), BorderLayout.SOUTH);

    JOptionPane.showMessageDialog(null, gui);
}

From source file:MainClass.java

public static void main(String argv[]) {

    java.net.URL u = null;//  w w w  .j  av  a2 s. co  m
    try {
        u = new java.net.URL("http://www.java2s.com/");
    } catch (java.net.MalformedURLException ignored) {
    }

    JFormattedTextField ftf1 = new JFormattedTextField(u);
    JFormattedTextField ftf2 = new JFormattedTextField(u);

    ftf2.setInputVerifier(new InputVerifier() {
        public boolean verify(JComponent input) {
            if (!(input instanceof JFormattedTextField))
                return true;
            return ((JFormattedTextField) input).isEditValid();
        }
    });

    JPanel p = new JPanel(new GridLayout(0, 2, 3, 8));
    p.add(new JLabel("plain JFormattedTextField:"));
    p.add(ftf1);
    p.add(new JLabel("FTF with InputVerifier:"));
    p.add(ftf2);
    p.add(new JLabel("plain JTextField:"));
    p.add(new JTextField(u.toString()));

    JFrame f = new JFrame("MainClass");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North");
    f.getContentPane().add(p, "Center");
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String s[]) {
    JWindow win = new JWindow();

    JPanel pan = new JPanel();
    win.add(pan, "Center");

    pan.setLayout(new FlowLayout());
    pan.add(new JButton("Hello"));

    win.setSize(200, 200);/*from w ww .j a  va2s . c o  m*/
    win.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  w w  w. ja  v  a2 s.  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);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL urlImage1 = new URL("http://www.java2s.com/style/download.png");

    final Image fgImage = ImageIO.read(urlImage1);
    int w = fgImage.getWidth(null);
    int h = fgImage.getHeight(null);
    final BufferedImage bgImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    final BufferedImage finalImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = finalImage.createGraphics();
    g.drawImage(bgImage, 0, 0, null);/*w w  w.j  ava 2  s . com*/
    g.drawImage(fgImage, 0, 0, null);
    g.dispose();

    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel(new GridLayout(1, 0, 5, 5));

            gui.add(new JLabel(new ImageIcon(bgImage)));
            gui.add(new JLabel(new ImageIcon(fgImage)));
            gui.add(new JLabel(new ImageIcon(finalImage)));

            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws AWTException {
    Runnable r = new Runnable() {
        @Override/*from  www .ja  va  2s.  com*/
        public void run() {
            try {
                URL url = new URL("http://www.java2s.com/style/download.png");
                BufferedImage bi = ImageIO.read(url);
                JPanel gui = new JPanel(new GridLayout(1, 2, 2, 2));

                gui.add(new JLabel(new ImageIcon(bi)));
                gui.add(new JLabel(new ImageIcon(getFlippedImage(bi))));

                JOptionPane.showMessageDialog(null, gui);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:GlassExample.java

/** Construct a Splash screen with the given image */
public static void main(String[] args) {
    JFrame f = new JFrame("GlassPane");

    final JPanel p1 = new JPanel();
    p1.add(new JLabel("GlassPane Example"));
    JButton show = new JButton("Show");
    p1.add(show);/* ww w  .  j  a  v  a  2  s  .c  o  m*/
    p1.add(new JButton("No-op"));
    f.getContentPane().add(p1);

    final JPanel glass = (JPanel) f.getGlassPane();

    glass.setVisible(true);
    glass.setLayout(new GridBagLayout());
    JButton glassButton = new JButton("Hide");
    glass.add(glassButton);

    f.setSize(150, 80);
    f.setVisible(true);

    boolean debug = false;
    if (debug) {
        System.out.println("Button is " + glassButton);
        System.out.println("GlassPane is " + glass);
    }

    // Add actions to the buttons...

    // show button (re-)shows the glass pane.
    show.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            glass.setVisible(true);
            p1.repaint();
        }
    });
    // hide button hides the Glass Pane to show what's under.
    glassButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            glass.setVisible(false);
            p1.repaint();
        }
    });
}