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:org.eclipse.swt.snippets.Snippet337.java

public static void main(String args[]) {
    display = new Display();
    EventQueue.invokeLater(() -> {
        JFrame mainFrame = new JFrame("Main Window");
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.addWindowListener(new Snippet337.CloseListener());
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        JButton launchBrowserButton = new JButton("Launch Browser");
        launchBrowserButton.addActionListener(e -> {
            JFrame childFrame = new JFrame();
            final Canvas canvas = new Canvas();
            childFrame.setSize(850, 650);
            childFrame.getContentPane().add(canvas);
            childFrame.setVisible(true);
            display.asyncExec(() -> {
                Shell shell = SWT_AWT.new_Shell(display, canvas);
                shell.setSize(800, 600);
                Browser browser = new Browser(shell, SWT.NONE);
                browser.setLayoutData(new GridData(GridData.FILL_BOTH));
                browser.setSize(800, 600);
                browser.setUrl("http://www.eclipse.org");
                shell.open();/*from w  w w .j ava  2  s  . co m*/
            });
        });

        mainPanel.add(new JTextField("a JTextField"));
        mainPanel.add(launchBrowserButton);
        mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
        mainFrame.pack();
        mainFrame.setVisible(true);
    });
    display.addListener(SWT.Close, event -> EventQueue.invokeLater(() -> {
        Frame[] frames = Frame.getFrames();
        for (int i = 0; i < frames.length; i++) {
            frames[i].dispose();
        }
    }));
    while (!display.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

From source file:com.adito.upgrade.Upgrade.java

/**
 * @param args//w  ww .j  a v a  2 s . co  m
 * @throws Exception on any error
 */
public static void main(String[] args) throws Exception {

    boolean gui = System.getProperty("os.name").toLowerCase().startsWith("windows")
            || System.getenv("DISPLAY") != null;

    if (args.length == 2 || !gui) {
        Upgrader upgrader = new CommandLineUpgrader(args);
        upgrader.upgrade();
    } else {
        JFrame f = new JFrame("0.1.16 to 0.2.5+ Upgrader");
        final Upgrader upgrader = new GUIUpgrader();
        f.setIconImage(new ImageIcon(Upgrade.class.getResource("upgrader-32x32.png")).getImage());
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add((JPanel) upgrader, BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        JPanel bp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        final JButton start = new JButton("Start");
        ;
        final JButton close = new JButton("Close");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    start.setEnabled(false);
                    close.setEnabled(false);
                    upgrader.upgrade();
                } catch (Exception ex) {
                    upgrader.error("Failed to upgrade.", ex);
                } finally {
                    close.setEnabled(true);
                }
            }
        });
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (close.isEnabled())
                    System.exit(0);
            }
        });
        bp.add(start);
        bp.add(close);
        f.getContentPane().add(bp, BorderLayout.SOUTH);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                if (close.isEnabled())
                    System.exit(0);
            }
        });
        f.setSize(new Dimension(480, 460));
        UIUtil.positionComponent(SwingConstants.CENTER, f);
        f.setVisible(true);
    }
}

From source file:JRadioButtonItemListener.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Grouping Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ItemListener itemListener = new ItemListener() {
        String lastSelected;/* w  w w.j  a  va 2s.co m*/

        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);
        }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addItemListener(itemListener);
    bRadioButton.addItemListener(itemListener);

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

From source file:Main.java

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

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    JTextField tfield1 = new JTextField(10);
    JTextField tfield2 = new JTextField(10);

    FocusListener tfieldListener = new FocusListener() {
        @Override//from   www.j  av a  2  s .  co m
        public void focusGained(FocusEvent fe) {
        }

        @Override
        public void focusLost(FocusEvent fe) {
            String num1 = tfield1.getText().trim();
            String num2 = tfield2.getText().trim();
            if (num1 == null || num1.equals(""))
                num1 = "0";
            if (num2 == null || num2.equals(""))
                num2 = "0";
            System.out.println(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2)));
        }
    };

    tfield1.addFocusListener(tfieldListener);
    tfield2.addFocusListener(tfieldListener);

    ((AbstractDocument) tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter());
    ((AbstractDocument) tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter());

    contentPane.add(tfield1);
    contentPane.add(tfield2);

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

From source file:DragDropTreeExample.java

public static void main(String[] args) {
    try {/*from  w  w  w  .j av  a2 s  . co m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    final JFrame f = new JFrame("FileTree Drop Target Example");

    try {
        final FileTree tree = new FileTree("D:\\");

        // Add a drop target to the FileTree
        FileTreeDropTarget target = new FileTreeDropTarget(tree);

        tree.setEditable(true);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });

        JPanel panel = new JPanel();
        final JCheckBox editable = new JCheckBox("Editable");
        editable.setSelected(true);
        panel.add(editable);
        editable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEditable(editable.isSelected());
            }
        });

        final JCheckBox enabled = new JCheckBox("Enabled");
        enabled.setSelected(true);
        panel.add(enabled);
        enabled.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEnabled(enabled.isSelected());
            }
        });

        f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
        f.getContentPane().add(panel, BorderLayout.SOUTH);
        f.setSize(500, 400);
        f.setVisible(true);
    } catch (Exception e) {
        System.out.println("Failed to build GUI: " + e);
    }
}

From source file:DragDropTreeExample.java

public static void main(String[] args) {
    try {/* w w  w  . j a  v a  2 s  .  c o  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    final JFrame f = new JFrame("FileTree Drop and Drop Example");
    try {
        final FileTree tree = new FileTree("D:\\");

        // Add a drop target to the FileTree
        FileTreeDropTarget target = new FileTreeDropTarget(tree);

        // Add a drag source to the FileTree
        FileTreeDragSource source = new FileTreeDragSource(tree);

        tree.setEditable(true);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });

        JPanel panel = new JPanel();
        final JCheckBox editable = new JCheckBox("Editable");
        editable.setSelected(true);
        panel.add(editable);
        editable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEditable(editable.isSelected());
            }
        });

        final JCheckBox enabled = new JCheckBox("Enabled");
        enabled.setSelected(true);
        panel.add(enabled);
        enabled.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEnabled(enabled.isSelected());
            }
        });

        f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
        f.getContentPane().add(panel, BorderLayout.SOUTH);
        f.setSize(500, 400);
        f.setVisible(true);
    } catch (Exception e) {
        System.out.println("Failed to build GUI: " + e);
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);/*from  w w  w . ja  v a2  s  .  c o m*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton redButton = new JButton("Red");
    JButton greenButton = new JButton("Green");
    JButton blueButton = new JButton("Blue");
    class Listener extends JPanel implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton) {
                color = Color.red;
                redButton.setBackground(color);
                panel.setBackground(color);
            } else if (event.getSource() == greenButton) {
                color = Color.green;
                greenButton.setBackground(color);
                panel.setBackground(color);
            } else {
                color = Color.blue;
                blueButton.setBackground(color);
                panel.setBackground(color);
            }
            setBackground(color);
            repaint();
        }
    }
    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());
    panel.add(redButton);
    panel.add(greenButton);
    panel.add(blueButton);
    frame.add(panel);
    frame.setVisible(true);
}

From source file:lu.lippmann.cdb.graph.GraphViewImpl.java

/**
 * /*from  w  w w. j  a  v  a 2  s .  co  m*/
 * @param args
 * @throws UnsupportedLookAndFeelException
 */
public static final void main(String[] args) throws UnsupportedLookAndFeelException {

    LookAndFeelUtil.init();

    final JXFrame f = new JXFrame();
    f.setDefaultCloseOperation(JXFrame.EXIT_ON_CLOSE);

    final GraphView view = new GraphViewImpl(new EventPublisherBushImpl(), new CommandDispatcherBushImpl());

    view.setViewMode(ViewMode.Add);
    view.init();

    final int panelWidth = 800;
    final int panelHeigth = 800;

    final VisualizationViewer<CNode, CEdge> vv = view.getVisualisationViewer();

    vv.setPreferredSize(new Dimension(panelWidth, panelHeigth));

    f.setSize(new Dimension(panelWidth, panelHeigth));
    LogoHelper.setLogo(f);
    f.setTitle("GraphView test");
    //f.setLayout(new BorderLayout());

    final GraphWithOperations gwo = new GraphWithOperations();
    final CGraph cg = new CGraph();
    final FRLayout<CNode, CEdge> layout = new FRLayout<CNode, CEdge>(gwo);
    cg.setInternalLayout(layout);
    view.setCGraph(cg);

    /*
    final JRadioButton hyperView = new JRadioButton("Hyperbolic View");
      hyperView.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent e) {
       ((GraphViewImpl)view).lensSupport.activate(e.getStateChange() == ItemEvent.SELECTED);
    }
      });
     */

    CadralGraphMouse gm = ((CadralGraphMouse) view.getVisualisationViewer().getGraphMouse());
    gm.setClickedGraph(gwo);

    final GraphWithOperations g = (GraphWithOperations) view.getVisualisationViewer().getModel()
            .getGraphLayout().getGraph();
    final CNode n1 = new CNode("N1");
    g.addVertex(n1);
    layout.setLocation(n1, new Point2D.Double(400, 400));
    /*
    final CNode n2 = new CNode("N2");
    g.addVertex(n2);
    final CNode n3 = new CNode("N3");
    g.addVertex(n3);
    g.addEdge(new CEdge("E1"),n1,n2,EdgeType.DIRECTED);
    layout.setLocation(n1,new Point2D.Double(250,250));
    layout.setLocation(n3,new Point2D.Double(100,250));
     */
    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });
    JPanel controls = new JPanel();
    JPanel zoomControls = new JPanel(new GridLayout(2, 1));
    zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
    JPanel hyperControls = new JPanel(new GridLayout(3, 2));
    hyperControls.setBorder(BorderFactory.createTitledBorder("Examiner Lens"));
    zoomControls.add(plus);
    zoomControls.add(minus);
    JPanel modeControls = new JPanel(new BorderLayout());
    modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
    modeControls.add(gm.getModeComboBox());
    //hyperControls.add(hyperView);

    Container content = f.getContentPane();
    controls.add(zoomControls);
    // controls.add(hyperControls);
    controls.add(modeControls);
    content.add(controls, BorderLayout.SOUTH);

    //GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
    content.add(view.asComponent());

    //f.add(view.asComponent());
    f.setVisible(true);
    f.pack();
}

From source file:com.joey.software.regionSelectionToolkit.controlers.ImageProfileTool.java

public static void main(String[] input) {
    try {/*from  ww  w. j  a v  a  2 s.  co  m*/
        ROIPanel pan = new ROIPanel(false);

        // BufferedImage img = ImageOperations.getBi(600);

        BufferedImage img = ImageOperations.getGrayTestImage(600, 600, 3);
        // ImageOperations.fillWithRandomColorSquares(5, 5, img);
        // ImageOperations.fillWithRandomColors(img);
        pan.setImage(img);

        ImageProfileTool tool = new ImageProfileTool(pan);

        tool.setListening(true);
        tool.setPanel(pan);

        tool.setDataLength(600);
        tool.setDataRange(new float[] { 0, 600 });

        JPanel holder = new JPanel(new GridLayout(3, 1));
        holder.add(pan);
        holder.add(tool.getChartHolderPanel());
        holder.add(tool.getControls());
        FrameFactroy.getFrame(holder);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    final DefaultListModel<String> model = new DefaultListModel<>();
    final JList<String> list = new JList<>(model);
    JFrame f = new JFrame();

    model.addElement("A");
    model.addElement("B");
    model.addElement("C");
    model.addElement("D");
    model.addElement("E");

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int index = list.locationToIndex(e.getPoint());
                Object item = model.getElementAt(index);
                String text = JOptionPane.showInputDialog("Rename item", item);
                String newitem = "";
                if (text != null)
                    newitem = text.trim();
                else
                    return;

                if (!newitem.isEmpty()) {
                    model.remove(index);
                    model.add(index, newitem);
                    ListSelectionModel selmodel = list.getSelectionModel();
                    selmodel.setLeadSelectionIndex(index);
                }//from   ww w  . j  a va 2s .  c  o m
            }
        }
    });
    leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    leftPanel.add(new JScrollPane(list));

    JButton removeall = new JButton("Remove All");
    JButton add = new JButton("Add");
    JButton rename = new JButton("Rename");
    JButton delete = new JButton("Delete");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = JOptionPane.showInputDialog("Add a new item");
            String item = null;

            if (text != null)
                item = text.trim();
            else
                return;

            if (!item.isEmpty())
                model.addElement(item);
        }
    });

    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index >= 0)
                model.remove(index);
        }

    });

    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel selmodel = list.getSelectionModel();
            int index = selmodel.getMinSelectionIndex();
            if (index == -1)
                return;
            Object item = model.getElementAt(index);
            String text = JOptionPane.showInputDialog("Rename item", item);
            String newitem = null;

            if (text != null) {
                newitem = text.trim();
            } else
                return;

            if (!newitem.isEmpty()) {
                model.remove(index);
                model.add(index, newitem);
            }
        }
    });

    removeall.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.clear();
        }
    });

    rightPanel.add(add);
    rightPanel.add(rename);
    rightPanel.add(delete);
    rightPanel.add(removeall);

    rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

    panel.add(leftPanel);
    panel.add(rightPanel);

    f.add(panel);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}