List of usage examples for java.awt Container add
public void add(Component comp, Object constraints)
From source file:MouseDragClip.java
public static void main(String[] av) { JFrame f = new JFrame("Mouse Dragger"); Container cp = f.getContentPane(); if (av.length < 1) { System.err.println("Usage: MouseDragClip imagefile"); System.exit(1);//from w w w .j ava 2 s. co m } Image im = Toolkit.getDefaultToolkit().getImage(av[0]); // create a MouseDragClip object MouseDragClip j = new MouseDragClip(im); cp.setLayout(new BorderLayout()); cp.add(BorderLayout.NORTH, new Label("Hello, and welcome to the world of Java")); cp.add(BorderLayout.CENTER, j); cp.add(BorderLayout.SOUTH, status = new Label()); status.setSize(f.getSize().width, status.getSize().height); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
From source file:ColorChooserSample.java
public static void main(String args[]) { JFrame f = new JFrame("JColorChooser Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); final JButton button = new JButton("Pick to Change Background"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color initialBackground = button.getBackground(); Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground); if (background != null) { button.setBackground(background); }/*from w w w . j a va 2 s.co m*/ } }; button.addActionListener(actionListener); content.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:JTextFieldSample.java
public static void main(String args[]) { String title = (args.length == 0 ? "TextField Listener Example" : args[0]); JFrame frame = new JFrame(title); Container content = frame.getContentPane(); JPanel namePanel = new JPanel(new BorderLayout()); JLabel nameLabel = new JLabel("Name: "); nameLabel.setDisplayedMnemonic(KeyEvent.VK_N); JTextField nameTextField = new JTextField(); nameLabel.setLabelFor(nameTextField); namePanel.add(nameLabel, BorderLayout.WEST); namePanel.add(nameTextField, BorderLayout.CENTER); content.add(namePanel, BorderLayout.NORTH); JPanel cityPanel = new JPanel(new BorderLayout()); JLabel cityLabel = new JLabel("City: "); cityLabel.setDisplayedMnemonic(KeyEvent.VK_C); JTextField cityTextField = new JTextField(); cityLabel.setLabelFor(cityTextField); cityPanel.add(cityLabel, BorderLayout.WEST); cityPanel.add(cityTextField, BorderLayout.CENTER); content.add(cityPanel, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); }/*from ww w .j a va2 s . c om*/ }; nameTextField.setActionCommand("Yo"); nameTextField.addActionListener(actionListener); cityTextField.addActionListener(actionListener); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); } public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; nameTextField.addKeyListener(keyListener); cityTextField.addKeyListener(keyListener); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { Runnable runnable = new Runnable() { public void run() { JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); } }; SwingUtilities.invokeLater(runnable); return false; } else { return true; } } }; nameTextField.setInputVerifier(verifier); cityTextField.setInputVerifier(verifier); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString + " / "); Document source = documentEvent.getDocument(); int length = source.getLength(); try { System.out.println("Contents: " + source.getText(0, length)); } catch (BadLocationException badLocationException) { System.out.println("Contents: Unknown"); } } }; nameTextField.getDocument().addDocumentListener(documentListener); cityTextField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:TableSample.java
public static void main(String args[]) { JFrame f = new JFrame("JTable Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); Object rows[][] = { { "AMZN", "Amazon", "67 9/16" }, { "AOL", "America Online", "68 3/4" }, { "BOUT", "About.com", "56 3/8" }, { "CDNW", "CDnow", "4 7/16" }, { "DCLK", "DoubleClick", "87 3/16" }, { "EBAY", "eBay", "180 7/8" }, { "EWBX", "EarthWeb", "18 1/4" }, { "MKTW", "MarketWatch", "29" }, { "TGLO", "Theglobe.com", "4 15/16" }, { "YHOO", "Yahoo!", "151 1/8" } }; Object columns[] = { "Symbol", "Name", "Price" }; JTable table = new JTable(rows, columns); JScrollPane scrollPane = new JScrollPane(table); content.add(scrollPane, BorderLayout.CENTER); f.setSize(300, 200);/*w w w .j av a 2 s . com*/ f.setVisible(true); }
From source file:ModifyModelSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Modifying Model"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); // Fill model final DefaultListModel model = new DefaultListModel(); for (int i = 0, n = labels.length; i < n; i++) { model.addElement(labels[i]);/*from ww w. j a v a 2 s. co m*/ } JList jlist = new JList(model); JScrollPane scrollPane1 = new JScrollPane(jlist); contentPane.add(scrollPane1, BorderLayout.WEST); final JTextArea textArea = new JTextArea(); textArea.setEditable(false); JScrollPane scrollPane2 = new JScrollPane(textArea); contentPane.add(scrollPane2, BorderLayout.CENTER); ListDataListener listDataListener = new ListDataListener() { public void contentsChanged(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } public void intervalAdded(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } public void intervalRemoved(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } private void appendEvent(ListDataEvent listDataEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); switch (listDataEvent.getType()) { case ListDataEvent.CONTENTS_CHANGED: pw.print("Type: Contents Changed"); break; case ListDataEvent.INTERVAL_ADDED: pw.print("Type: Interval Added"); break; case ListDataEvent.INTERVAL_REMOVED: pw.print("Type: Interval Removed"); break; } pw.print(", Index0: " + listDataEvent.getIndex0()); pw.print(", Index1: " + listDataEvent.getIndex1()); DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource(); Enumeration elements = theModel.elements(); pw.print(", Elements: "); while (elements.hasMoreElements()) { pw.print(elements.nextElement()); pw.print(","); } pw.println(); textArea.append(sw.toString()); } }; model.addListDataListener(listDataListener); // Setup buttons JPanel jp = new JPanel(new GridLayout(2, 1)); JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); jp.add(jp1); jp.add(jp2); JButton jb = new JButton("add F"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.add(0, "First"); } }); jb = new JButton("addElement L"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("Last"); } }); jb = new JButton("insertElementAt M"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); model.insertElementAt("Middle", size / 2); } }); jb = new JButton("set F"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.set(0, "New First"); } }); jb = new JButton("setElementAt L"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.setElementAt("New Last", size - 1); } }); jb = new JButton("load 10"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { for (int i = 0, n = labels.length; i < n; i++) { model.addElement(labels[i]); } } }); jb = new JButton("clear"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.clear(); } }); jb = new JButton("remove F"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.remove(0); } }); jb = new JButton("removeAllElements"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.removeAllElements(); } }); jb = new JButton("removeElement 'Last'"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.removeElement("Last"); } }); jb = new JButton("removeElementAt M"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.removeElementAt(size / 2); } }); jb = new JButton("removeRange FM"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.removeRange(0, size / 2); } }); contentPane.add(jp, BorderLayout.SOUTH); frame.setSize(640, 300); frame.setVisible(true); }
From source file:OptionPaneSample.java
public static void main(String args[]) { JFrame f = new JFrame("JOptionPane Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); Object response = JOptionPane.showInputDialog(source, "Where do you want to go tomorrow?", "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C", "D", "E" }, "E"); System.out.println("Response: " + response); }/* w ww. ja va 2 s . c om*/ }; button.addActionListener(actionListener); content.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:ElementSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Element Example"); Container content = frame.getContentPane(); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }//from w w w.j ava 2 s.c om } }; button.addActionListener(actionListener); content.add(scrollPane, BorderLayout.CENTER); content.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) throws BadLocationException { JFrame jf = new JFrame("StyledText"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = jf.getContentPane(); JTextPane pane = new JTextPane(); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setBold(set, true); // Set the attributes before adding text pane.setCharacterAttributes(set, true); pane.setText("java2s.com "); set = new SimpleAttributeSet(); StyleConstants.setItalic(set, true); StyleConstants.setForeground(set, Color.red); StyleConstants.setBackground(set, Color.blue); Document doc = pane.getStyledDocument(); doc.insertString(doc.getLength(), "Swing ", set); set = new SimpleAttributeSet(); StyleConstants.setFontSize(set, 24); doc.insertString(doc.getLength(), "Tutorial", set); JScrollPane scrollPane = new JScrollPane(pane); cp.add(scrollPane, BorderLayout.CENTER); jf.setSize(400, 300);//from ww w .j av a2 s .com jf.setVisible(true); }
From source file:HyperlinkTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); final JEditorPane ep = new JEditorPane(); try {//from www. j a v a 2s . c o m ep.setPage("http://www.java2s.com"); } catch (IOException e) { System.err.println("Bad URL: " + e); System.exit(-1); } HyperlinkListener listener = new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { ep.setPage(e.getURL()); } catch (IOException ioe) { System.err.println("Error loading: " + ioe); } } } }; ep.addHyperlinkListener(listener); ep.setEditable(false); JScrollPane pane = new JScrollPane(ep); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(640, 480); frame.show(); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); JTextArea b = new JTextArea(); b.setPreferredSize(new Dimension(600, 600)); JScrollPane pane = new JScrollPane(b); AdjustmentListener hListener = new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("Horizontal: "); dumpInfo(e);//from w w w .j a v a2 s . co m } }; JScrollBar hBar = pane.getHorizontalScrollBar(); hBar.addAdjustmentListener(hListener); AdjustmentListener vListener = new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("Vertical: "); dumpInfo(e); } }; JScrollBar vBar = pane.getVerticalScrollBar(); vBar.addAdjustmentListener(vListener); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }