-1

Hi i have a class where i am using mouseclick event i want to call another class when i click from my mouse

     MouseListener mouseListener = new MouseAdapter() {
  public void mouseClicked(MouseEvent mouseEvent) {
    JList theList = (JList) mouseEvent.getSource();
    if (mouseEvent.getClickCount() == 2) {
      int index = theList.locationToIndex(mouseEvent.getPoint());
      if (index >= 0) {
        Object o = theList.getModel().getElementAt(index);
      //  System.out.println("Double-clicked on: " + o.toString());
         String a=o.toString();


                 LiistSelection.setListIndex(a);
                System.out.println(LiistSelection.getListIndex());
                new MyGui4();
      }
    }
  }
};

i want to call this class when user click on list then new window should open

here is my class mygui4.java

public class MyGui4 extends JFrame
{
JLabel jLabel1;


Container pane;

 private static ResultSet resultSet = null;
 public void Gui(  )
  {
      {
getContentPane().setBackground(new java.awt.Color(255,153,51));
} 
 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container c = getContentPane();

      setUndecorated(true);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      setBounds(0,0,screenSize.width, screenSize.height);
ImageIcon image = new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
       Border border = LineBorder.createGrayLineBorder();
       jLabel1 = new JLabel(image);
       jLabel1.setBorder(border);
      jLabel1.setBackground(Color.red);
     c.add(jLabel1);
     setLayout(null);
  }

  public static void main( String[] args )
  {
      final MyGui4 frame = new MyGui4();

     frame.Gui();

      frame.setVisible(true);

  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

0

You want to Create a object of another Class and call a function using a object.

  class second 
      { 
       //.....
       public void function()
          {
       //........
          }
       public void function(int index)
         {
       //..........
         }
     }
    second s=new second();
    s.function()//calling function 
    int i=10;
    s.function(i)//calling function with parameter

Try This Example :

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class m  extends JFrame 
     {
      String s="The Value of List is  10";
      m()
        {
        setVisible(true);
        pack();
        setLayout(null);
        JButton b=new JButton("Click to Open another form");
        b.setBounds(10,10,200,40);
        add(b);
        b.addMouseListener(new MouseAdapter() 
            {
            public void mouseClicked(MouseEvent e)
              {
                  new s(s);//calling another class contructor
              }
              });
           }
          public static void main (String[] args) 
          {
           new m();
          } 
       }

   class s extends JFrame
   {
     s(String s)
     { 
      setVisible(true);
      setSize(100,100);
      setTitle(s);
      }
   }

enter image description here Click The button Another Class and Open The Window enter image description here

Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

It looks to me like you are tying to invoke the class MyGui4 from the command line when you start the JVM or from another application when you click on the JList, If so then the code needs to be the same in both places.

When invoked from the command line the main() method is invoked which in turn invokes 3 lines of code:

final MyGui4 frame = new MyGui4();
frame.Gui();
frame.setVisible(true);

When you invoke the code when clicking on the JList you invoke 1 line of code:

new MyGui4();

Can you tell me what the difference is?

Of course I still don't understand the point of this code because none of the methods in your MyGui4 class accept a parameter. So it doesn't matter which item in the JList you click on you will still display the same GUI with the same information. You need to pass the selected object from your JList to your GUI.

camickr
  • 321,443
  • 19
  • 166
  • 288