FAQ Database Discussion Community


How to get mouse position in the same place with different resolutions

java,coordinates,screen-resolution,java-2d,mouselistener
I developed a Monopoly Clone game (Bolivian monopoly) for the university. I put the monopoly board into Jpanel and try to get the mouse position in the exactly coordinate using e.getX() into the mouse listener event. the problem is that my game supports a loot of resolution 800x600, 1024x768, 1680x1050...

Add mouseListener to Labels in Array Loop

java,loops,mouselistener
I want to add mouseListener to all labels in the array. Every label should show an other card of the layout. If I use below code, all labels show card6. What is wrong? sorry this is correct code.. panList = new JPanel(); panList.setBounds(0, 0, 206, 517); panList.setLayout(null); cs.add(panList); CreateCards(); int...

Adding mouselistener to canvas with lwjgl display on it

java,lwjgl,mouselistener
I'm using java and LWJGL. I created a mouse listener for a canvas, but when I set the Displays parent to the canvas it doesn't work. The mousehandlerClass: private static class handlerClass implements MouseListener { public handlerClass() { } @Override public void mouseClicked(MouseEvent e) { System.out.println("Canvas clicked"); } @Override public...

Mouslistener causing problems

java,swing,mouseevent,mouselistener
I've have created a GoInvisible class in my code which implements a mouse listener, i'm tryna use the mouse pressed and the mouse released methods to get my frame to go transparent and then return to normal on the press and release of a button on the frame. i'm invoking...

Anonymous Listener inside for loop

java,mouselistener
This may be a stupid question but in my code when mouseClicked is performed on a panel will the item from the loop condition be the same as the item parameter of ItemDialog? for (Item item: itemsList) { JPanel panel = new ItemPanel(item); panel.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent arg0)...

How to use the value of the variable from “for loop” for mouseEntered?

java,for-loop,mouselistener
How to get the "i" variable used in "for loop" for mouseEntered inside the panelResult[i].addMouseListener? Thanks! buttonCategory[0].addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { //hidden codes here... panelCategory.remove(scrollPane); panelCategory.add(scrollPane); panelArray.removeAll(); panelResult = new JPanel[totalCount]; int y = 0; for(int i = 0; i < panelResult.length; i++){ panelResult[i] = new...

JTree select node with mouse event

java,swing,jtree,mouselistener
I'm trying to track the node the user clicks on with a mouse listener on a JTree. The click event works, but I'm not able to select the node in the tree. public FileTreeController(JTree t) { this.myTree = t; this.myTree.setCellRenderer(new FileTreeCellRenderer()); this.myTree.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { doMouseClicked(me);...

How do i add a mouseListener to a anonymous JLabel object?

java,swing,awt,jlabel,mouselistener
Currently I'm using a for-loop to populate the JPanel with numbers from 1-31 So basically, What I want to do is if let say i click on number 1, it will do show System.out.println(1); Here's the code: public class MonthPanel extends JPanel implements MouseListener { public MonthPanel() { setLayout(new GridLayout(6,7));...

MouseListener on a drawString() Method

java,graphics2d,mouselistener
How can I detect if the text ("Resume", "Restart", "Quit") that I drew with a drawString() method is being clicked? My code so far: public class Pause { public Pause() { } public void draw(Graphics2D g) { g.setFont(new Font("Arial", Font.BOLD, 14)); int intValue = Integer.parseInt( "ff5030",16); g.setColor(new Color(intValue)); g.drawString("Resume", 200,...

Clearing text on mouse click from multiple JTextFields in Java

java,swing,focus,jtextfield,mouselistener
I'm doing some error checking in two JTextFields. I'll label them jtext1 and jtext2. Right now my GUI looks like this: Enter number 1: [ ] // jtext1 Enter number 2: [ ] // jtext2 Convert [ answer ] I want to be able to check for multiple conditions and...

How to write a MouseListener for JavaFX

javafx,mouseevent,java-8,mouse,mouselistener
I want to write a little game where I schoot from an object to a specific direction on a JavaFX Panel using my mouse position. I want to turn a Line in the direction where my Mouse is. Line line = new Line(startPosX, startPosY, mouseDirectionX, mouseDirectionY); How can I do...

GUI Calendar DefaultTableModel Addig MouseListeners

java,swing,jtable,mouselistener,jcalendar
So I followed a tutorial about how to create a GUI Calendar, but now I want to take it a step further. I would like to be able to click on the individual days in the calendar in order to prompt the user to add an event. But I don't...

Mouse click coordinates are always 0

java,mouselistener,java-canvas
As you can see I added the a mouse listener to the game. import java.awt.Graphics; import java.awt.image.BufferStrategy; public class Game implements Runnable { private Display display; public int width, height; public String title; private boolean running = false; private Thread thread; private BufferStrategy bs; private Graphics g; // States public...

Java - Access datas of a MouseListener from another Component

java,swing,jpanel,mouseevent,mouselistener
I'm currently working on a level editor for a game. Currently, to create an Entity, you have to click on a creation JButton, then a form appears, where you enter its coordinate and its size. I would like to implement a mouse fonction. The user click on the creation JButton,...

Adding MouseMotionListener to paddle (Java Pong Game)

java,jframe,mouselistener,pong
I searched the entire web trying to find a solution but in the end i just ended up being much more confused and frustrated. I am trying to get my paddle to move (paddle_y). Paddle does not need an x coordinate and it just has to move up and down...

The same (int)doubles round up and down in Java

java,int,double,rounding,mouselistener
I'm having the weirdest problem with my game in java. :| I can get around it, so I will be able to continue on this game without a solution, but I just thought it was really weird. It's a 2D Minecraft-ish (I know, I know, it's just for practice) game,...

Get position of Click JTable while using Cell renderer

java,swing,jtable,mouselistener
I'm currently using a JTable in Java to display a large amount of text information, and as such have implemented text wrapping, using the following code: MyCellRenderer mcr = new MyCellRenderer(); table.getColumnModel().getColumn(0).setCellRenderer(mcr); class MyCellRenderer extends JTextArea implements TableCellRenderer { public MyCellRenderer() { setLineWrap(true); setWrapStyleWord(true); } public Component getTableCellRendererComponent(JTable table, Object...

Using the mouseClicked method in the JMapViewer does not update the getPosition return value

java,coordinates,mouselistener,mouseclick-event,jmapviewer
I have create a MouseInputAdapter listener to get coordinates in a JMapViewer map and create the MapMarker on that spot but even though I click on a different possition the value of the map.getPossition() method does not updates with the new values. My listener code: @Override public void mouseClicked(MouseEvent e)...

toggle button mouse listener null pointer exception

java,mouselistener,togglebutton
I'm creating a version of Minesweeper in java, but am having issues setting up the mouse listener for the toggleButtons. I need to do different things on left and right click, so this is what I've setup so far in my MyPanel class: import javax.swing.*; import java.awt.*; import java.awt.event.*; import...

Java Mouselistener not being added

java,swing,mouseevent,mouselistener
I've creating a new game class that is basically a GUI for my game. I'm wanting to add a mouselistener so that I can check when the user is clicking on different things. However, I'm struggling to get the basics working. Here is what I have so far. The game...

Temporarily disable or prevent repainting JViewPort on scrolling with a mouseDrag

java,swing,mouselistener,java-5,jviewport
I have written a MouseListener as defined below so that I can move a JButton around to reorder the components that are within the JPanel. The JPanel is within a JScrollPane so that when multiple components are added they can be scrolled. The problem I have is that when dragging...

Shortening a piece of code gives an error; How do I solve this issue? [duplicate]

java,swing,final,mouselistener
This question already has an answer here: A final counter in a for loop? 6 answers I have this piece of code in a method: switches[0].addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { if(switchstate[0] == false) { if((e.getX() >= OFFBUTTONLEFT && e.getX() <= OFFBUTTONRIGHT) && (e.getY() >= OFFBUTTONTOP && e.getY()...

JavaFX application, framework to relay data from a nativeMouseListener to JavaFX

events,javafx,frameworks,mouselistener
I'm making a JavaFX application and I want to do the following. I present a couple buttons, when they are pressed, the GUI will minimize and waits for a mouseClick from the JNativeHook library. When a mouse is clicked it's x,y coordinats needs to be relayed back to the javaFx...

Java addMouseListener accepts wierd argument in Oracle example

java,mouselistener
The following is an example of using mouseEvent from the Oracle website: I don't expect anyone to read through the entire code. I'm only interested at a particular line of the code below: addMouseListener(this); //WHY IS "this" ALLOWED AS AN ARGUMENT? In this line of code given by Oracle website,...

java swing action event after two jbuttons are clicked

java,swing,jbutton,mouselistener
I am trying to print "Hello" only when the jButton2 is clicked after the jButton1 and print "Bye" only when any other jButtons are clicked after jButton1. At the moment, the following code does not print anything. public void MouseClicked(java.awt.event.MouseEvent e){ if (e.getSource() == jButton1) { if (e.getSource() == jButton2)...

Is it possible to get mouse inputs from JFrame?

java,swing,jframe,mouselistener
Good day! I have experimenting with Swing and AWT, and I stumbled upon java.swing.JFrame and java.awt.event.MouseListener. Based on the following code: Game.java (...) public Game() { frame = new JFrame(); ih = new IH(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); (...) frame.add(this); frame.addMouseListener(ih); } IH.java import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class IH implements...

AWT Frame does not handle events

java,awt,mouselistener
The frame opens and close normally but mouse click doesn't work. import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //Create a frame window that responds to mouse click public class AWT3 extends Frame { String Mmsg=""; int mouseX=0, mouseY=0; public AWT3() { addWindowListener(new MyWindowwAdapter(this)); addMouseListener(new MyMouseeAdapter(this));...

mouse listener does not listen

java,swing,listener,mouselistener
I have a school assignment that I'm stuck with. I'm aiming for a MVC implementation but right now I'm just doing it all in the view (just to make thing simple for now). So - I have a frame and it has a panel. The panel has a list of...

JApplet does not receive mouse-events

java,ubuntu,mouselistener
I'm trying to listen to MouseEvents in Java on a JApplet in a JFrame. My program is actually doing more stuff, but I've simplified it for you to this: import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class mouse extends JApplet implements MouseListener, MouseMotionListener { static final private int...

Java statement causes a mouseClick to be a mouseRelease for MouseListener in Jpanel

java,swing,mouselistener
i have added a mouse listener to my JPanel in the following way. What is outputed to JPanel is a image painted on screen from an array of images. public class NewFrame extend JFrame { public New JFrame() { statusBar = new JLabel("Mouse Outdide Panel"); //displays events for mouse add(statusBar,BorderLayout.SOUTH);...