Hey,

Does anyone know how I can draw lines between the textfields?
If I ask for x and y then this is relative to the component itself.. Also now I have made a separate class but this means that when I draw the lines the textfields disappear they cannot exist at the same time...

Thanks in advance!

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 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 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);


  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);
  

  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)
   {

    MovablePanel2 c = new MovablePanel2();

    
    q.add(c);
    
    
   }
  });


}



  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);

     
   }

  static class MovablePanel2 extends JPanel
   {
    
    private int x1=0 ;
    private int y1=0 ;
    private int x2=0 ;
    private int y2=0 ;
    private int a=1;
    
    public MovablePanel2()
    {
     
     addMouseMotionListener(new MouseMotionAdapter()
     {
      public void mouseDragged(MouseEvent e)
      {
	
        if (a==1)
        {
	
	  x1 = e.getX();
          y1 = e.getY();
	  x2 = e.getX();
 	  y2 = e.getY();

	  a = 2;
         }
	
	  x2 = e.getX();
 	  y2 = e.getY();

	  repaint();
         
      }
     });
    }
  
    protected void paintComponent( Graphics g) 
      {
       super.paintComponent(g);
       g.drawLine(x1,y1,x2,y2);
      
      }
 
  }


 
}

!

answer here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.