Help in Swing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crochunter
    New Member
    • May 2009
    • 21

    Help in Swing

    Hi,

    I have to add a mouse over event and mouse click event in my rectangles that I draw while taking the values from an input file. Now since I am taking rectangle height and width from the input text file as well I also want to take a text value to be shown while moving a mouse over a particular rectangle from my input file. Also when user clicks on that rectangle the height and width of that particular rectangle only should be displayed (re-adjusted).

    Like suppose my input file has 3 fields:-
    Code:
    Height   Width  Test_To_Show
    10          23         Hello
    20          44         Bye
    Now When my paint component draws these 2 rectangles and a user moves his mouse over the rectangle he should see text message attached to it:- Like for first row 10, 23 he should see "Hello". In this case when he clicks on this rectangle the picture should read just to show only 10 and 20 as height and width. Now the problem is I am confused where to add the mouse and mouse click event function.
    I am planning to add it like this in paintcomponent method:-

    Code:
    for(int i=0; i<myarraylist.size(); i++){
                Coloring fc = myarraylist.get(i);
                GradientPaint gpi = new GradientPaint(0, 0,c.getColor() , 0, 20, Color.white, true);
                g2d.setPaint(gpi);
                g2d.drawRect(fc.value1, 29, fc.mywidth, 83);
                g2d.fillRect(fc.value1, 29, fc.mywidth, 83);
                fc.addMouseMotionListener(this);
                fc.addMouseListener(this);
                                                }
    and then write a new functions like this:-
    Code:
          public void mouseEntered(MouseEvent e) {
                  //I will be reading from a file and then pass the string that I want to show as text while mouse over
                   String s = // takes value from the inpuf file.
                      fc.setText(s);
                                                 }
    public void mouseClicked(MouseEvent e) {
                 //same way reading from the file ... Not sure how I will do this one
                 }
    Would it be the correct way to do it ? I am doing mouse over functions for the first time so donno much about there usage.

    I would be enlightened to get help and advice smile.gif
    I am extending my JPanel like this:-
    Code:
    public class Testing extends JPanel implements MouseListener {
    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Start by reading How to write a mouse listener.
    Last edited by r035198x; Jun 2 '09, 09:03 AM. Reason: be->by

    Comment

    • crochunter
      New Member
      • May 2009
      • 21

      #3
      Hi

      Thanks. Plz do send me some other useful links if u think they will help me.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        That one is probably best for now. Also see the mouse motion listener section after that page.

        Comment

        Working...