To keep track of the panel that has been clicked by the mouse

This is my object code::

package build;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Table
{
    private ArrayList<Column> columns;
    private String NAME;
    private static int count=0;
    public Table() 
    {
        setColumns(new ArrayList<Column>());
        NAME=new String();

        this.NAME="Table"+count;
        count++;
    }

    public void AddColumn(Column column)
    {
        /*for(Column c: getColumns())
        {
           if(c.getname().equals(column.getname())){return;}
        }
        */
        getColumns().add(column);

    }
public int getCount(){return count;}
    public void SaveToFile() throws IOException
    {


        FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt", true);
        BufferedWriter bw = new BufferedWriter(fw);
         bw.write("\r\n");
        bw.write(this.NAME);
         bw.write("\r\n");

for(Column c: getColumns())
{
     bw.write("\r\n");
            bw.write(c.getname()+"  ");

            bw.write(c.getdatatype()+"  ");


     if(c.getPK()== true)
     {
            bw.write("true"+"  ");

     }    
        else
        {   bw.write("false"+"  ");

        }
    if(c.getNN()== true)
    {
        bw.write("true"+"   ");

    }     
    else
        {bw.write("false"+"  "); } 



}
bw.close();
    }

    public boolean DeleteColumn(String name)
    {
        for (Column c : getColumns())
        {
            if (c.getname().equals(name)) {return getColumns().remove(c);}
        }
        return false;
    }

    public void Delete()
    {

        for (Column c : getColumns())
        {
            c.setname(null);
            c.setdatatype(null);
            c.setNN(false);
            c.setNN(false);

        }

    }

    public void viewColumns()
    {
          System.out.append(NAME+"  ");

        for(Column c: getColumns())
      {
          System.out.append("\n");
          System.out.append(c.getname()+"  ");

          System.out.append( c.getdatatype()+"  ");

    if(c.getNN()== true)
    {
        System.out.print("true");
    }     
    else
        System.out.print("false");  
      }
    }

    public String getNAME() {

        return NAME;
    }

    public ArrayList<Column> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<Column> columns) {
        this.columns = columns;
    }

    public void setNAME(String text) {
        NAME=text;

    }

    public Column findPK()
    {

         for (Column c : getColumns())
            {
                if (c.getPK()==true) {return c;}
            }
        return null;

    }

    public void equal(Table temp) 
    {
        this.columns=temp.columns;
    }



}

I have added this object i.e. Table via panel to my Jframe :: as shown below

          final Table T=new Table();
          JPanel panel = new JPanel();
          panel.setBounds(197, 5+T.getCount(), 114+T.getCount(), 104+T.getCount());

          layeredPane.add(panel);

          final JLabel lblNewLabel = new JLabel(T.getNAME());
          panel.add(lblNewLabel);
          handleDrag(panel);
          table = new JTable();
          table.setShowVerticalLines(false);
          panel.add(table);
          table.setModel(new DefaultTableModel(
            new Object[][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
            },
            new String[] {
                "Name", "type"
            }
          ) {
            Class[] columnTypes = new Class[] {
                String.class, Object.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
            boolean[] columnEditables = new boolean[] {
                false, true
            };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }

i want to add a mouse listener that whenever this table(object) is clicked name of that table shall perform " Console.writeline(Table.getNAME()); " :: i want to print object's name on the console when ever the object is clicked.

I have allocated names to these components i.e. panels by panel.setName(text);

 String name=new String();

                   mntmOneToOne.addMouseListener(new MouseAdapter()
                   {
                        @Override
                        public void mouseClicked(MouseEvent arg0)
                        {
                            int count = arg0.getClickCount();
                            if (count == 1)
                            {
           // below Line is showing error as it doesnot know which panel is it about                        

                             name = panel.getName(); 
                            }

                        }
                   });

Answers


You can use Component.setName() to give names to Swing and AWT components.

And then you can use Component.getName() to print the name when the specific component is clicked.


Need Your Help

Implementing android music search

android sqlite search media-player android-sqlite

I am implementing a music player for android and want to implement the search functionality.

Dynamic menu with symfony layout

php symfony1 symfony-1.4

I started using symfony a few weeks ago, and there are some things that are not clear for me.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.