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):
-------------------------- ---------- -----
setPreferredScrollableView portSize(n ew Dimension(750, 550));
setSelectionMode(ListSelec tionModel. MULTIPLE_I NTERVAL_SE LECTION);
setRowSelectionAllowed(tru e);
dtm = new DefaultTableModel(columnNa mes, 0);
TableSorter sorter = new TableSorter(dtm);
sorter.addMouseListenerToH eaderInTab le(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 :)
Below is the code I am currently using (again, this works fine, but double clicking doesn't work too well):
--------------------------
setPreferredScrollableView
setSelectionMode(ListSelec
setRowSelectionAllowed(tru
dtm = new DefaultTableModel(columnNa
TableSorter sorter = new TableSorter(dtm);
sorter.addMouseListenerToH
//addMouseListener(this);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println("double
}
}
} );
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Thanks,
Chris
ASKER
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
Thanks,
Chris
ASKER
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
Thanks,
Chris
ASKER
oops..sorry about the multiple posts!
ASKER
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(tabl e.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
public void addData(Vector v) {
table.add(v.toArray());
fireTableRowsInserted(tabl
}
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
ASKER
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
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.
ASKER
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("FirstNam e", 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("WorkOrde r", 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
--------------------------
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
}
}
});
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("FirstNam
new ColumnDescriptor("LastName
new ColumnDescriptor("Phone", 50, JLabel.LEFT),
new ColumnDescriptor("City", 50, JLabel.LEFT),
new ColumnDescriptor("State", 50, JLabel.LEFT),
new ColumnDescriptor("Street",
new ColumnDescriptor("Notes", 50, JLabel.LEFT),
new ColumnDescriptor("WorkOrde
};
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
ASKER
Sorry, the line:
dtm = new RecordsViewModel();
is:
rvm = new RecordsViewModel();
Just letting you know that that isn't hte problem :)
Thanks,
Chris
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.
ASKER
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
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 ...
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 ...
ASKER
Gotcha.. thank you very much and sorry I didn't have any more points to give you!
no problem, sorry for late feedback :-)
ASKER
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