List of usage examples for java.awt.event MouseAdapter MouseAdapter
MouseAdapter
From source file:Main.java
public static void main(String[] args) { final DefaultListModel<String> model = new DefaultListModel<>(); final JList<String> list = new JList<>(model); JFrame f = new JFrame(); model.addElement("A"); model.addElement("B"); model.addElement("C"); model.addElement("D"); model.addElement("E"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = ""; if (text != null) newitem = text.trim(); else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); ListSelectionModel selmodel = list.getSelectionModel(); selmodel.setLeadSelectionIndex(index); }//from www. j ava 2 s .com } } }); leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); leftPanel.add(new JScrollPane(list)); JButton removeall = new JButton("Remove All"); JButton add = new JButton("Add"); JButton rename = new JButton("Rename"); JButton delete = new JButton("Delete"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = JOptionPane.showInputDialog("Add a new item"); String item = null; if (text != null) item = text.trim(); else return; if (!item.isEmpty()) model.addElement(item); } }); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) model.remove(index); } }); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index == -1) return; Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = null; if (text != null) { newitem = text.trim(); } else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); } } }); removeall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.clear(); } }); rightPanel.add(add); rightPanel.add(rename); rightPanel.add(delete); rightPanel.add(removeall); rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); panel.add(leftPanel); panel.add(rightPanel); f.add(panel); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:List.java
public static void main(String[] args) { final DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); JFrame f = new JFrame(); f.setTitle("JList models"); model.addElement("A"); model.addElement("B"); model.addElement("C"); model.addElement("D"); model.addElement("E"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = ""; if (text != null) newitem = text.trim(); else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); ListSelectionModel selmodel = list.getSelectionModel(); selmodel.setLeadSelectionIndex(index); }//from w ww .ja v a 2 s . c o m } } }); leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); leftPanel.add(new JScrollPane(list)); JButton removeall = new JButton("Remove All"); JButton add = new JButton("Add"); JButton rename = new JButton("Rename"); JButton delete = new JButton("Delete"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = JOptionPane.showInputDialog("Add a new item"); String item = null; if (text != null) item = text.trim(); else return; if (!item.isEmpty()) model.addElement(item); } }); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) model.remove(index); } }); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index == -1) return; Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = null; if (text != null) { newitem = text.trim(); } else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); } } }); removeall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.clear(); } }); rightPanel.add(add); rightPanel.add(rename); rightPanel.add(delete); rightPanel.add(removeall); rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); panel.add(leftPanel); panel.add(rightPanel); f.add(panel); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:tk.mystudio.ocr.OCRTest.java
public static void main(String[] args) { JFrame frame = new JFrame("??"); final JLabel label = new JLabel(new ImageIcon(), JLabel.CENTER); byte[] image = client.getCodeByte(Constants.LOGIN_CODE_URL); String randCodeByRob = OCR.read(image); label.setIcon(new ImageIcon(image)); label.setText(randCodeByRob);//from ww w . j a v a2 s. c om label.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { byte[] image = client.getCodeByte(Constants.LOGIN_CODE_URL); String randCodeByRob = OCR.read(image); label.setIcon(new ImageIcon(image)); label.setText(randCodeByRob); System.out.println(randCodeByRob); } }); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static final void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane textPane = new JTextPane(); textPane.addMouseMotionListener(new MouseAdapter() { public void mouseMoved(MouseEvent e) { AttributeSet style = getAttributes(e); if (style != null && StyleConstants.getIcon(style) != null) { textPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } else { textPane.setCursor(Cursor.getDefaultCursor()); }/* www . j a va 2 s .c o m*/ } }); frame.add(new JScrollPane(textPane)); StyledDocument doc = (StyledDocument) textPane.getDocument(); SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setIcon(style, createImage()); doc.insertString(doc.getLength(), "this is a test", null); doc.insertString(doc.getLength(), "test", style); doc.insertString(doc.getLength(), "this is a test\n", null); doc.insertString(doc.getLength(), "another image", style); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:SelectingJListSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; JFrame frame = new JFrame("Selecting JList"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JList jlist = new JList(labels); JScrollPane scrollPane1 = new JScrollPane(jlist); contentPane.add(scrollPane1, BorderLayout.WEST); ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { System.out.print("First index: " + listSelectionEvent.getFirstIndex()); System.out.print(", Last index: " + listSelectionEvent.getLastIndex()); boolean adjust = listSelectionEvent.getValueIsAdjusting(); System.out.println(", Adjusting? " + adjust); if (!adjust) { JList list = (JList) listSelectionEvent.getSource(); int selections[] = list.getSelectedIndices(); Object selectionValues[] = list.getSelectedValues(); for (int i = 0, n = selections.length; i < n; i++) { if (i == 0) { System.out.print(" Selections: "); }/*from www . j a v a2 s. co m*/ System.out.print(selections[i] + "/" + selectionValues[i] + " "); } System.out.println(); } } }; jlist.addListSelectionListener(listSelectionListener); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent mouseEvent) { JList theList = (JList) mouseEvent.getSource(); if (mouseEvent.getClickCount() == 2) { int index = theList.locationToIndex(mouseEvent.getPoint()); if (index >= 0) { Object o = theList.getModel().getElementAt(index); System.out.println("Double-clicked on: " + o.toString()); } } } }; jlist.addMouseListener(mouseListener); frame.setSize(350, 200); frame.setVisible(true); }
From source file:MenuDemo1.java
public static void main(String[] args) { // Create a window for this demo JFrame frame = new JFrame("Menu Demo"); JPanel panel = new JPanel(); frame.getContentPane().add(panel, "Center"); // Create an action listener for the menu items we will create // The MenuItemActionListener class is defined below ActionListener listener = new MenuItemActionListener(panel); // Create some menu panes, and fill them with menu items // The menuItem() method is important. It is defined below. JMenu file = new JMenu("File"); file.setMnemonic('F'); file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N)); file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O)); file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S)); file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A)); JMenu edit = new JMenu("Edit"); edit.setMnemonic('E'); edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X)); edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C)); edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V)); // Create a menubar and add these panes to it. JMenuBar menubar = new JMenuBar(); menubar.add(file);/*from ww w . ja v a 2s. co m*/ menubar.add(edit); // Add menubar to the main window. Note special method to add menubars frame.setJMenuBar(menubar); // Now create a popup menu and add the some stuff to it final JPopupMenu popup = new JPopupMenu(); popup.add(menuItem("Open...", listener, "open", 0, 0)); popup.addSeparator(); // Add a separator between items JMenu colors = new JMenu("Colors"); // Create a submenu popup.add(colors); // and add it to the popup menu // Now fill the submenu with mutually-exclusive radio buttons ButtonGroup colorgroup = new ButtonGroup(); colors.add(radioItem("Red", listener, "color(red)", colorgroup)); colors.add(radioItem("Green", listener, "color(green)", colorgroup)); colors.add(radioItem("Blue", listener, "color(blue)", colorgroup)); // Arrange to display the popup menu when the user clicks in the window panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // Check whether this is the right type of event to pop up a popup // menu on this platform. Usually checks for right button down. if (e.isPopupTrigger()) popup.show((Component) e.getSource(), e.getX(), e.getY()); } }); // Finally, make our main window appear frame.setSize(450, 300); frame.setVisible(true); }
From source file:ImageSelection.java
public static void main(String args[]) { JFrame frame = new JFrame("Drag Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon icon = new ImageIcon("yourFile.gif"); 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); }/* www . j av a2 s . c o m*/ }; label.addMouseListener(listener); frame.add(new JScrollPane(label), BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:com.qawaa.gui.PointAnalysisGUI.java
/** * Auto-generated main method to display this JFrame *///from ww w . j a v a2 s. com public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { PointAnalysisGUI inst = new PointAnalysisGUI(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFrame.setDefaultLookAndFeelDecorated(true); FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(FlowLayout.LEFT); flowLayout.setAlignOnBaseline(true); inst.setTitle(PROGRAM_NAME + " [" + PROGRAM_VERSION + "] " + " - " + DefaultMessage.SOFT_NAME + " - " + DefaultMessage.COMPANY_NAME); { consoleScrollPane = new JScrollPane(); inst.getContentPane().add(consoleScrollPane, BorderLayout.CENTER); { consolePane = new JEditorPane(); consoleScrollPane.setViewportView(consolePane); consolePane.setText(""); setConsoleRight(); jConsole = new JConsole(System.out, consolePane); System.setOut(jConsole); System.setErr(jConsole); consolePane.setEditable(false); consolePane.addMouseListener(new MouseAdapter() { /* (non-Javadoc) * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent) */ public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { consolePane.add(consoleRight); consoleRight.show(e.getComponent(), e.getX(), e.getY()); } } }); } } { infoPanel = new JPanel(); inst.getContentPane().add(infoPanel, BorderLayout.SOUTH); infoPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, false)); infoPanel.setPreferredSize(new Dimension(784, 30)); infoPanel.setLayout(flowLayout); { { statusbar_count = new JTextPane(); infoPanel.add(statusbar_count); statusbar_count .setText(CONTEXT.getMessage("point.statusbar.count", null, Locale.CHINA)); statusbar_count.setBackground(null); statusbar_count.setEditable(false); } { statusbar_count_value = new JTextPane(); infoPanel.add(statusbar_count_value); statusbar_count_value.setText(String.valueOf(SCAN_COUNT)); statusbar_count_value.setBackground(null); statusbar_count_value.setEditable(false); statusbar_count_value.setEnabled(false); } { programPID = new JTextPane(); infoPanel.add(programPID); programPID.setText("PID:"); programPID.setBackground(null); programPID.setEditable(false); } { programPID_value = new JTextPane(); infoPanel.add(programPID_value); programPID_value.setText(JvmPid.getPID()); programPID_value.setBackground(null); programPID_value.setEditable(false); programPID_value.setEnabled(false); } { memory = new JTextPane(); infoPanel.add(memory); memory.setText(CONTEXT.getMessage("gobal.gui.memory", null, Locale.CHINA)); memory.setBackground(null); memory.setEditable(false); } { memory_value = new JTextPane(); infoPanel.add(memory_value); memory_value.setText("0KB"); memory_value.setBackground(null); memory_value.setEditable(false); memory_value.setEnabled(false); MemoryListener memory = new MemoryListener(memory_value); memory.start(); } { runtime = new JTextPane(); infoPanel.add(runtime); runtime.setText(CONTEXT.getMessage("gobal.gui.runtime", null, Locale.CHINA)); runtime.setBackground(null); runtime.setEditable(false); } { runtime_value = new JTextPane(); infoPanel.add(runtime_value); runtime_value.setText("NULL"); runtime_value.setBackground(null); runtime_value.setEditable(false); runtime_value.setEnabled(false); } } } { shortcut = new JPanel(); inst.getContentPane().add(shortcut, BorderLayout.NORTH); shortcut.setSize(784, 35); shortcut.setPreferredSize(new Dimension(784, 35)); shortcut.setBorder(new LineBorder(new Color(0, 0, 0), 1, false)); shortcut.setLayout(new BorderLayout()); { eventText = new JTextPane(); shortcut.add(eventText, BorderLayout.WEST); eventText.setText(CONTEXT.getMessage("point.event.name", null, Locale.CHINA) + ": "); eventText.setEditable(false); eventText.setEnabled(true); eventText.setBackground(null); eventText.setCaretColor(new Color(0, 0, 0)); } { eventTextField = new JTextField(); shortcut.add(eventTextField, BorderLayout.CENTER); eventTextField.setSize(500, 25); eventTextField.setText(""); eventTextField.setPreferredSize(new Dimension(500, 25)); eventTextField.setEditable(false); } { submit = new JButton(); shortcut.add(submit, BorderLayout.EAST); submit.setText(CONTEXT.getMessage("gobal.gui.button.run", null, Locale.CHINA)); submit.setSize(new Dimension(75, 25)); submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { submitActionPerformed(evt); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } } }); }
From source file:com.qawaa.gui.EventWebScanGUI.java
/** * Auto-generated main method to display this JFrame *//*from www. j a v a 2s . c om*/ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { EventWebScanGUI inst = new EventWebScanGUI(); inst.setLocationRelativeTo(null); inst.setVisible(true); inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFrame.setDefaultLookAndFeelDecorated(true); FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(FlowLayout.LEFT); flowLayout.setAlignOnBaseline(true); inst.setTitle(PROGRAM_NAME + " [" + PROGRAM_VERSION + "] " + " - " + DefaultMessage.SOFT_NAME + " - " + DefaultMessage.COMPANY_NAME); { consoleScrollPane = new JScrollPane(); inst.getContentPane().add(consoleScrollPane, BorderLayout.CENTER); { consolePane = new JEditorPane(); consoleScrollPane.setViewportView(consolePane); consolePane.setText(""); setConsoleRight(); jConsole = new JConsole(System.out, consolePane); System.setOut(jConsole); System.setErr(jConsole); consolePane.setEditable(false); consolePane.addMouseListener(new MouseAdapter() { /* (non-Javadoc) * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent) */ public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { consolePane.add(consoleRight); consoleRight.show(e.getComponent(), e.getX(), e.getY()); } } }); } } { infoPanel = new JPanel(); inst.getContentPane().add(infoPanel, BorderLayout.SOUTH); infoPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, false)); infoPanel.setPreferredSize(new Dimension(784, 30)); infoPanel.setLayout(flowLayout); { { statusbar_count = new JTextPane(); infoPanel.add(statusbar_count); statusbar_count.setText( CONTEXT.getMessage("event.web.scan.statusbar.count", null, Locale.CHINA)); statusbar_count.setBackground(null); statusbar_count.setEditable(false); } { statusbar_count_value = new JTextPane(); infoPanel.add(statusbar_count_value); statusbar_count_value.setText(String.valueOf(SCAN_COUNT)); statusbar_count_value.setBackground(null); statusbar_count_value.setEditable(false); statusbar_count_value.setEnabled(false); } { programPID = new JTextPane(); infoPanel.add(programPID); programPID.setText("PID:"); programPID.setBackground(null); programPID.setEditable(false); } { programPID_value = new JTextPane(); infoPanel.add(programPID_value); programPID_value.setText(JvmPid.getPID()); programPID_value.setBackground(null); programPID_value.setEditable(false); programPID_value.setEnabled(false); } { memory = new JTextPane(); infoPanel.add(memory); memory.setText(CONTEXT.getMessage("gobal.gui.memory", null, Locale.CHINA)); memory.setBackground(null); memory.setEditable(false); } { memory_value = new JTextPane(); infoPanel.add(memory_value); memory_value.setText("0KB"); memory_value.setBackground(null); memory_value.setEditable(false); memory_value.setEnabled(false); MemoryListener memory = new MemoryListener(memory_value); memory.start(); } { runtime = new JTextPane(); infoPanel.add(runtime); runtime.setText(CONTEXT.getMessage("gobal.gui.runtime", null, Locale.CHINA)); runtime.setBackground(null); runtime.setEditable(false); } { runtime_value = new JTextPane(); infoPanel.add(runtime_value); runtime_value.setText("NULL"); runtime_value.setBackground(null); runtime_value.setEditable(false); runtime_value.setEnabled(false); } } } { shortcut = new JPanel(); inst.getContentPane().add(shortcut, BorderLayout.NORTH); shortcut.setSize(784, 35); shortcut.setPreferredSize(new Dimension(784, 35)); shortcut.setBorder(new LineBorder(new Color(0, 0, 0), 1, false)); shortcut.setLayout(new BorderLayout()); { eventText = new JTextPane(); shortcut.add(eventText, BorderLayout.WEST); eventText.setText(CONTEXT.getMessage("event.web.scan.name", null, Locale.CHINA) + ": "); eventText.setEditable(false); eventText.setEnabled(true); eventText.setBackground(null); eventText.setCaretColor(new Color(0, 0, 0)); } { eventTextField = new JTextField(); shortcut.add(eventTextField, BorderLayout.CENTER); eventTextField.setSize(500, 25); eventTextField.setText(""); eventTextField.setPreferredSize(new Dimension(500, 25)); eventTextField.setEditable(false); } { submit = new JButton(); shortcut.add(submit, BorderLayout.EAST); submit.setText(CONTEXT.getMessage("gobal.gui.button.run", null, Locale.CHINA)); submit.setSize(new Dimension(75, 25)); submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { submitActionPerformed(evt); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } } }); }
From source file:Main.java
/** * Gives a component a popup menu/*from w ww .j av a 2s . c o m*/ * * @param component * The target component * @param popup * The popup menu */ public static void setPopupMenu(final JComponent component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { @Override public void mousePressed(final MouseEvent e) { if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } @Override public void mouseReleased(final MouseEvent e) { if (e.isPopupTrigger()) { popup.show(component, e.getX(), e.getY()); } } }); }