List of usage examples for javax.swing JPanel add
public Component add(Component comp)
From source file:LoadSave.java
public static void main(String args[]) { final String filename = "text.out"; JFrame frame = new JFrame("Loading/Saving Example"); Container content = frame.getContentPane(); final JTextField textField = new JTextField(); content.add(textField, BorderLayout.NORTH); JPanel panel = new JPanel(); Action loadAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doLoadCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace();//from www . j a v a 2s . c o m } } }; loadAction.putValue(Action.NAME, "Load"); JButton loadButton = new JButton(loadAction); panel.add(loadButton); Action saveAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doSaveCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }; saveAction.putValue(Action.NAME, "Save"); JButton saveButton = new JButton(saveAction); panel.add(saveButton); Action clearAction = new AbstractAction() { { putValue(Action.NAME, "Clear"); } public void actionPerformed(ActionEvent e) { textField.setText(""); } }; JButton clearButton = new JButton(clearAction); panel.add(clearButton); content.add(panel, BorderLayout.SOUTH); frame.setSize(250, 150); 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 GridLayout(1, 2, 2, 2)); JTextArea tArea1 = new JTextArea(); tArea1.setLineWrap(true);//ww w. java 2 s. co m JTextArea tArea2 = new JTextArea(); tArea2.setLineWrap(true); tArea1.setText("I got a long long line of text in my JTextArea"); tArea2.setText("I got a long long line of text in my JTextArea"); JScrollPane scroller1 = new JScrollPane(); JScrollPane scroller2 = new JScrollPane(); scroller1.setViewportView(tArea1); scroller2.setViewportView(tArea2); contentPane.add(scroller1); contentPane.add(scroller2); frame.setContentPane(contentPane); frame.setSize(100, 100); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:GlueSample.java
public static void main(String args[]) { Box horizontalBox;/*w w w. j a v a 2 s. c om*/ JPanel panel; JFrame frame = new JFrame("Horizontal Glue"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(0, 1)); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(Box.createGlue()); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("Beginning Glue")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(new JButton("Left")); horizontalBox.add(Box.createGlue()); horizontalBox.add(new JButton("Middle")); horizontalBox.add(Box.createGlue()); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("2 Middle Glues")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(Box.createGlue()); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); horizontalBox.add(Box.createGlue()); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("Beginning/End Glues")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); horizontalBox.add(Box.createGlue()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("End Glue")); contentPane.add(panel); frame.setSize(300, 300); frame.setVisible(true); }
From source file:StrutSample.java
public static void main(String args[]) { Box horizontalBox;/*from w w w . ja va 2 s .co m*/ JPanel panel; JFrame frame = new JFrame("Horizontal Strut"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(0, 1)); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(Box.createHorizontalStrut(10)); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("Beginning Strut")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(new JButton("Left")); horizontalBox.add(Box.createHorizontalStrut(10)); horizontalBox.add(new JButton("Middle")); horizontalBox.add(Box.createHorizontalStrut(25)); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("2 Middle Struts")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(Box.createHorizontalStrut(25)); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); horizontalBox.add(Box.createHorizontalStrut(10)); panel = new JPanel(new BorderLayout()); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("Beginning/End Struts")); contentPane.add(panel); horizontalBox = Box.createHorizontalBox(); horizontalBox.add(new JButton("Left")); horizontalBox.add(new JButton("Middle")); horizontalBox.add(new JButton("Right")); panel = new JPanel(new BorderLayout()); horizontalBox.add(Box.createHorizontalStrut(10)); panel.add(horizontalBox); panel.setBorder(BorderFactory.createTitledBorder("End Strut")); contentPane.add(panel); frame.setSize(300, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); buttonPanel.addContainerListener(new ContainerListener() { public void componentAdded(ContainerEvent e) { displayMessage(" added to ", e); }/*ww w .ja v a 2s . co m*/ public void componentRemoved(ContainerEvent e) { displayMessage(" removed from ", e); } void displayMessage(String action, ContainerEvent e) { System.out.println(((JButton) e.getChild()).getText() + " was" + action + e.getContainer().getClass().getName()); } }); buttonPanel.add(new JButton("A")); frame.add(buttonPanel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:UsingContainerListener.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); buttonPanel.addContainerListener(new ContainerListener() { public void componentAdded(ContainerEvent e) { displayMessage(" added to ", e); }// w w w . j av a 2s .co m public void componentRemoved(ContainerEvent e) { displayMessage(" removed from ", e); } void displayMessage(String action, ContainerEvent e) { System.out.println(((JButton) e.getChild()).getText() + " was" + action + e.getContainer().getClass().getName()); } }); buttonPanel.add(new JButton("A")); frame.add(buttonPanel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(true); buttonPanel.addContainerListener(new ContainerListener() { public void componentAdded(ContainerEvent e) { displayMessage(" added to ", e); }//from w w w .j a v a 2s. c om public void componentRemoved(ContainerEvent e) { displayMessage(" removed from ", e); } void displayMessage(String action, ContainerEvent e) { System.out.println(((JButton) e.getChild()).getText() + " was" + action + e.getContainer().getClass().getName()); } }); buttonPanel.add(new JButton("A")); frame.add(buttonPanel); 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 gui = new JPanel(new BorderLayout(5, 5)); JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3)); plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)")); UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels(); String[] plafNames = new String[plafInfos.length]; for (int ii = 0; ii < plafInfos.length; ii++) { plafNames[ii] = plafInfos[ii].getName(); }//from ww w .j a va 2 s.c om JComboBox plafChooser = new JComboBox(plafNames); plafComponents.add(plafChooser); JCheckBox pack = new JCheckBox("Pack on PLAF change", true); plafComponents.add(pack); plafChooser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int index = plafChooser.getSelectedIndex(); try { UIManager.setLookAndFeel(plafInfos[index].getClassName()); SwingUtilities.updateComponentTreeUI(frame); if (pack.isSelected()) { frame.pack(); frame.setMinimumSize(frame.getSize()); } } catch (Exception e) { e.printStackTrace(); } } }); gui.add(plafComponents, BorderLayout.NORTH); JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4)); dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)")); gui.add(dynamicLabels, BorderLayout.WEST); final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3)); labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)")); JButton addNew = new JButton("Add Another Label"); dynamicLabels.add(addNew, BorderLayout.NORTH); addNew.addActionListener(new ActionListener() { private int labelCount = 0; public void actionPerformed(ActionEvent ae) { labels.add(new JLabel("Label " + ++labelCount)); frame.validate(); } }); dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER); String[] header = { "Name", "Value" }; String[] a = new String[0]; String[] names = System.getProperties().stringPropertyNames().toArray(a); String[][] data = new String[names.length][2]; for (int ii = 0; ii < names.length; ii++) { data[ii][0] = names[ii]; data[ii][1] = System.getProperty(names[ii]); } DefaultTableModel model = new DefaultTableModel(data, header); JTable table = new JTable(model); JScrollPane tableScroll = new JScrollPane(table); Dimension tablePreferred = tableScroll.getPreferredSize(); tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3)); JPanel imagePanel = new JPanel(new GridBagLayout()); JLabel imageLabel = new JLabel("test"); imagePanel.add(imageLabel, null); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel)); gui.add(splitPane, BorderLayout.CENTER); frame.setContentPane(gui); frame.pack(); frame.setVisible(true); }
From source file:de.codesourcery.geoip.Main.java
public static void main(String[] args) throws Exception { final IGeoLocator<StringSubject> locator = createGeoLocator(); final JFrame frame = new JFrame("GeoIP"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { try { locator.dispose();//w w w . ja v a 2 s . c o m } catch (Exception e1) { e1.printStackTrace(); } }; }); final MapImage image = MapImage.getRobinsonWorldMap(); // MapImage.getMillerWorldMap(); final MapCanvas canvas = new MapCanvas(image); for (GeoLocation<StringSubject> loc : locator.locate(getSpammers())) { if (loc.hasValidCoordinates()) { canvas.addCoordinate(PointRenderer.createPoint(loc, Color.YELLOW)); } } // canvas.addCoordinate( PointRenderer.createPoint( ZERO , Color.YELLOW ) ); // canvas.addCoordinate( PointRenderer.createPoint( WELLINGTON , Color.RED ) ); // canvas.addCoordinate( PointRenderer.createPoint( MELBOURNE , Color.RED ) ); // canvas.addCoordinate( PointRenderer.createPoint( HAMBURG , Color.RED ) ); final double heightToWidth = image.height() / (double) image.width(); // preserve aspect ratio of map canvas.setPreferredSize(new Dimension(640, (int) Math.round(640 * heightToWidth))); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.add(new JLabel("Scale-X")); final JTextField scaleX = new JTextField(Double.toString(image.getScaleX())); scaleX.setColumns(5); final JTextField scaleY = new JTextField(Double.toString(image.getScaleY())); scaleY.setColumns(5); final ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { double x = Double.parseDouble(scaleX.getText()); double y = Double.parseDouble(scaleY.getText()); image.setScale(x, y); canvas.repaint(); } }; scaleX.addActionListener(listener); scaleY.addActionListener(listener); panel.add(new JLabel("Scale-X")); panel.add(scaleX); panel.add(new JLabel("Scale-Y")); panel.add(scaleY); final JTextField ipAddress = new JTextField("www.kickstarter.com"); ipAddress.setColumns(20); final ActionListener ipListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { final String destinationIP = ipAddress.getText(); if (StringUtils.isBlank(destinationIP)) { return; } /* * Perform traceroute. */ final List<String> hops; try { if (TracePath.isPathTracingAvailable()) { hops = TracePath.trace(destinationIP); } else { System.err.println("tracepath not available."); if (TracePath.isValidAddress(destinationIP)) { hops = new ArrayList<>(); hops.add(destinationIP); } else { System.err.println(destinationIP + " is no valid IP"); return; } } } catch (Exception ex) { System.err.println("Failed to trace " + destinationIP); ex.printStackTrace(); return; } System.out.println("Trace contains " + hops.size() + " IPs"); /* * Gather locations. */ final List<StringSubject> subjects = new ArrayList<>(); for (String ip : hops) { subjects.add(new StringSubject(ip)); } final List<GeoLocation<StringSubject>> locations; try { long time = -System.currentTimeMillis(); locations = locator.locate(subjects); time += System.currentTimeMillis(); System.out.println("Locating hops for " + destinationIP + " returned " + locations.size() + " valid locations ( time: " + time + " ms)"); System.out.flush(); } catch (Exception e2) { e2.printStackTrace(); return; } /* * Weed-out invalid/unknown locations. */ { GeoLocation<StringSubject> previous = null; for (Iterator<GeoLocation<StringSubject>> it = locations.iterator(); it.hasNext();) { final GeoLocation<StringSubject> location = it.next(); if (!location.hasValidCoordinates() || (previous != null && previous.coordinate().equals(location.coordinate()))) { it.remove(); System.err.println("Ignoring invalid/duplicate location for " + location); } else { previous = location; } } } /* * Populate chart. */ System.out.println("Adding " + locations.size() + " hops to chart"); System.out.flush(); canvas.removeAllCoordinates(); if (locations.size() == 1) { canvas.addCoordinate( PointRenderer.createPoint(locations.get(0), getLabel(locations.get(0)), Color.BLACK)); } else if (locations.size() > 1) { GeoLocation<StringSubject> previous = locations.get(0); MapPoint previousPoint = PointRenderer.createPoint(previous, getLabel(previous), Color.BLACK); final int len = locations.size(); for (int i = 1; i < len; i++) { final GeoLocation<StringSubject> current = locations.get(i); // final MapPoint currentPoint = PointRenderer.createPoint( current , getLabel( current ) , Color.BLACK ); final MapPoint currentPoint = PointRenderer.createPoint(current, Color.BLACK); // canvas.addCoordinate( LineRenderer.createLine( previousPoint , currentPoint , Color.RED ) ); canvas.addCoordinate(CurvedLineRenderer.createLine(previousPoint, currentPoint, Color.RED)); previous = locations.get(i); previousPoint = currentPoint; } } System.out.println("Finished adding"); System.out.flush(); canvas.repaint(); } }; ipAddress.addActionListener(ipListener); panel.add(new JLabel("IP")); panel.add(ipAddress); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(panel, BorderLayout.NORTH); frame.getContentPane().add(canvas, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(300, 500);// w ww .ja v a2s. c o m f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pan = new JPanel(new GridLayout(1, 1)); XmlJTree myTree = new XmlJTree(null); f.add(new JScrollPane(myTree)); JButton button = new JButton("Choose file"); button.addActionListener(e -> { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("XML file", "xml"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { myTree.setPath(chooser.getSelectedFile().getAbsolutePath()); } }); pan.add(button); f.add(pan, BorderLayout.SOUTH); f.setVisible(true); }