octoberqueen
asked on
How to make one entire column unselectable(JTable) in java? I am using Netbeans, JDBC Driver and MySQL.
I am doing my java project using netbeans. I want to make entire columns except one column unselectable.The user should be only able to click rows in only in one column. How to do that?
ASKER
I am using Netbeans and I don't some of the basics Java. I just started off with and have no time to study everything from scratch. I want to complete my course project as fast as possible. It's already due now.
I am doing a simple Multiplex Ticket Booking System. I use the attached piece of code for obtaining selected row values into a Text Field. However this works without any error only if movie column is selected. I want to make sure that rows and cell values in other columns can't be selected by user and if he clicks any row with mouse, only data that should get selected should from the movie column.
I am doing a simple Multiplex Ticket Booking System. I use the attached piece of code for obtaining selected row values into a Text Field. However this works without any error only if movie column is selected. I want to make sure that rows and cell values in other columns can't be selected by user and if he clicks any row with mouse, only data that should get selected should from the movie column.
private void movTableMouseClicked(java.awt.event.MouseEvent evt) {
int row = movTable.getSelectedRow();
int column = movTable.getSelectedColumn();
Object a = movTable.getValueAt(row, column);
Object b = movTable.getValueAt(row, column+1);
Object c = movTable.getValueAt(row, column+2);
Object d = movTable.getValueAt(row, column+3);
movLab.setText(""+a);
langLab.setText(""+b);
stLab.setText(""+c);
screenLab.setText(""+d);
mayajaal-1.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is my jTable
private JTable getJTable() {
String[] colName = { "Name", "Email", "Contact No. 1", "Contact No. 2",
"Group", "" };
if (jTable == null) {
jTable = new JTable() {
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
}
DefaultTableModel contactTableModel = (DefaultTableModel) jTable
.getModel();
contactTableModel.setColum
jTable.setSelectionMode(Li
return jTable;
}
I will call this method to retrieve the data from database and put it into table model
public void setUpTableData() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
ArrayList<Contact> list = new ArrayList<Contact>();
if (!con.equals(""))
list = sql.getContactListsByGroup
else
list = sql.getContactLists();
for (int i = 0; i < list.size(); i++) {
String[] data = new String[7];
data[0] = list.get(i).getName();
data[1] = list.get(i).getEmail();
data[2] = list.get(i).getPhone1();
data[3] = list.get(i).getPhone2();
data[4] = list.get(i).getGroup();
data[5] = list.get(i).getId();
tableModel.addRow(data);
}
jTable.setModel(tableModel
}
Currently I was using this method to refresh the table after updating the table data. I will frist clear the table
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
tableModel.setRowCount(0);
ASKER
Not what I asked but really helped me.
Thank you Friend
Thank you Friend
Thanx 4 axxepting.
(Just wondering what answer would have deserved an A grade...)
(Just wondering what answer would have deserved an A grade...)
Look at the TableColumnModel and the TableColumnModelListener
http://www.java2s.com/Code/Java/Swing-JFC/Table-Column.htm
and the ListSelectionModel
and the ListCellRendererCompent
http://www.java2s.com/Code/Java/Swing-JFC/CombineTableModelandColumModel.htm
http://www.java2s.com/Code/Java/Swing-JFC/CatalogSwing-JFC.htm
Here is also an example on how to allow row,column or cell selections
http://www.roseindia.net/java/example/java/swing/EnableRowColumnAndCell.shtml
Hope this helps.
Regards,
Tomas Helgi