?
Solved

How to make one entire column unselectable(JTable) in java? I am using Netbeans, JDBC Driver and MySQL.

Posted on 2011-10-27
6
Medium Priority
?
953 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:octoberqueen
6 Comments
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 37050375
         Hi!

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
0
 

Author Comment

by:octoberqueen
ID: 37053202
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.





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);

Open in new window

mayajaal-1.png
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1500 total points
ID: 37056592
Replace this code:

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);

Open in new window


with this code:

int row = movTable.getSelectedRow();
Object a = movTable.getValueAt(row, 0);
Object b = movTable.getValueAt(row, 1);
Object c = movTable.getValueAt(row, 2);
Object d = movTable.getValueAt(row, 3);

Open in new window


and you won't have any problems, no matter what column is selected.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:niketpipaliya
ID: 37101155

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.setColumnIdentifiers(colName);
    jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    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(con);
    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);
0
 

Author Closing Comment

by:octoberqueen
ID: 37123249
Not what I asked but really helped me.
Thank you Friend
0
 
LVL 37

Expert Comment

by:zzynx
ID: 37125595
Thanx 4 axxepting.
(Just wondering what answer would have deserved an A grade...)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question