mouse-listeners

How to check if and which mouse button is pressed in Swing

守給你的承諾、 提交于 2021-02-17 05:47:46
问题 How can I check if currently any mouse button is pressed and if so, which one it is? The thing is that I need to use this kind of information in MouseListener.mouseEntered() . I checked MouseEvent but I could not find the method which would help me. The getButton() method seems to only return value if there has been change in buttons' state. Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods. 回答1: How can I

Click hyperlink in jtable?

北城以北 提交于 2020-06-25 03:52:30
问题 How can I enable hyperlink for every record in the JTable ? What I want to do is such that a user can click on the hyperlink which will then display the information they can edit/update. Alternatively how can I enable in place editing of the table data? Another question is i am currently using the following way to display different screen. But this is not an elegant way i understand we should use cardlayout but how exactly to go about it? mainPanel.setVisible(false); createBlogEntryPanel

How do I get my image to follow my mouse?

百般思念 提交于 2020-01-13 06:37:10
问题 How do I get my image to follow my mouse anywhere on the screen? The below code makes the image move along the x axis. import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public class PlayerTwo implements KeyListener, MouseListener, MouseMotionListener{ public static int PLAYER_HEIGHT = 15;

Event listeners and Mouse listeners

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 06:59:14
问题 I was wondering if it is possible to check if a JButton is double clicked using an event Listener instead of a mouse listener. Consider the following code; public void actionPerformed(ActionEvent arg0){ if (arg0.getClickCount() == 2){ System.out.println("You Doubled clicked"); } } I get an error saying getClickCount() is undefined for the type ActionEvent . Is the click or doubleclick of a mouse not also considered as an event? Thoughts. 回答1: You want to use a MouseAdapter . It permits you to

repaint JPanel with every click at JList

只愿长相守 提交于 2019-12-31 03:53:05
问题 everytime i click on a JList item, i need to clear + refresh my current panel & load another panel, returned via method 'populateWithButtons()'. temp is an int variable that stores what was clicked at the JList. How do i rectify the following? list_1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { //refresh + populate JPanel Food food = new Food(); JPanel panel2 = new JPanel(); JPanel pane11 = new JPanel(); panel2.add(panel1); panel1.validate();

Drag and Resize undecorated JFrame

ε祈祈猫儿з 提交于 2019-12-28 03:59:05
问题 Currently, I am using the following code to drag and move my undecordated JFrames. private void initialiseGUI(Component component){ //<editor-fold defaultstate="collapsed" desc="code"> component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { posX = e.getX(); posY = e.getY(); } }); component.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent evt) { //sets frame position when mouse dragged Rectangle rectangle = getBounds(); getGUI()

Getting the colour of where you have clicked on a JPanel?

匆匆过客 提交于 2019-12-22 18:27:12
问题 I only want a method to activate if the pixel that is clicked is white. How would I implement this? Trying to look for a method that returns the colour at a coord, but I can't find one. 回答1: Depends. If you have got a BufferedImage (or some other Image) you can use its getRGB methods (or getRaster().getPixel ). If you haven't, you can use JPanel's createImage methods and use the returned image to get the pixel data. 来源: https://stackoverflow.com/questions/2623351/getting-the-colour-of-where

How do I create a JPanel with two images where only a part of the one below is shown on mouse over?

三世轮回 提交于 2019-12-19 11:49:09
问题 I am trying to create a button panel where the button that was clicked becomes 'differently colored'; i.e show the background image. p.s I only need this approach(with 2 images), and not anything else. Thanks ! Eg: public class TestPane extends JPanel { private BufferedImage imgUnclicked; private BufferedImage imgClicked; private Point mousePoint; public TestPane() { try { imgUnclicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Uncolored.png")); imgClicked = ImageIO.read(new File(

.drawLine() issues and buffered image

两盒软妹~` 提交于 2019-12-17 21:28:38
问题 I have a paint programme and i have all the buttons and sliders done however i am having a problem with the actual painting itself. When I drag the cursor across the screen instead of an unbroken line I am getting almost a dotted line which i dont want. Here's the code for the MouseListener in the JPanel and BufferedImage : public void mouseDragged(MouseEvent e) { Graphics g=buffered.getGraphics(); g.setColor(mycol); Graphics2D graph=(Graphics2D)g; BasicStroke stroke=new BasicStroke(30);