how to detect if someone clicks on a jpanel in java
Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame{
public TestFrame(int size){
JPanel content = new JPanel(new GridLayout(size, size));
JPanel[] panel = new JPanel[size * size];
PanelListener listener = new PanelListener();
for(int i = 0; i < panel.length; i++){
panel[i] = new JPanel();
panel[i].setBackground(Color.white);
panel[i].addMouseListener(listener);
content.add(panel[i]);
}
this.add(content);
}
// MouseListener offers the method mouseClicked(MouseEvent e)
private class PanelListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent event) {
/* source is the object that got clicked
*
* If the source is actually a JPanel,
* then will the object be parsed to JPanel
* since we need the setBackground() method
*/
Object source = event.getSource();
if(source instanceof JPanel){
JPanel panelPressed = (JPanel) source;
panelPressed.setBackground(Color.blue);
}
}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
}
public static void main(String[] args){
TestFrame theGUI = new TestFrame(8);
theGUI.setTitle("Grid");
theGUI.setVisible(true);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setSize(400,400);
}
}
Also in Java:
- Title
- java switch display panel
- Category
- Java
- Title
- how to know if String is the same java
- Category
- Java
- Title
- check if object is empty java 8
- Category
- Java
- Title
- array rotation program in java
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- java uuid
- Category
- Java
- Title
- java random char a-z
- Category
- Java
- Title
- java mockito any string import
- Category
- Java
- Title
- protect java
- Category
- Java
- Title
- Write a method that raises a number to a power without using Math.pow() method
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- java observable to observer
- Category
- Java
- Title
- java check if a line is enclosed in quotation marks
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- how lock horizontal orientation android
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- split by new line java
- Category
- Java
- Title
- jenkins decrypt password script console
- Category
- Java
- Title
- remove part of string java
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- fibonacci sequence java using recursion
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- java android development get element by id
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- print values of bst java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- check if a char is a space java
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- byte java
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- java 8 group a collections by 2 property
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- java merge sort
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- big integer java
- Category
- Java
- Title
- java split array into two
- Category
- Java
- Title
- how to create an array of arraylist in java
- Category
- Java
- Title
- how to change double to int in java
- Category
- Java
- Title
- get today date in java 8
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- initialiser une arraylist
- Category
- Java
- Title
- java print syntax
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- generate random number java
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- contains example in java
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- java split into arraylist
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- java list to set
- Category
- Java
- Title
- java parse json
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- pass list to intent in android java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- get sha key android
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- iterator for array java
- Category
- Java
- Title
- java string to int
- Category
- Java
- Title
- java empty array
- Category
- Java
- Title
- java create circle
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- close scanner java
- Category
- Java
- Title
- nth prime number java
- Category
- Java
- Title
- java run project from command line
- Category
- Java
- Title
- vector length java
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- int to binary java
- Category
- Java
- Title
- check if optional is empty java
- Category
- Java
- Title
- java arraylist in enum
- Category
- Java
- Title
- initialize applet in java
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- how to get witdth of window android
- Category
- Java
- Title
- ecommerce app github android
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- javafx action event enter key
- Category
- Java
- Title
- set color of text for jlabel
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java