List of usage examples for javax.swing JLabel JLabel
public JLabel(Icon image)
JLabel
instance with the specified image. From source file:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Drag Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon icon = new ImageIcon("image.jpg"); JLabel label = new JLabel(icon); label.setTransferHandler(new ImageSelection()); MouseListener listener = new MouseAdapter() { public void mousePressed(MouseEvent me) { JComponent comp = (JComponent) me.getSource(); TransferHandler handler = comp.getTransferHandler(); handler.exportAsDrag(comp, me, TransferHandler.COPY); }//ww w .ja va 2s.c o m }; label.addMouseListener(listener); frame.add(new JScrollPane(label), BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JTextField[] txtAllAverages;/* ww w . j a va 2 s . c o m*/ int testCount = 2; txtAllAverages = new JTextField[testCount]; JPanel inputControls = new JPanel(new BorderLayout(5, 5)); JPanel inputControlsLabels = new JPanel(new GridLayout(0, 1, 3, 3)); JPanel inputControlsFields = new JPanel(new GridLayout(0, 1, 3, 3)); inputControls.add(inputControlsLabels, BorderLayout.WEST); inputControls.add(inputControlsFields, BorderLayout.CENTER); for (int i = 0; i < testCount; i++) { inputControlsLabels.add(new JLabel("Test score: ")); JTextField field = new JTextField(10); inputControlsFields.add(field); txtAllAverages[i] = field; } JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2)); controls.add(new JButton("Reset")); controls.add(new JButton("Submit")); JPanel gui = new JPanel(new BorderLayout(10, 10)); gui.setBorder(new TitledBorder("Averages")); gui.add(inputControls, BorderLayout.CENTER); gui.add(controls, BorderLayout.SOUTH); JFrame f = new JFrame(); f.setContentPane(gui); f.pack(); f.setLocationByPlatform(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { Robot robot = new Robot(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage image = robot.createScreenCapture(new Rectangle(d)); BufferedImage sub = image.getSubimage(0, 0, 400, 400); File f = new File("SubImage.png"); ImageIO.write(sub, "png", f); final ImageIcon im = new ImageIcon(f.toURI().toURL()); Runnable r = new Runnable() { @Override//w w w . ja v a2s. c om public void run() { JOptionPane.showMessageDialog(null, new JLabel(im)); } }; SwingUtilities.invokeLater(r); }
From source file:JTextPaneWithIcon.java
public static void main(String args[]) { JFrame frame = new JFrame("TextPane Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); StyleContext context = new StyleContext(); StyledDocument document = new DefaultStyledDocument(context); Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE); Icon icon = new ImageIcon("yourFile.gif"); JLabel label = new JLabel(icon); StyleConstants.setComponent(labelStyle, label); try {//from w w w. ja va2 s .co m document.insertString(document.getLength(), "Ignored", labelStyle); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } JTextPane textPane = new JTextPane(document); textPane.setEditable(false); JScrollPane scrollPane = new JScrollPane(textPane); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { String mapUrlPath = "http://www.java2s.com/style/download.png"; URL mapUrl = new URL(mapUrlPath); BufferedImage mapImage = ImageIO.read(mapUrl); Image newMapImage = Toolkit.getDefaultToolkit() .createImage(new FilteredImageSource(mapImage.getSource(), new GrayToColorFilter(Color.red))); ImageIcon mapIcon = new ImageIcon(mapImage); ImageIcon newMapIcon = new ImageIcon(newMapImage); JPanel imagePanel = new JPanel(); imagePanel.add(new JLabel(mapIcon)); imagePanel.add(new JLabel(newMapIcon)); JOptionPane.showMessageDialog(null, imagePanel); }
From source file:Main.java
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); Graphics2D imageGraphics = image.createGraphics(); GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange); imageGraphics.setPaint(gp);/*from w w w. j a va 2s . c o m*/ imageGraphics.fillRect(0, 0, 400, 300); JLabel textLabel = new JLabel("java2s.com"); textLabel.setSize(textLabel.getPreferredSize()); Dimension d = textLabel.getPreferredSize(); BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); g.setColor(new Color(255, 200, 255, 128)); g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10); g.setColor(Color.black); textLabel.paint(g); Graphics g2 = image.getGraphics(); g2.drawImage(bi, 20, 20, f); ImageIcon ii = new ImageIcon(image); JLabel imageLabel = new JLabel(ii); f.getContentPane().add(imageLabel); f.pack(); f.setVisible(true); } }); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/*from w w w . j a v a 2s . co m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.USE_SELECTION); f.setSize(300, 400); f.setVisible(true); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/*from w w w . j av a 2s. c o m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.INSERT); f.setSize(300, 400); f.setVisible(true); }
From source file:DndTree.java
public static void main(String args[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel top = new JPanel(new BorderLayout()); JLabel dragLabel = new JLabel("Drag me:"); JTextField text = new JTextField(); text.setDragEnabled(true);/*from w w w .j a v a 2 s .co m*/ top.add(dragLabel, BorderLayout.WEST); top.add(text, BorderLayout.CENTER); f.add(top, BorderLayout.NORTH); final JTree tree = new JTree(); final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); tree.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); return dropLocation.getPath() != null; } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation(); TreePath path = dropLocation.getPath(); Transferable transferable = support.getTransferable(); String transferData; try { transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } int childIndex = dropLocation.getChildIndex(); if (childIndex == -1) { childIndex = model.getChildCount(path.getLastPathComponent()); } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent(); model.insertNodeInto(newNode, parentNode, childIndex); TreePath newPath = path.pathByAddingChild(newNode); tree.makeVisible(newPath); tree.scrollRectToVisible(tree.getPathBounds(newPath)); return true; } }); JScrollPane pane = new JScrollPane(tree); f.add(pane, BorderLayout.CENTER); tree.setDropMode(DropMode.ON_OR_INSERT); f.setSize(300, 400); f.setVisible(true); }
From source file:MainClass.java
public static void main(String argv[]) { java.net.URL u = null;/*from w w w . j a v a2s. c o m*/ try { u = new java.net.URL("http://www.java2s.com/"); } catch (java.net.MalformedURLException ignored) { } JFormattedTextField ftf1 = new JFormattedTextField(u); JFormattedTextField ftf2 = new JFormattedTextField(u); ftf2.setInputVerifier(new InputVerifier() { public boolean verify(JComponent input) { if (!(input instanceof JFormattedTextField)) return true; return ((JFormattedTextField) input).isEditValid(); } }); JPanel p = new JPanel(new GridLayout(0, 2, 3, 8)); p.add(new JLabel("plain JFormattedTextField:")); p.add(ftf1); p.add(new JLabel("FTF with InputVerifier:")); p.add(ftf2); p.add(new JLabel("plain JTextField:")); p.add(new JTextField(u.toString())); JFrame f = new JFrame("MainClass"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North"); f.getContentPane().add(p, "Center"); f.pack(); f.setVisible(true); }