Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
iscelleditable is not working in jtable
vijayakumar durai
Ranch Hand
Posts: 153
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i am trying the jtable cell uneditable but it is not working can any body tell what is the problem
View Employee1.java
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.*; import javax.swing.table.DefaultTableModel; public class ViewEmployee1 extends JPanel { private boolean DEBUG = false; public ViewEmployee1() { super(new GridLayout(1,0)); ResultSet rs=null; try { Statement stmt1=getStatement(); rs=stmt1.executeQuery("select * from addemp"); } catch(Exception e) { e.printStackTrace(); } DButils db=new DButils(); JTable table = new JTable(db.resultSetToTableModel(rs)); //calling the dbutils class method table.setModel(new MyDefaultTableModel()); //This is not working calling the MyDefaultTableMOdel //table.setModel(new MyTableModel()); table.setPreferredScrollableViewportSize(new Dimension(550, 70)); JScrollPane scrollPane = new JScrollPane(table); //table.setFillsViewportHeight(true); if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { //printDebugData(table); } }); } //Create the scroll pane and add the table to it. //JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); } public Statement getStatement() { Connection con=null; Statement stmt=null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=d:/Project1.mdb;"); stmt=con.createStatement(); } catch(Exception e) { e.printStackTrace(); } return stmt; } /* private void printDebugData(JTable table) { int numRows = table.getRowCount(); int numCols = table.getColumnCount(); javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: "); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + model.getValueAt(i, j)); } System.out.println(); } System.out.println("--------------------------"); }*/ /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. ViewEmployee1 newContentPane = new ViewEmployee1(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
DButils.java
import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.Vector; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; public class DButils { public static TableModel resultSetToTableModel(ResultSet rs) { try { ResultSetMetaData metaData = rs.getMetaData(); int numberOfColumns = metaData.getColumnCount(); Vector columnNames = new Vector(); // Get the column names /*for (int column = 0; column < numberOfColumns; column++) { columnNames.addElement(metaData.getColumnLabel(column + 1)); }*/ // Get all rows. Vector v1=new Vector(); v1.addElement("Employee Id"); v1.addElement("Name"); v1.addElement("Address"); v1.addElement("Phone No"); v1.addElement("Date Of Birth "); v1.addElement("Date Of Joining"); v1.addElement("Designation"); v1.addElement("GrossSalary"); Vector rows = new Vector(); DefaultTableModel d=new DefaultTableModel(); while (rs.next()) { Vector newRow = new Vector(); for (int i = 1; i <= numberOfColumns; i++) { newRow.addElement(rs.getObject(i)); //d.isCellEditable(i,i); } rows.addElement(newRow); } //model return new DefaultTableModel(rows, v1); } catch (Exception e) { e.printStackTrace(); return null; } } }
MyDefaultTableModel.java
import javax.swing.table.*; public class MyDefaultTableModel extends DefaultTableModel { public MyDefaultTableModel() { super(); } public boolean isCellEditable(int row, int col) { // iscelleditable function return false; } }
Rob Camick
Rancher
Posts: 3324
32
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
JTable table = new JTable(db.resultSetToTableModel(rs)); table.setModel(new MyDefaultTableModel
Doesn't seem to make much sense to create a table with data from the SQL query, and then change the model to an empty model.
Did you add any debug statement to your custom model to see if the isCellEditable(...) method is ever invoked?
If you need further help then you need to create a
SSCCE (Short, Self Contained, Compilable and Executable, Example Program)
, that demonstrates the incorrect behaviour.
I brought this back from the farm where they grow the tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
class cast exception
JRadioButton in JTable
Cannot uncheck JCheckBox in JTable
JTable
More...