Example usage for java.awt Container add

List of usage examples for java.awt Container add

Introduction

In this page you can find the example usage for java.awt Container add.

Prototype

public void add(Component comp, Object constraints) 

Source Link

Document

Adds the specified component to the end of this container.

Usage

From source file:TableIt.java

public TableIt() {
    JFrame f = new JFrame();
    TableModel tm = new MyTableModel();
    final JTable table = new JTable(tm);

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1);
    TableCellRenderer renderer = new MyTableCellRenderer();
    column.setCellRenderer(renderer);//from ww w .  java  2  s  .c  o  m

    JButton selectionType = new JButton("Next Type");
    selectionType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean rowSet = table.getRowSelectionAllowed();
            boolean colSet = table.getColumnSelectionAllowed();
            boolean cellSet = table.getCellSelectionEnabled();

            boolean setRow = !rowSet;
            boolean setCol = rowSet ^ colSet;
            boolean setCell = rowSet & colSet;

            table.setCellSelectionEnabled(setCell);
            table.setColumnSelectionAllowed(setCol);
            table.setRowSelectionAllowed(setRow);
            System.out.println("Row Selection Allowed? " + setRow);
            System.out.println("Column Selection Allowed? " + setCol);
            System.out.println("Cell Selection Enabled? " + setCell);
            table.repaint();
        }
    });
    JButton selectionMode = new JButton("Next Mode");
    selectionMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ListSelectionModel lsm = table.getSelectionModel();
            int mode = lsm.getSelectionMode();
            int nextMode;
            String nextModeString;
            if (mode == ListSelectionModel.SINGLE_SELECTION) {
                nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION;
                nextModeString = "Single Interval Selection";
            } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) {
                nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
                nextModeString = "Multiple Interval Selection";
            } else {
                nextMode = ListSelectionModel.SINGLE_SELECTION;
                nextModeString = "Single Selection";
            }
            lsm.setSelectionMode(nextMode);
            System.out.println("Selection Mode: " + nextModeString);
            table.repaint();
        }
    });
    JPanel jp = new JPanel();
    jp.add(selectionType);
    jp.add(selectionMode);
    JScrollPane jsp = new JScrollPane(table);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    c.add(jp, BorderLayout.SOUTH);
    f.setSize(300, 250);
    f.show();
}

From source file:UndoDemo.java

/** Construct a GUI that demonstrates use of UndoManager */
public UndoDemo() {

    Container cp = getContentPane();
    cp.add(ta = new JTextArea(20, 60), BorderLayout.CENTER);
    JPanel bp;// w  w w  .  ja  v a2  s . c  om
    cp.add(bp = new JPanel(), BorderLayout.SOUTH);

    // Create a javax.swing.undo.UndoManager; this is an amazing class that
    // keeps a Stack of UndoableEdits and lets you invoke them;
    // by registering it as a Listener on the TextComponent.Document,
    // the Document will create the UndoableEdit objects and send them
    // to the UndoManager. Between them they do ALL the work!
    um = new UndoManager();
    ta.getDocument().addUndoableEditListener(um);

    // Create the buttons
    JButton cutButton, copyButton, pasteButton, undoButton, redoButton;
    bp.add(cutButton = new JButton("Cut"));
    cutButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.cut();
        }
    });

    bp.add(copyButton = new JButton("Copy"));
    copyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.copy();
        }
    });

    bp.add(pasteButton = new JButton("Paste"));
    pasteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ta.paste();
        }
    });
    bp.add(undoButton = new JButton("UnDo"));
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (um.canUndo()) {
                um.undo();
            } else {
                warn("Can't undo");
            }
        }
    });
    bp.add(redoButton = new JButton("ReDo"));
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (um.canRedo()) {
                um.redo();
            } else {
                warn("Can't redo");
            }
        }
    });
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

From source file:CardLayoutBehaviour.java

public CardLayoutBehaviour() {
    JPanel tab;//from  w  w w.  ja v a2  s  .  c  o m
    Container pane = getContentPane();
    layout = new CardLayout();
    pane.setLayout(layout);
    tab = new JPanel();
    tab.setBackground(Color.red);
    pane.add(tab, "Red Tab");
    tab = new JPanel();
    tab.setBackground(Color.green);
    pane.add(tab, "Green Tab");
    tab = new JPanel();
    tab.setBackground(Color.blue);
    pane.add(tab, "Blue Tab");
}

From source file:SwingWorkerProcessor.java

public SwingWorkerFrame() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    cancelButton.setEnabled(false);//from   www  .ja va2s. c  om

    contentPane.add(statusLabel, BorderLayout.NORTH);
    contentPane.add(startButton, BorderLayout.WEST);
    contentPane.add(cancelButton, BorderLayout.EAST);

    startButton.addActionListener(e -> startProcessing());
    cancelButton.addActionListener(e -> cancelProcessing());
}

From source file:TableValues.java

public Main() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    pane.add(table, BorderLayout.CENTER);
}

From source file:TableValues.java

public ExtendsAbstractTableModel() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    table = new JTable(tv);
    pane.add(table, BorderLayout.CENTER);
}

From source file:com.diversityarrays.util.DatePickerDialog.java

public DatePickerDialog(Window owner, String title, Closure<Date> onComplete) {
    super(owner, title, ModalityType.APPLICATION_MODAL);

    this.onComplete = onComplete;

    datePicker.getMonthView().setZoomable(true);
    datePicker.setLinkPanel(null);//  w w  w  .  ja  va 2 s.c  o m
    datePicker.setFormats(getDefaultDateFormats());

    Box buttons = Box.createHorizontalBox();
    buttons.add(Box.createHorizontalStrut(10));
    buttons.add(new JButton(cancel));
    buttons.add(Box.createHorizontalStrut(10));
    buttons.add(new JButton(save));
    buttons.add(Box.createHorizontalGlue());

    Container cp = getContentPane();
    cp.add(datePicker.getMonthView(), BorderLayout.CENTER);
    cp.add(buttons, BorderLayout.SOUTH);

    pack();
}

From source file:MessageDigestTest.java

public MessageDigestFrame() {
    setTitle("MessageDigestTest");
    setSize(400, 200);/*from w  w w . j  a  va2s . c o  m*/
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JPanel panel = new JPanel();
    ButtonGroup group = new ButtonGroup();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JCheckBox b = (JCheckBox) event.getSource();
            setAlgorithm(b.getText());
        }
    };
    addCheckBox(panel, "SHA-1", group, true, listener);
    addCheckBox(panel, "MD5", group, false, listener);

    Container contentPane = getContentPane();

    contentPane.add(panel, "North");
    contentPane.add(new JScrollPane(message), "Center");
    contentPane.add(digest, "South");
    digest.setFont(new Font("Monospaced", Font.PLAIN, 12));

    setAlgorithm("SHA-1");

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenuItem fileDigestItem = new JMenuItem("File digest");
    fileDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            loadFile();
        }
    });
    menu.add(fileDigestItem);
    JMenuItem textDigestItem = new JMenuItem("Text area digest");
    textDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            String m = message.getText();
            computeDigest(m.getBytes());
        }
    });
    menu.add(textDigestItem);
    menuBar.add(menu);
    setJMenuBar(menuBar);
}

From source file:AppletJDBCDrop.java

public void init() {
    Connection connection;/* w ww  . j  a  va2s. c  o  m*/
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager
                .getConnection("jdbc:mysql://192.168.1.25/accounts?user=spider&password=spider");
    } catch (Exception connectException) {
        connectException.printStackTrace();
    }

    Container c = getContentPane();
    tableList = new JList();
    loadTables();
    c.add(new JScrollPane(tableList), BorderLayout.NORTH);

    dropButton = new JButton("Drop Table");
    dropButton.addActionListener(this);
    c.add(dropButton, BorderLayout.SOUTH);
}

From source file:SwingMenus.java

public void init() {
    ML ml = new ML();
    CMIL cmil = new CMIL();
    safety[0].setActionCommand("Guard");
    safety[0].setMnemonic(KeyEvent.VK_G);
    safety[0].addItemListener(cmil);//from  w  w  w .  j a  va2s .c o m
    safety[1].setActionCommand("Hide");
    safety[1].setMnemonic(KeyEvent.VK_H);
    safety[1].addItemListener(cmil);
    other[0].addActionListener(new FooL());
    other[1].addActionListener(new BarL());
    other[2].addActionListener(new BazL());
    FL fl = new FL();
    for (int i = 0; i < flavors.length; i++) {
        JMenuItem mi = new JMenuItem(flavors[i]);
        mi.addActionListener(fl);
        m.add(mi);
        // Add separators at intervals:
        if ((i + 1) % 3 == 0)
            m.addSeparator();
    }
    for (int i = 0; i < safety.length; i++)
        s.add(safety[i]);
    s.setMnemonic(KeyEvent.VK_A);
    f.add(s);
    f.setMnemonic(KeyEvent.VK_F);
    for (int i = 0; i < file.length; i++) {
        file[i].addActionListener(fl);
        f.add(file[i]);
    }
    mb1.add(f);
    mb1.add(m);
    setJMenuBar(mb1);
    t.setEditable(false);
    Container cp = getContentPane();
    cp.add(t, BorderLayout.CENTER);
    // Set up the system for swapping menus:
    b.addActionListener(new BL());
    b.setMnemonic(KeyEvent.VK_S);
    cp.add(b, BorderLayout.NORTH);
    for (int i = 0; i < other.length; i++)
        fooBar.add(other[i]);
    fooBar.setMnemonic(KeyEvent.VK_B);
    mb2.add(fooBar);
}