• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable configuration

 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not extend JTable
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic