List of usage examples for javax.swing JPanel JPanel
public JPanel(boolean isDoubleBuffered)
JPanel
with FlowLayout
and the specified buffering strategy. From source file:RegexPatternFormatter.java
public static void main(String argv[]) { // a demo main() to show how RegexPatternFormatter could be used JLabel lab1 = new JLabel("even length strings:"); java.util.regex.Pattern evenLength = java.util.regex.Pattern.compile("(..)*"); JFormattedTextField ftf1 = new JFormattedTextField(new RegexPatternFormatter(evenLength)); JLabel lab2 = new JLabel("no vowels:"); java.util.regex.Pattern noVowels = java.util.regex.Pattern.compile("[^aeiou]*", java.util.regex.Pattern.CASE_INSENSITIVE); RegexPatternFormatter noVowelFormatter = new RegexPatternFormatter(noVowels); noVowelFormatter.setAllowsInvalid(false); // don't allow user to type // vowels/*from w w w . j ava 2 s . co m*/ JFormattedTextField ftf2 = new JFormattedTextField(noVowelFormatter); JFrame f = new JFrame("RegexPatternFormatter Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pan1 = new JPanel(new java.awt.BorderLayout()); pan1.add(lab1, java.awt.BorderLayout.WEST); pan1.add(ftf1, java.awt.BorderLayout.CENTER); lab1.setLabelFor(ftf1); f.getContentPane().add(pan1, java.awt.BorderLayout.NORTH); JPanel pan2 = new JPanel(new java.awt.BorderLayout()); pan2.add(lab2, java.awt.BorderLayout.WEST); pan2.add(ftf2, java.awt.BorderLayout.CENTER); lab2.setLabelFor(ftf2); f.getContentPane().add(pan2, java.awt.BorderLayout.SOUTH); f.setSize(300, 80); f.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { // create a hierarchy of nodes MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode bNode = new DefaultMutableTreeNode("B"); MutableTreeNode cNode = new DefaultMutableTreeNode("C"); root.insert(bNode, 0);//from w ww . ja va2s . com root.insert(cNode, 1); bNode.insert(new DefaultMutableTreeNode("1"), 0); bNode.insert(new DefaultMutableTreeNode("2"), 1); cNode.insert(new DefaultMutableTreeNode("1"), 0); cNode.insert(new DefaultMutableTreeNode("2"), 1); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("Z"); final JButton button = new JButton("Add"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode beams = new DefaultMutableTreeNode("B"); MutableTreeNode gears = new DefaultMutableTreeNode("C"); root.insert(beams, 0);// w w w . ja v a2s . co m root.insert(gears, 1); beams.insert(new DefaultMutableTreeNode("4 "), 0); beams.insert(new DefaultMutableTreeNode("6 "), 1); beams.insert(new DefaultMutableTreeNode("8 "), 2); beams.insert(new DefaultMutableTreeNode("12 "), 3); gears.insert(new DefaultMutableTreeNode("8t"), 0); gears.insert(new DefaultMutableTreeNode("24t"), 1); gears.insert(new DefaultMutableTreeNode("40t"), 2); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("16t"); final JButton button = new JButton("Add a part"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); frame.setVisible(true); }
From source file:com.adito.upgrade.Upgrade.java
/** * @param args//w w w . j a v a 2 s . c o 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: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 w w w.j a v a 2 s. c o 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:com.amazonaws.services.kinesis.leases.impl.LeaseCoordinatorExerciser.java
public static void main(String[] args) throws InterruptedException, DependencyException, InvalidStateException, ProvisionedThroughputException, IOException { int numCoordinators = 9; int numLeases = 73; int leaseDurationMillis = 10000; int epsilonMillis = 100; AWSCredentialsProvider creds = new DefaultAWSCredentialsProviderChain(); AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(creds); ILeaseManager<KinesisClientLease> leaseManager = new KinesisClientLeaseManager("nagl_ShardProgress", ddb); if (leaseManager.createLeaseTableIfNotExists(10L, 50L)) { LOG.info("Waiting for newly created lease table"); if (!leaseManager.waitUntilLeaseTableExists(10, 300)) { LOG.error("Table was not created in time"); return; }/*from w w w.ja v a 2s . co m*/ } CWMetricsFactory metricsFactory = new CWMetricsFactory(creds, "testNamespace", 30 * 1000, 1000); final List<LeaseCoordinator<KinesisClientLease>> coordinators = new ArrayList<LeaseCoordinator<KinesisClientLease>>(); for (int i = 0; i < numCoordinators; i++) { String workerIdentifier = "worker-" + Integer.toString(i); LeaseCoordinator<KinesisClientLease> coord = new LeaseCoordinator<KinesisClientLease>(leaseManager, workerIdentifier, leaseDurationMillis, epsilonMillis, metricsFactory); coordinators.add(coord); } leaseManager.deleteAll(); for (int i = 0; i < numLeases; i++) { KinesisClientLease lease = new KinesisClientLease(); lease.setLeaseKey(Integer.toString(i)); lease.setCheckpoint(new ExtendedSequenceNumber("checkpoint")); leaseManager.createLeaseIfNotExists(lease); } final JFrame frame = new JFrame("Test Visualizer"); frame.setPreferredSize(new Dimension(800, 600)); final JPanel panel = new JPanel(new GridLayout(coordinators.size() + 1, 0)); final JLabel ticker = new JLabel("tick"); panel.add(ticker); frame.getContentPane().add(panel); final Map<String, JLabel> labels = new HashMap<String, JLabel>(); for (final LeaseCoordinator<KinesisClientLease> coord : coordinators) { JPanel coordPanel = new JPanel(); coordPanel.setLayout(new BoxLayout(coordPanel, BoxLayout.X_AXIS)); final Button button = new Button("Stop " + coord.getWorkerIdentifier()); button.setMaximumSize(new Dimension(200, 50)); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (coord.isRunning()) { coord.stop(); button.setLabel("Start " + coord.getWorkerIdentifier()); } else { try { coord.start(); } catch (LeasingException e) { LOG.error(e); } button.setLabel("Stop " + coord.getWorkerIdentifier()); } } }); coordPanel.add(button); JLabel label = new JLabel(); coordPanel.add(label); labels.put(coord.getWorkerIdentifier(), label); panel.add(coordPanel); } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); new Thread() { // Key is lease key, value is green-ness as a value from 0 to 255. // Great variable name, huh? private Map<String, Integer> greenNesses = new HashMap<String, Integer>(); // Key is lease key, value is last owning worker private Map<String, String> lastOwners = new HashMap<String, String>(); @Override public void run() { while (true) { for (LeaseCoordinator<KinesisClientLease> coord : coordinators) { String workerIdentifier = coord.getWorkerIdentifier(); JLabel label = labels.get(workerIdentifier); List<KinesisClientLease> asgn = new ArrayList<KinesisClientLease>(coord.getAssignments()); Collections.sort(asgn, new Comparator<KinesisClientLease>() { @Override public int compare(KinesisClientLease arg0, KinesisClientLease arg1) { return arg0.getLeaseKey().compareTo(arg1.getLeaseKey()); } }); StringBuilder builder = new StringBuilder(); builder.append("<html>"); builder.append(workerIdentifier).append(":").append(asgn.size()).append(" "); for (KinesisClientLease lease : asgn) { String leaseKey = lease.getLeaseKey(); String lastOwner = lastOwners.get(leaseKey); // Color things green when they switch owners, decay the green-ness over time. Integer greenNess = greenNesses.get(leaseKey); if (greenNess == null || lastOwner == null || !lastOwner.equals(lease.getLeaseOwner())) { greenNess = 200; } else { greenNess = Math.max(0, greenNess - 20); } greenNesses.put(leaseKey, greenNess); lastOwners.put(leaseKey, lease.getLeaseOwner()); builder.append(String.format("<font color=\"%s\">%03d</font>", String.format("#00%02x00", greenNess), Integer.parseInt(leaseKey))).append(" "); } builder.append("</html>"); label.setText(builder.toString()); label.revalidate(); label.repaint(); } if (ticker.getText().equals("tick")) { ticker.setText("tock"); } else { ticker.setText("tick"); } try { Thread.sleep(200); } catch (InterruptedException e) { } } } }.start(); frame.pack(); frame.setVisible(true); for (LeaseCoordinator<KinesisClientLease> coord : coordinators) { coord.start(); } }
From source file:SimpleClient.java
public static void main(String argv[]) { boolean usage = false; for (int optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-L")) { url.addElement(argv[++optind]); } else if (argv[optind].startsWith("-")) { usage = true;/*from w w w. j av a 2 s . com*/ break; } else { usage = true; break; } } if (usage || url.size() == 0) { System.out.println("Usage: SimpleClient -L url"); System.out.println(" where url is protocol://username:password@hostname/"); System.exit(1); } try { // Set up our Mailcap entries. This will allow the JAF // to locate our viewers. File capfile = new File("simple.mailcap"); if (!capfile.isFile()) { System.out.println("Cannot locate the \"simple.mailcap\" file."); System.exit(1); } CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile))); JFrame frame = new JFrame("Simple JavaMail Client"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Get a Store object SimpleAuthenticator auth = new SimpleAuthenticator(frame); Session session = Session.getDefaultInstance(System.getProperties(), auth); //session.setDebug(true); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); // create a node for each store we have for (Enumeration e = url.elements(); e.hasMoreElements();) { String urlstring = (String) e.nextElement(); URLName urln = new URLName(urlstring); Store store = session.getStore(urln); StoreTreeNode storenode = new StoreTreeNode(store); root.add(storenode); } DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); tree.addTreeSelectionListener(new TreePress()); /* Put the Tree in a scroller. */ JScrollPane sp = new JScrollPane(); sp.setPreferredSize(new Dimension(250, 300)); sp.getViewport().add(tree); /* Create a double buffered JPanel */ JPanel sv = new JPanel(new BorderLayout()); sv.add("Center", sp); fv = new FolderViewer(null); JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv); jsp.setOneTouchExpandable(true); mv = new MessageViewer(); JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv); jsp2.setOneTouchExpandable(true); frame.getContentPane().add(jsp2); frame.pack(); frame.show(); } catch (Exception ex) { System.out.println("SimpletClient caught exception"); ex.printStackTrace(); System.exit(1); } }
From source file:Main.java
public static Component getButtonLayout(int num) { JPanel p = new JPanel(new BorderLayout(3, 3)); p.add(new JLabel("Label " + num), BorderLayout.NORTH); JPanel b = new JPanel(new GridLayout(1, 0, 25, 5)); for (int ii = 1; ii < 4; ii++) { b.add(new JButton("Button " + ii)); }//from w w w . j a v a 2 s . c o m p.add(b, BorderLayout.CENTER); return p; }
From source file:Main.java
public static JPanel addToFlowLayout(JComponent comp, int flowLayoutAlignment) { JPanel panel = new JPanel(new FlowLayout(flowLayoutAlignment)); panel.add(comp);/*from ww w . j a v a 2s . co m*/ return panel; }
From source file:Main.java
public static void showFrameWithToolBar(String toolBarPosition) { JPanel gui = new JPanel(new BorderLayout()); JToolBar tb = new JToolBar(); gui.add(tb, toolBarPosition);/*from ww w . j av a2s . com*/ tb.add(new JButton("Button 1")); tb.add(new JButton("Button 2")); tb.addSeparator(); tb.add(new JButton("Button 3")); tb.add(new JCheckBox("Check 1", true)); JFrame f = new JFrame(toolBarPosition); f.setContentPane(gui); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationByPlatform(true); f.pack(); f.setSize(400, 120); f.setVisible(true); }