2 mouseListeners

add mouseListener on textfields that are on panels

Page 1 of 1

1 Replies - 708 Views - Last Post: 21 April 2009 - 04:26 PM Rate Topic: -----

#1 ahoest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-April 09

2 mouseListeners

Posted 21 April 2009 - 01:57 PM

Hey all,

I'm stuck. I've got the code below. Now I need to add mouseListeners on the textFields that are added to the Panels. Problem is: I cannot add mouselisteners in the methods of the buttons (that would be perfect) but I have to do it in the try8() class. I create an array of textfields which I can add to every panel, but it is the same array. I don't know how to make several and then add them automatically (I hope you can follow me).
Anyway, I need the array. So I cannot add mouseListener to an array that hasn't been filled yet. However if I fill the array already then I can only use all the textfields in there once. So I cannot fill them already. I cannot add the textfields to the panels yet either because it should depend on the user how many textfields should be added on the panels.

Ok the question is: how do add a mouselistener to the textfields after they are created (without changing things in the program given below for certain reasons).

Hope someone can help me out!! You can ask for more info because I understand it's not so clear, but it's hard to explain!

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;

public class Try8 extends JFrame implements MouseListener

{
  public JTextField h = new JTextField (10);
  static Graphics g;

  public int i=0;
  public int k=10;
  public int b=20;
  public int m=-1;
  public int c=1;
  public int d=0;

  public int x;
  public int y;

 // public component1= new Component();
 // public component2= new Component();
  
 // public int t=-1;

  public JPanel [] panel= new JPanel[31];
  public JTextField [] text=new JTextField[16];

  
  
  public Try8()
 {


 JPanel p = new JPanel();
 p.setLayout(new GridLayout(6,1, 5, 5));
 
  JButton b1 = new JButton("input space");
  JButton b2 = new JButton("generic space");
  JButton b3 = new JButton("blend space");
  JButton b4 = new JButton("remove textfield");
  JButton b5 = new JButton("textfield");
  JButton b6 = new JButton("connection line");

  p.add(b1);
  p.add(b2);
  p.add(b3);
  p.add(b4);
  p.add(b5);
  p.add(b6);

  g=p.getGraphics();

  add(p, BorderLayout.WEST);

 final JPanel q = new JPanel();
 q.setLayout(new BorderLayout());
 final JPanel r = new JPanel();
 final JPanel s = new JPanel();
 final JPanel t = new JPanel();
 

 add(q, BorderLayout.CENTER);
 q.add(r, BorderLayout.CENTER);
 q.add(s, BorderLayout.EAST);
 q.add(t, BorderLayout.SOUTH);

  
  
  h.addMouseListener(this);
  

  for (i=0; i<=30; i++)
  {
  panel[i]=new JPanel();
  panel[i].addMouseListener(this);
  panel[i].setName("" + i);
  }



  
  i=0;


  b1.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	d=d+1;
	i=i+1;
	text[1]=new JTextField(10);

	
	
	r.add(panel[i]); 
	panel[i].setLayout(new GridLayout(1,1,6,5));
	
	panel[i].setBorder(new TitledBorder("input " + i));
   
	panel[i].add(text[1]);
	panel[i].add(h);
  
   }
  });

 
 b2.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	d=d+1;
	k=k+1;
	text[1]=new JTextField(10);
	
	s.add(panel[k]); 
	panel[k].setLayout(new GridLayout(1,1,6,5));
	
	panel[k].setBorder(new TitledBorder("generic " + (k-10)));
   
	panel[k].add(text[1]);
  
	
   }
  });
 

b3.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	d=d+1;
	
	b=b+1;
	text[1]=new JTextField(10);
	
	t.add(panel[b]); 
	panel[b].setLayout(new GridLayout(1,1,6,5));
	
	panel[b].setBorder(new TitledBorder("blend "));
   
	panel[b].add(text[1]);
  
	
   }
  });
b4.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	 
	 if (m==-1) {}
	 else
	 {
		 
	c = panel[m].getComponentCount();
	 
	if (c ==1) {}
	else
	{
	 
	  
	 Component component = panel[m].getComponent(c-1);
	 
	panel[m].remove(component);
	c = panel[m].getComponentCount();
	 
	panel[m].setLayout(new GridLayout(c,1,6,5));
	 
	validate();
	 
	}
	 }
   }
  }); 

b5.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	 

	 if (m==-1) {}
	 else
	 {
			
	  if (c==14) {}
	  else
	  {
	 c = panel[m].getComponentCount();
 
	 
	 text[c+1]=new JTextField(10);
	 
	 
	 panel[m].add(text[c+1]);

	 c = panel[m].getComponentCount();
	 
	 
	 panel[m].setLayout(new GridLayout(c,1,6,5));

	 validate();
	  }
	 }	 
	}
   });



b6.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
	
	if (m==-1) {}
	 else
	 {		
/*
	  int x1 = x;
	  int y1 = y;

	  int y2 = y;
	  int x2 = x;

	  g.drawLine(x1, y1, x2, y2);

	  
   
 c = panel[m].getComponentCount();
 Woop(c);
 
 System.out.println(Woop(c));


	  
	Component [] component1 = panel[m].getComponents();


  //  Component component2 = panel[m].getComponent();
	 
  // System.out.println(component1.getLocation());
  // System.out.println(component2.getLocation());
	 
	validate();
	 
 */   
	 }
	
   }
  });

}



  public void mousePressed(MouseEvent e) 
  {}
  public void mouseReleased(MouseEvent e) 
  {}
  public void mouseEntered(MouseEvent e) 
  {}
  public void mouseExited(MouseEvent e) 
  {}
  public void mouseClicked(MouseEvent e) 
  {
	 

   String name = e.getComponent().getName();
   m = Integer.parseInt(name);

   x = e.getX();
   y = e.getY();

System.out.println(name);
System.out.println("x is " + e.getX());
System.out.println("y is " + e.getY());

   
   }

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

	frame.setTitle("let's try");
	frame.setSize (400, 400);
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible (true);

	 
   }


 
}







Is This A Good Question/Topic? 0
  • +

Replies To: 2 mouseListeners

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: 2 mouseListeners

Posted 21 April 2009 - 04:26 PM

View Postahoest, on 21 Apr, 2009 - 12:57 PM, said:

Now I need to add mouseListeners on the textFields that are added to the Panels. Problem is: I cannot add mouselisteners in the methods of the buttons (that would be perfect) but I have to do it in the try8() class. I create an array of textfields which I can add to every panel, but it is the same array. I don't know how to make several and then add them automatically (I hope you can follow me).

Not really :D
There is no rule saying that the MouseListener should be in the same class that the JComponent they watch.
But anyhow your Buttons and your JTextFieds are in the same class try8 so what is the problem ?
P.S.
before creating a ActionListener for every button never though of building only one that will receive it its constructor the button number ?

class MyActionListener implements ActionListener {
	 int buttonNumber;

	 MyActionListener(int n) {
		 buttonNumber = n;
	 }

	 public void actionPerformed(ActionEvent e) {
		switch(buttonNumber) {
			  case 1:
				  button1 was pressed
				  break;
			  case 2:
....


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1