mouselistener

How to programmatically fire a MouseEvent to a MouseListener with Java?

孤街醉人 提交于 2019-12-30 08:53:09
问题 I have a JTree with a custom associated MouseListener (for showing popup etc.). I need to fire a MouseEvent that will be caught by the MouseListener . How should I do that programmatically? 回答1: You could create your own MouseEvent and loop through all the listeners and make the call. For example: MouseEvent me = new MouseEvent(tree, 0, 0, 0, 100, 100, 1, false); for(MouseListener ml: tree.getMouseListeners()){ ml.mousePressed(me); } 回答2: The Robot class might be what you're looking for. This

How to programmatically fire a MouseEvent to a MouseListener with Java?

僤鯓⒐⒋嵵緔 提交于 2019-12-30 08:52:19
问题 I have a JTree with a custom associated MouseListener (for showing popup etc.). I need to fire a MouseEvent that will be caught by the MouseListener . How should I do that programmatically? 回答1: You could create your own MouseEvent and loop through all the listeners and make the call. For example: MouseEvent me = new MouseEvent(tree, 0, 0, 0, 100, 100, 1, false); for(MouseListener ml: tree.getMouseListeners()){ ml.mousePressed(me); } 回答2: The Robot class might be what you're looking for. This

How do I get a PopupMenu to show up when I left-click on a TrayIcon in Java?

柔情痞子 提交于 2019-12-29 01:31:06
问题 Currently, the PopupMenu will show up when I right-click on the TrayIcon in the SystemTray. However, I want it to do the same when I left-click on the TrayIcon. I thought I might accomplish this by using a mouseListener on the TrayIcon, but I don't know what method to invoke in the mouseClicked event to achieve the desired results. icon = new TrayIcon(img, tooltip, popup); icon.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { popup.setEnabled(true); } }); Using

Move an Oval in java

非 Y 不嫁゛ 提交于 2019-12-28 06:54:10
问题 I made a mini code that draw oval and link each other , now i try to move the oval(Circle) but I have a problem (in coding) // Panneau.java public class Panneau extends JPanel { private int R = 20; private boolean isDrag = false; String text = "stack"; int x = 250, y = 200; int height = 50, width = 50; Random Rnd = new Random(); int rand=Rnd.nextInt(); int r=Math.abs(rand%250); int r2=Math.abs(rand%250); public Panneau() { addMouseListener(new MouseAdapter() { @Override public void

Java only addMouseListener once

匆匆过客 提交于 2019-12-25 08:58:02
问题 I have a combobox in my program, with three options CIRCLE, RECTANGLE, FREEHAND. Each option is connected to a mouselistener. If I swich between the three options the mouselisteners are causing me some problems. Therefore I would like to add a mouselistener only once (for example in the constructor or in the beginning of the method, or somewhere else). Is it even possible, and how would the code look like? If it is not possible, is there any other way I can solve it? public void

onclick i want to get the name of the JPanel that is present on the JFrame

别说谁变了你拦得住时间么 提交于 2019-12-25 05:54:54
问题 Below is what im trying to implement but its giving error on the line mentioned ; this is because the function doesnot get which Component.getName() is of ... String name=new String(); mntmOneToOne.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { String name=new String(); int count = arg0.getClickCount(); if (count == 1) { Component panel = (Component) arg0.getSource(); System.out.println(panel.getName()); } } }); 回答1: Get the clicked object via your

Java inline class calling method in parent class

一曲冷凌霜 提交于 2019-12-25 04:43:33
问题 I'm working on a little Swing application and am in need of some assistance. I have an inline class for a MouseListener and inside one of the methods I would like to call a method in the parent class, however, this is an instance of the MouseListener. class ParentClass { void ParentMethod() { //... swing_obj.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) { //Want to call this.methodX("str"), but //this is the instance of MouseListener } public void mouseEntered

Java inline class calling method in parent class

倾然丶 夕夏残阳落幕 提交于 2019-12-25 04:43:09
问题 I'm working on a little Swing application and am in need of some assistance. I have an inline class for a MouseListener and inside one of the methods I would like to call a method in the parent class, however, this is an instance of the MouseListener. class ParentClass { void ParentMethod() { //... swing_obj.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) { //Want to call this.methodX("str"), but //this is the instance of MouseListener } public void mouseEntered

Changing cell background on click in another cell

拟墨画扇 提交于 2019-12-25 04:03:25
问题 I am trying to build a 9x9 sudoku puzzle. For the UI part, I want to change the background of cell when clicked and return to normal when any other cell is clicked. Below is my Cell Class. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class Cell extends JPanel implements MouseListener{ Cell[][] cell; private int x;//x pos; private int y;//y pos; private int num=0; private JLabel lnum; private Color bg; private static int xpos=

Changing cell background on click in another cell

主宰稳场 提交于 2019-12-25 04:03:09
问题 I am trying to build a 9x9 sudoku puzzle. For the UI part, I want to change the background of cell when clicked and return to normal when any other cell is clicked. Below is my Cell Class. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class Cell extends JPanel implements MouseListener{ Cell[][] cell; private int x;//x pos; private int y;//y pos; private int num=0; private JLabel lnum; private Color bg; private static int xpos=