Link to home
Start Free TrialLog in
Avatar of ctjoumas
ctjoumas

asked on

Don't want cells in a JTable to receive focus

In a class that extends JTable, I am creating a table that uses a DefaultTableModel.  I need this table to be dynamic so I can load files and add records that are stored in those files and then allow the user to add records at runtime (all of this works).  My question is...how can I get the cells to not receive focus?  What I'm trying to do is receive a double click action.  The double click works, but ONLY if you double click on the "cell separators" - because when you double click on a cell, the first click gives focus to the cell so a double click on a cell isn't possible.

Below is the code I am currently using (again, this works fine, but double clicking doesn't work too well):
-----------------------------------------
setPreferredScrollableViewportSize(new Dimension(750, 550));
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setRowSelectionAllowed(true);

dtm = new DefaultTableModel(columnNames, 0);

TableSorter sorter = new TableSorter(dtm);
sorter.addMouseListenerToHeaderInTable(this);

//addMouseListener(this);
addMouseListener(new MouseAdapter() {
     public void mouseClicked(MouseEvent e) {
          if (e.getClickCount() == 2) {
               System.out.println("double click");
          }
     }
} );

setModel(sorter);
-----------------------------------------

I found that I could use an AbstractTableModel to do what I want - but then it doesn't look like an ATM can give a dynamic table.

Thanks!

Chris

p.s. - sorry about the points - this is all i have left!  If this turns out to be kinda difficult, I will give you more points when I get some :)
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ctjoumas
ctjoumas

ASKER

Hmm... basically, the SystemViewModel that you have is what I want.  I don't really need any of the other things.  My program already saves and loads info to and from a file...  and I can add and delete records.  I'm trying to mess with the SystemViewModel and integrate it into my program....all I need is to change my DefaultTableModel to the AbstractTableModel so that I can't "select" a cell.

I will be trying to fix this - but you seem to be a lot more knowledgable about Java than me, so if you can possibly help me with just the SystemViewModel and make this a lot less confusing for me, I thank you very much :)  So, I guess i just need the SystemViewModel to add, delete, and retrieve records for me.  I think I am close - it is just adding blank records (and about 8 of them) right now!

Thanks,

Chris
One question i have is - im not so sure what the fileObjects vector is doing.  Is this vector being "put" to the table?  If so...how is it doing this?  So, if I have 7 fields (or columns) in one record and I want to add this record - how can I add this to the table?

Thanks,

Chris
One question i have is - im not so sure what the fileObjects vector is doing.  Is this vector being "put" to the table?  If so...how is it doing this?  So, if I have 7 fields (or columns) in one record and I want to add this record - how can I add this to the table?

Thanks,

Chris
One question i have is - im not so sure what the fileObjects vector is doing.  Is this vector being "put" to the table?  If so...how is it doing this?  So, if I have 7 fields (or columns) in one record and I want to add this record - how can I add this to the table?

Thanks,

Chris
oops..sorry about the multiple posts!
OK...  I have gotten it to add the correct amount of rows now... basically,  the addData function looks like:

public void addData(Vector v) {
    table.add(v.toArray());
    fireTableRowsInserted(table.size(), table.size()+1);
}

where table is my table vector - it contains all of the data that the table shows.  So, my thinking is, that the vector v that is sent to addData contains the customer record (a vector of size 7...which contains the 7 elements of each of the 7 columns I have) and this is added to my table vector...and when I fireTableRowsInserted, this vector v should now show up as a new row in the table.

However, this doesn't happen.  A new row appears, but it is empty.  I don't get any errors or anything..

Chris
For the most part - what you gave me helped...it is doing what I want it to do.  But, when I add a row, no data gets added - just a blank row is added.  Is there some way I need to "link" my data vector into my table model?  I think I'm missing something simple.

Thanks,

Chris
Sorry for absence!
try with fireTableDataChanged() instead of rowsAdded. Follow my source code and see if it works. There is a method call on every model metod which modify the internal data, called updateObjects(). Focus on that one.
I have already tried fireTableDataChanged() :)  Here is the code I am working with right now (i've left out unimportant methods and library imports...):

----------------------------
public class RecordsTable extends JTable {

  /**
   * Handle to the parent application object.
   */  
  private HelpDesk helpDeskObj;

  private RecordsViewModel rvm = null;

  public RecordsTable(HelpDesk helpDesk) {

    dtm = new RecordsViewModel();

    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
       System.out.println("double click");
     }
      }
    });

    setModel(rvm);
  }

  // this gets called from another class, which
  // passes in a vector that holds the customer data
  public void addRow(Vector record) {
    rvm.addData(record);
  }
}


class RecordsViewModel extends AbstractTableModel {

  protected ColumnDescriptor cols[] =  {
    new ColumnDescriptor("FirstName", 40, JLabel.LEFT),
    new ColumnDescriptor("LastName", 50, JLabel.LEFT),
    new ColumnDescriptor("Phone", 50, JLabel.LEFT),
    new ColumnDescriptor("City", 50, JLabel.LEFT),
    new ColumnDescriptor("State", 50, JLabel.LEFT),
    new ColumnDescriptor("Street", 50, JLabel.LEFT),
    new ColumnDescriptor("Notes", 50, JLabel.LEFT),
    new ColumnDescriptor("WorkOrder", 50, JLabel.LEFT),
  };

  private Vector table;

  public RecordsViewModel() {
    table = new Vector();
  }  

  public void addData(Vector v) {
    table.addElement(v);
    fireTableDataChanged();
  }

  class ColumnDescriptor {
    public String name;
    public int width;
    public int alignment;

    public ColumnDescriptor(String n, int w, int a) {
      name = n;
      width = w;
      alignment = a;
    }
  }
}
----------------------------


Thanks,

Chris
Sorry, the line:
dtm = new RecordsViewModel();

is:
rvm = new RecordsViewModel();

Just letting you know that that isn't hte problem :)

Thanks,

Chris
I see that uou are holding a Vector of vectors structure into your model. In my original code there was a dedicated object to actually represent a row in the table/model, called FileElement. Check your getValueAt(int row, int col) method in your model.
Ah...ok - that was my problem.  I didn't know that getValueAt gets called when displaying (or adding) a record.  I need to read up somewhere (Hopefully Java's API has explanations) on how all this works together!

Thanks!!

Chris
Yes, the methods are called

Eg: getValueAt() request the value of a particular cell
getColumnName() should return the column name stored by your collumns array
getColumnCount should return columns.length
getRowCount should return table.size()
... and so on ...
Gotcha..  thank you very much and sorry I didn't have any more points to give you!
no problem, sorry for late feedback :-)