JTable - Preventing user input.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: JTable - Preventing user input.

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Posts
    17

    Question JTable - Preventing Editing

    I am using a JTable. I want to have the rows selectable, however I dont want the user to be able to edit or input data directly via the table.

    I can use: jTable1.setCellSelectionEnabled(false);

    However this stops the rows being highligted when clicked on how do I go about this??

    Thanks for Reading


    Scottie!!
    Last edited by scottie_uk; 12-07-2005 at 11:29 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    Guadalajara, Jal. Mexico
    Posts
    5
    A way to achieve that, itís creating a TableModel class which extends from a AbstractTableModel. Here is an example:

    Code:
    import javax.swing.table.AbstractTableModel;
    
    public class MyTableModel extends AbstractTableModel{
    	String[] columnNames = {"First Name",
    							"Last Name",
    							"Sport",
    							"# of Years"};
    	
    	Object[][] data = {
    			{"Mary", "Campione",
    				"Snowboarding", new Integer(5)},
    				{"Alison", "Huml",
    					"Rowing", new Integer(3)},
    					{"Kathy", "Walrath",
    						"Knitting", new Integer(2)}};
    	
    	public MyTableModel(){
    		
    	}
        public int getColumnCount() {
            return columnNames.length;
        }
    
        public int getRowCount() {
            return data.length;
        }
    
        public String getColumnName(int col) {
            return columnNames[col];
        }
    
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
    	public boolean isCellEditable(int row, int col) {
    		return false;
    	}
    }
    If you want, you can test it, using the following code:

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class JTableSample extends JFrame{
    	
    	public JTableSample() {
    		JTable table = new JTable(new MyTableModel());
    		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    		
    		//Create the scroll pane and add the table to it.
    		JScrollPane scrollPane = new JScrollPane(table);
    		
    		//Add the scroll pane to this panel.
    		this.getContentPane().add(scrollPane);
    		
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setSize(500,300);
    	}
    	
    	public static void main(String[] args) {
    		JTableSample t = new JTableSample();
    		t.setVisible(true);
    	}
    	
    }

    For more information, I recommend you to take a look in the following links.

    http://java.sun.com/docs/books/tutor...able.html#data
    http://java.sun.com/j2se/1.4.2/docs/...ableModel.html


    Regards!!

  3. #3
    Join Date
    Feb 2003
    Posts
    17
    Thanks a lovely example.

    Works fine now.

    Now all I need to do is prevent the colums from being moved via drag n drop.

  4. #4
    Join Date
    Oct 2005
    Location
    Guadalajara, Jal. Mexico
    Posts
    5
    Add the following line to your code:

    table.getTableHeader().setReorderingAllowed(false);

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class JTableSample extends JFrame{
    	
    	public JTableSample() {
    		JTable table = new JTable(new MyTableModel());
    		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    		table.getTableHeader().setReorderingAllowed(false);
    		
    		//Create the scroll pane and add the table to it.
    		JScrollPane scrollPane = new JScrollPane(table);
    		
    		//Add the scroll pane to this panel.
    		this.getContentPane().add(scrollPane);
    		
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setSize(500,300);
    	}
    	
    	public static void main(String[] args) {
    		JTableSample t = new JTableSample();
    		t.setVisible(true);
    	}
    	
    }

Similar Threads

  1. Replies: 0
    Last Post: 02-05-2002, 03:11 PM
  2. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  3. Check if an application is waiting for user input
    By Gerardo Gomez in forum VB Classic
    Replies: 6
    Last Post: 06-26-2000, 11:34 AM
  4. Creating a user input search
    By oSSiE in forum ASP.NET
    Replies: 0
    Last Post: 05-01-2000, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center