jim164
jim164

Reputation: 11

Gridlayout and mouse listeners

hi i am trying to build a grid-layout GUI with mouse listener. SO when a particular cell is clicked in a grid ,information would be displayed. I don't know where to start, any help would be good thankyou

Upvotes: 0

Views: 242

Answers (3)

xenteros
xenteros

Reputation: 15842

I believe you have a class that inherits from JPanel or JFrame and there is whole GUI in it. Then, this class should implement mouseListener. Then your class should have similar code:

@override
public void mouseClicked(MouseEvent e){}
@override
public void mousePressed(MouseEvent e){}
@override
public void mouseEntered(MouseEvent e){}
@override
public void mouseReleased(MouseEvent e){
    /*This method is being called when you release your click. It's better
      then mouseClicked because mouseClicked is only called when you press
      and release on the same pixel or Object (not sure about it)
    */
}
@override
public void mouseExiteded(MouseEvent e){}

In each method you can get source of

MouseEvent e

using

Object source = e.getSource();
if (source == button1){
    //Do sth
}if (source == button2){
    //Do sth else
}if (source == radioButton1){
    //Do whatever you want
}

Then you have reference to the source, so you can modify what you want.

Upvotes: 1

Normal design
Normal design

Reputation: 148

To use properly a gridbaglayout, you should first work on the gridbagconstraints. Then, you should use the ActionListener interface to handle the mouse clicks. If the cells are of the type Labels, you coud hide the text by using myLabel.setText("") and putting the text by using myLabel.setText("information to display"). If you need more help, just ask :D and +1 if it helps ^^

Upvotes: 0

RehanZahoor
RehanZahoor

Reputation: 412

In your gridlayout, set all grids with some Component such as Button or Label. You can set listeners on the components added and display information when a component is clicked

Upvotes: 0

Related Questions