How can i make perticular row or perticular cell of a JTable as Editable dynamically

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 3
Reputation: sandip.bhoi is an unknown quantity at this point 
Solved Threads: 0
sandip.bhoi sandip.bhoi is offline Offline
Newbie Poster

How can i make perticular row or perticular cell of a JTable as Editable dynamically

 
0
  #1
Jan 16th, 2007
Dear all,

i am having a JTable, in which a column of each row is having a checkbox field.


If the checkbox is checked then and then i should be able to modify the cells in that row, ...where the checkbox is.

I have created the table with AbstractTableModel of which the isCellEditable(int row, int col) method is overridden.

Whatever return value (true/false) gives, it reflects the perticular
cells becomes editable/non-editable respectively.


but at run time...(mean the table is created now) and now i want to make the cells editable/non-editable depending on the checkbox value...


how can i implement it.........
please suggest.........

thank you.........


following is the sample code from some tutorial...

  1.  
  2.  
  3. /*
  4.  * TableRenderDemo.java requires no other files.
  5.  */
  6.  
  7. import java.awt.Component;
  8. import java.awt.Dimension;
  9. import java.awt.GridLayout;
  10.  
  11. import javax.swing.DefaultCellEditor;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17. import javax.swing.table.AbstractTableModel;
  18. import javax.swing.table.DefaultTableCellRenderer;
  19. import javax.swing.table.TableCellRenderer;
  20. import javax.swing.table.TableColumn;
  21.  
  22. /**
  23.  * TableRenderDemo is just like TableDemo, except that it explicitly initializes
  24.  * column sizes and it uses a combo box as an editor for the Sport column.
  25.  */
  26. @SuppressWarnings("serial")
  27. public class TableRenderDemo extends JPanel {
  28. private boolean DEBUG = true;
  29.  
  30. public TableRenderDemo() {
  31. super(new GridLayout(1, 0));
  32.  
  33. JTable table = new JTable(new MyTableModel());
  34.  
  35. // table.setEditingColumn(0);
  36. // table.editCellAt(0, 0);
  37.  
  38. table.setPreferredScrollableViewportSize(new Dimension(500, 100));
  39.  
  40. // Create the scroll pane and add the table to it.
  41. JScrollPane scrollPane = new JScrollPane(table);
  42.  
  43. // Set up column sizes.
  44. initColumnSizes(table);
  45.  
  46. // Fiddle with the Sport column's cell editors/renderers.
  47. setUpSportColumn(table, table.getColumnModel().getColumn(2));
  48.  
  49. // Add the scroll pane to this panel.
  50. add(scrollPane);
  51. }
  52.  
  53. /*
  54.   * This method picks good column sizes. If all column heads are wider than
  55.   * the column's cells' contents, then you can just use
  56.   * column.sizeWidthToFit().
  57.   */
  58. private void initColumnSizes(JTable table) {
  59. MyTableModel model = (MyTableModel) table.getModel();
  60. TableColumn column = null;
  61. Component comp = null;
  62. int headerWidth = 0;
  63. int cellWidth = 0;
  64. Object[] longValues = model.longValues;
  65. TableCellRenderer headerRenderer = table.getTableHeader()
  66. .getDefaultRenderer();
  67.  
  68. for (int i = 0; i < 5; i++) {
  69. column = table.getColumnModel().getColumn(i);
  70.  
  71. comp = headerRenderer.getTableCellRendererComponent(null, column
  72. .getHeaderValue(), false, false, 0, 0);
  73. headerWidth = comp.getPreferredSize().width;
  74.  
  75. comp = table.getDefaultRenderer(model.getColumnClass(i))
  76. .getTableCellRendererComponent(table, longValues[i], false,
  77. false, 0, i);
  78.  
  79. cellWidth = comp.getPreferredSize().width;
  80.  
  81. if (DEBUG) {
  82. System.out.println("Initializing width of column " + i + ". "
  83. + "headerWidth = " + headerWidth + "; cellWidth = "
  84. + cellWidth);
  85. }
  86.  
  87. // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
  88. column.setPreferredWidth(Math.max(headerWidth, cellWidth));
  89. }
  90. }
  91.  
  92. public void setUpSportColumn(JTable table, TableColumn sportColumn) {
  93. // Set up the editor for the sport cells.
  94. JComboBox comboBox = new JComboBox();
  95. comboBox.addItem("Snowboarding");
  96. comboBox.addItem("Rowing");
  97. comboBox.addItem("Knitting");
  98. comboBox.addItem("Speed reading");
  99. comboBox.addItem("Pool");
  100. comboBox.addItem("None of the above");
  101. sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
  102.  
  103. // Set up tool tips for the sport cells.
  104. DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
  105. renderer.setToolTipText("Click for combo box");
  106. sportColumn.setCellRenderer(renderer);
  107. }
  108.  
  109. class MyTableModel extends AbstractTableModel {
  110. private String[] columnNames = { "First Name", "Last Name", "Sport",
  111. "# of Years", "Vegetarian" };
  112.  
  113. private Object[][] data = {
  114. { "Mary", "Campione", "Snowboarding", new Integer(5),
  115. new Boolean(false) },
  116. { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
  117. { "Kathy", "Walrath", "Knitting", new Integer(2),
  118. new Boolean(false) },
  119. { "Sharon", "Zakhour", "Speed reading", new Integer(20),
  120. new Boolean(true) },
  121. { "Philip", "Milne", "Pool", new Integer(10),
  122. new Boolean(false) } };
  123.  
  124. public final Object[] longValues = { "Sharon", "Campione",
  125. "None of the above", new Integer(20), Boolean.TRUE };
  126.  
  127. public int getColumnCount() {
  128. return columnNames.length;
  129. }
  130.  
  131. public int getRowCount() {
  132. return data.length;
  133. }
  134.  
  135. public String getColumnName(int col) {
  136. return columnNames[col];
  137. }
  138.  
  139. public Object getValueAt(int row, int col) {
  140. return data[row][col];
  141. }
  142.  
  143. /*
  144.   * JTable uses this method to determine the default renderer/ editor for
  145.   * each cell. If we didn't implement this method, then the last column
  146.   * would contain text ("true"/"false"), rather than a check box.
  147.   */
  148. public Class<?> getColumnClass(int c) {
  149. return getValueAt(0, c).getClass();
  150. }
  151.  
  152. /*
  153.   * Don't need to implement this method unless your table's editable.
  154.   */
  155. public boolean isCellEditable(int row, int col) {
  156. // Note that the data/cell address is constant,
  157. // no matter where the cell appears onscreen.
  158. // return false;
  159. return true;
  160.  
  161. }
  162.  
  163. /*
  164.   * Don't need to implement this method unless your table's data can
  165.   * change.
  166.   */
  167. public void setValueAt(Object value, int row, int col) {
  168. if (DEBUG) {
  169. System.out.println("Setting value at " + row + "," + col
  170. + " to " + value + " (an instance of "
  171. + value.getClass() + ")");
  172. }
  173.  
  174. data[row][col] = value;
  175. fireTableCellUpdated(row, col);
  176.  
  177. if (DEBUG) {
  178. System.out.println("New value of data:");
  179. printDebugData();
  180. }
  181. }
  182.  
  183. private void printDebugData() {
  184. int numRows = getRowCount();
  185. int numCols = getColumnCount();
  186.  
  187. for (int i = 0; i < numRows; i++) {
  188. System.out.print(" row " + i + ":");
  189. for (int j = 0; j < numCols; j++) {
  190. System.out.print(" " + data[i][j]);
  191. }
  192. System.out.println();
  193. }
  194. System.out.println("--------------------------");
  195. }
  196. }
  197.  
  198. /**
  199.   * Create the GUI and show it. For thread safety, this method should be
  200.   * invoked from the event-dispatching thread.
  201.   */
  202. private static void createAndShowGUI() {
  203. // Create and set up the window.
  204. JFrame frame = new JFrame("TableRenderDemo");
  205. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  206.  
  207. // Create and set up the content pane.
  208. TableRenderDemo newContentPane = new TableRenderDemo();
  209. newContentPane.setOpaque(true); // content panes must be opaque
  210. frame.setContentPane(newContentPane);
  211.  
  212. // Display the window.
  213. frame.pack();
  214. frame.setVisible(true);
  215. }
  216.  
  217. public static void main(String[] args) {
  218. // Schedule a job for the event-dispatching thread:
  219. // creating and showing this application's GUI.
  220. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  221. public void run() {
  222. createAndShowGUI();
  223. }
  224. });
  225. }
  226. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,275
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How can i make perticular row or perticular cell of a JTable as Editable dynamically

 
0
  #2
Jan 16th, 2007
Use actionListener on checkbox to see if any changes happends, if yes than change status from non-editable to editable and oposite
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: sandip.bhoi is an unknown quantity at this point 
Solved Threads: 0
sandip.bhoi sandip.bhoi is offline Offline
Newbie Poster

Re: How can i make perticular row or perticular cell of a JTable as Editable dynamica

 
0
  #3
Jan 16th, 2007
thank you for reply.........

means r u saying that..... i should change some property of table on the event of any action performed......??

that is okay.... on that event what property should i change....???


thanks for spending your precious time....

plz reply...









Originally Posted by peter_budo View Post
Use actionListener on checkbox to see if any changes happends, if yes than change status from non-editable to editable and oposite
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 6985 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC