aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JTable configuration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JTable configuration" Watch "JTable configuration" New topic
Author

JTable configuration

Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Hi, guys! I am working on final touches to my project, and I ran into some JTable configuration problems. Namely, the JTable allows user to edit individual fields. I am not very familiar with JTable class, and some books I have on the subject leave the matter unclear, as to this particular problem. Maybe someone with knowledge of Swing can give me some advice on how to turn off cell editing?

Thanks in advance.


Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Anton Golovin:
Hi, guys! I am working on final touches to my project, and I ran into some JTable configuration problems. Namely, the JTable allows user to edit individual fields. I am not very familiar with JTable class, and some books I have on the subject leave the matter unclear, as to this particular problem. Maybe someone with knowledge of Swing can give me some advice on how to turn off cell editing?

Thanks in advance.


It depends on two things:

- what kind of selections do you allow, if you only allow row selections, the user can never actually select a cell

- what does the JTable.isCellEditable method return for the selected cell, you can override this.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Originally posted by peter wooster:


It depends on two things:

- what kind of selections do you allow, if you only allow row selections, the user can never actually select a cell

- what does the JTable.isCellEditable method return for the selected cell, you can override this.


Well, I got a very simple GUI. There is no custom data model, just the DafaultTableModel and a JTable, and a controller. So basically, my options are not to overwrite
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Anton Golovin:


Well, I got a very simple GUI. There is no custom data model, just the DafaultTableModel and a JTable, and a controller. So basically, my options are not to overwrite


If your View extends JTable, you can override the isCellEditable method to always return false, or to return false on the non-editable cells. Whether you extend JTable or just use one, you can set the various selection options to not allow both row and column selection. If the user can't select a single cell they can't edit it.

I allow users to select rows only, so no editing is possible directly in the table. To book or update a row I open a dialog with the row contents in it.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Peter, would you mind sharing a bit of your code? This is what I have, and it is exactly what you are saying, but it still allows editing of cells:

// record table config
dataTableView = new JTable(controller.getTableModel());

dataTableView.setAutoscrolls(true);
dataTableView.setDoubleBuffered(true);
dataTableView.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
dataTableView.setCellSelectionEnabled(false);
dataTableView.setColumnSelectionAllowed(false);
dataTableView.setRowSelectionAllowed(true);
dataTableView.setSelectionBackground(new Color(200, 200, 200));
mike acre
Ranch Hand

Joined: Sep 23, 2003
Posts: 197
Do not extend JTable


SCJP 1.4, SCJD
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Anton Golovin:
Peter, would you mind sharing a bit of your code? This is what I have, and it is exactly what you are saying, but it still allows editing of cells:

// record table config
dataTableView = new JTable(controller.getTableModel());

dataTableView.setAutoscrolls(true);
dataTableView.setDoubleBuffered(true);
dataTableView.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
dataTableView.setCellSelectionEnabled(false);
dataTableView.setColumnSelectionAllowed(false);
dataTableView.setRowSelectionAllowed(true);
dataTableView.setSelectionBackground(new Color(200, 200, 200));


Sorry, I just ran some tests, the table configuration won't fix the problem, you must override the isCellEditable method in DefaultTableModel to return false, or build your model by extending AbstractTableModel.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by mike acre:
Do not extend JTable


Probably a good idea, although it doesn't answer the question. The class you should extend is AbstractTableModel.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Thanks, Peter. Overrode isCellEditable(int row, int column) from the DefaultDataModel class. No more editing
[ September 20, 2004: Message edited by: Anton Golovin ]
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Anton Golovin:
Thanks, Peter. Overrode isCellEditable(int row, int column) from the DefaultDataModel class. No more editing

[ September 20, 2004: Message edited by: Anton Golovin ]


Your welcome, I'm glad its all working, sorry about the confusion on which class to extend.
/peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable configuration