Example usage for javax.swing JFrame add

List of usage examples for javax.swing JFrame add

Introduction

In this page you can find the example usage for javax.swing JFrame 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[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);/*w w w  . j  ava  2 s.  c om*/
    frame.add(new JButton("1"));
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    double[][] weights = gbl.getLayoutWeights();
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < weights[i].length; j++) {
            weights[i][j] = 1;
        }
    }
    gbl.columnWeights = weights[0];
    gbl.rowWeights = weights[1];

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String[] args) {
    JFrame frame = new JFrame("Frame");
    JDialog dialog = new JDialog(frame, "Dialog");
    frame.add(new JLabel("Content"));
    frame.addMouseListener(new MouseAdapter() {
        @Override/*from  www  .j av  a 2s .c  o m*/
        public void mousePressed(MouseEvent arg0) {
            System.out.println("frame pressed");
            System.out.println("dialog focused " + dialog.isFocused());
            System.out.println("frame focused " + frame.isFocused());
            super.mousePressed(arg0);
        }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    dialog.add(new JLabel("Content"));
    dialog.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            dialog.requestFocus();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable t = new JTable(10, 1);
    frame.add(new JScrollPane(t));

    t.getSelectionModel().clearSelection();
    t.getSelectionModel().addSelectionInterval(5, 6);
    t.getSelectionModel().addSelectionInterval(8, 8);
    frame.pack();//from  ww  w .java 2 s .com
    frame.setVisible(true);
}

From source file:Colors.java

public static void main(String[] args) {

    JFrame frame = new JFrame("Colors");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Colors());
    frame.setSize(360, 300);//w  ww .  ja v a  2s. com
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new ButtonDemo());
    f.setSize(500, 500);/*from www. jav  a  2 s. c om*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }/*from w w  w.  j  av a2  s  .  com*/
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textcomp));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {

    JTree tree = new JTree() {
        protected void setExpandedState(TreePath path, boolean state) {

            if (state) {
                super.setExpandedState(path, state);
            }// ww w  .  ja  va 2s .  c  om
        }
    };

    JFrame frame = new JFrame("Ignore all collapse requests");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override//  w  w  w. j  a  v  a 2s .c om
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new ImagePanel("yourImage.JPG"));
            frame.pack();
            frame.setVisible(true);
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("FormatDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Main());
    frame.pack();/*from   ww  w .j a v a  2s . co  m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox combo = new JComboBox();
    combo.setEditable(true);// www.  jav a2s  . co m
    for (int i = 0; i < 10; i++) {
        combo.addItem(i);
    }
    JLabel tip = new JLabel();
    tip.setText("Outside combobox");
    JPanel panel = new JPanel();
    panel.add(combo);
    panel.add(tip);
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            tip.setText("Outside combobox");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
            tip.setText(c != null && SwingUtilities.isDescendingFrom(c, combo) ? "Inside combo box"
                    : "Outside combobox");
        }
    });
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}