Disable first Column in jTable

Hello people my problem is I use jTable and in the table I need to disable first column so that the user is not able to change edit the values that are listed in this column. Please help me with some code samples if possible. Thanks in advance.

A custom table model makes this simple. You just override the isCellEditable method and return false when the column index is 0.

Similar Messages

  • How to disable moving Columns in JTable

    Hi,
    How can I make sure that the columns in my JTable cannot be moved?
    Any ideas?
    thanks

    How about:myTable.getTableHeader().setReorderingAllowed(false);

  • (JTable) How can i get the first columns cell in a selected row?

    Hello. I am trying to figure out how i can get the first columns cell within a selected row no matter what cell is selected in that row. I have a class that extends AbstractTableModel which represents the table. Now i have another class that extends DefaultSelectionModel. Each model is added to the JTable via setModel(TableModel dataModel), setSelectionModel(ListSelectionModel newModel). Now i don't understand what i have to return from getMaxSelectionIndex(). Any ides? Thanks.
      class xTableModel extends AbstractTableModel{
         private static String[] cols;
         private Object[][] data;
            public void setTableModel(Object data[][], String[] cols){
             this.cols = cols;
             this.data = data;
            public String getColumnName(int col){
             return cols[col];
            public int getRowCount() {
             return data.length;
            public Object getValueAt(int row, int col) {
             return data[row][col];           
            public void setValueAt(Object value,int row,int col){
             data[row][col]=value;
            public int getColumnCount(){
             return cols.length;
    class ColumnListenerModel extends DefaultListSelectionModel{
         public int getMaxSelectionIndex(){}

    int row = table.getSelectedRow();
    Object data = table.getValueAt(row, 0);

  • Disabling validations if the first column is empty in an editable table

    Hi ,
    I'd want to disable validations if the first column in an editable table is empty - coz we'd assume that the user does not want to enter any data in the new row. How can this be done ?

    Hi Viren,
    It is correct that the first line for every Master Data is an empty row, if I am not wrong it derives from SEM settings.
    1.If you have some rows with 0CUST_SALES without any attribute it is because you are uploading this MAster Data with a source that does not consider compound objects: it is surely a Transactional DataSource (you do not upload Attributes). So for every Transactional record for which you do not upload also compound objects the system creates a record in your Master Data that is not correct.
    2. If you do not have Distr Channel update in Inventory Cube I think you must re-upload all your request starting from PSA. If you have deleted your PSA you can create a DataSource on your cube and feed once again your cube with its same data (built Update Rules that start from Cube and end in the same Cube). Make an upload of all your request and the delete all previous request.
    Ciao.
    Riccardo.

  • Making JTable columns 2- n scrollable, not the first column, how?

    Id like for my JTable's first column to always be visible, and columns 2->n to be scrollable. The table also follows the sortable table model found from the swing examples...so if the user sorts any of the columns, all the other columns should sort accordingly.
    is there anyway i can separate the first column with the others, so that i can put them in their own scrollpanes (first column just scrolls up/down, and 2->n can scroll either way)?

    I only glanced at the example at the posted link... if memory servers, the trick was to create one JTable but with two different TableModels. To link the two models, you can simply make the getValueAt method in the fixed model call the getValueAt method of the non-fixed model. It's a bit tricky for beginner, but doable.
    ;o)
    V.V.

  • JTable - make first column not scrollable

    I like to know using one JTable and JScrollPane to make it generically not scroll for the first column.
    Do you set the viewport to start at the second column, if so how?
    Thanks
    Abraham

    Heres a test to run with the renderers and editors for fixed table columns
    See source for FixedColumnScrollPane
    // import libraries required, no space
    public class SimpleTable extends JFrame {
       private static class MyTableCellRenderer extends DefaultTableCellRenderer {
          public MyTableCellRenderer() {
             super();
             setOpaque(true);
             noFocusBorder = new EmptyBorder(1, 2, 1, 2);
             setBorder(noFocusBorder);
          public Component getTableCellRendererComponent(JTable table, Object value,
                                                         boolean isSelected, boolean hasFocus,
                                                         int row, int col) {
             JLabel renderer = (JLabel) super.getTableCellRendererComponent(table, value,
                   isSelected, hasFocus, row, col);
             String cellValue = value.toString();
             renderer.setHorizontalAlignment(SwingConstants.LEFT);
             renderer.setToolTipText(cellValue);
             String headerValue = table.getColumnModel().getColumn(col).getHeaderValue().toString();
             // Use header value for renderers when splitting table columns, dont check by column index
             if (headerValue.length() == 0) {
                renderer.setText("Row " + (row+1));
             if (cellValue.startsWith("a")) {
                renderer.setForeground(Color.blue);
             } else if (cellValue.startsWith("b")) {
                renderer.setForeground(Color.green);
             } else if (cellValue.startsWith("c")) {
                renderer.setForeground(Color.blue);
             } else if (cellValue.startsWith("d")) {
                renderer.setForeground(Color.magenta);
             } else if (cellValue.startsWith("e")) {
                renderer.setForeground(Color.ORANGE);
             return this;
       public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
          private JComponent component = null;
          public Component getTableCellEditorComponent(JTable table, Object value,
                                                       boolean isSelected,
                                                       int row, int col) {
             this.component = new JTextField(value.toString());
             return this.component;
          public Object getCellEditorValue() {
             if (component instanceof JTextComponent) {
                String text = ((JTextComponent) component).getText();
                return text;
             return null;
       public SimpleTable() {
          super("Simple JTable Test");
          setSize(300, 200);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          TableModel mainTableModel = new AbstractTableModel() {
             private static final int ROWS = 1000;
             String colValues[] = {"", "a", "b", "c", "d", "e"};
             String data[][] = new String[ROWS][colValues.length];
             String headers[] = {"", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5"};
             public int getColumnCount() {
                return colValues.length;
             public int getRowCount() {
                return ROWS;
             public String getColumnName(int col) {
                return headers[col];
             // Synthesize some entries using the data values & the row #
             public Object getValueAt(int row, int col) {
                String val = data[row][col];
                if (val == null) {
                   val = colValues[col] + row;
                   data[row][col] = val;
                return val;
             public void setValueAt(Object value, int row, int col) {
                data[row][col] = value.toString();
             public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
             public boolean isCellEditable(int row, int col) {
                return (col > 0);
          JTable mainTable = new JTable(mainTableModel);
          TableColumnModel mainColumnModel = mainTable.getColumnModel();
          for (int i = 0; i < mainColumnModel.getColumnCount(); i++) {
             TableColumn tableColumn = mainColumnModel.getColumn(i);
             tableColumn.setCellRenderer(new MyTableCellRenderer());
             tableColumn.setCellEditor(new MyTableCellEditor());
          FixedColumnScrollPane fixedColumnScrollPane
                = new FixedColumnScrollPane(mainTable, 1);
          setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
          JLabel fixedLabel = new JLabel("Fixed Column Table");
          fixedLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 11));
          getContentPane().add(fixedLabel);
          getContentPane().add(fixedColumnScrollPane);
       public static void main(String args[]) {
          SimpleTable st = new SimpleTable();
          st.setVisible(true);
    }

  • JTable - Disable some columns

    How should I disable some columns in a JTable so that they cannot be edited at all? The methods provided do this but double clicking the column enables it.

    Hi
    i dont know whether u r using TableModel or not.
    if ur using a customTableModel for ur entries ..
    u can implement the following method of
    Table Model Interface.
    public boolean isCellEditable(int rowIndex,
                                  int columnIndex)then implement in such a way that ..
    compare always with ur column number and return false.
    as follows.public boolean isCellEditable(int rowIndex,
                                  int columnIndex){
    if (columnIndex ==4 )
    return false;
    else
    return true;
    }above code makes all cells in the col=4 uneditable.
    even u rearrange the cols in table it works as per documentation.
    test this .. and let me know whether u succeeded r not.
    note: i think The implementation of this method in AbstractTableModel or defaultTableModel
    returns true always.
    thanks

  • JTable howTo: column header = first column??

    Hi,
    i have created a JTable and with the TableColumnModel i have defined column headers. I now would like to have the first column of my table looking the same way like the column headers.
    It shall look similar to what you have in Excel tables: A B C.... = Column headers and 1 2 3 ... = line numbers = first (not editable) column of the table.
    I already tried to write an own table renderer which sets the color equal to the color of the headers but it still looks not equal :-( And there is no TableRowModel, right?
    How can I achieve this appearance?
    Thanks!!!
    Robert

    Here is a simple example to get you started:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=624365

  • Problem in updating first column header in JTable

    hi
    I am using JTable in my application. I want to change first column name of JTable every time when a JList is selected. I am using DefaultTableModel for JTable. I added first column in Constructor and another in a method. I have to edit only first column header. How can i do this???
    I used this code but it is not working.
    table.getColumnModel().getColumn(0).setHeaderValue("Name of column");
    table.getTableHeader().resizeAndRepaint();
    Your help will be appreciated.
    Thanks in advance
    Sonal

    table.getColumnModel().getColumn(0).setHeaderValue("Name of column");
    table.getTableHeader().repaint();Works for me so post a SSCCE ( http://www.sscce.org/ ) that shows the problem.

  • Why is my ping so high in the first column only when i do a tracert?

    When i do a tracert my ping is really high in the first column only on each hop and then it drops to a respectable level. Does anyone know why it is like this on my 4G LTE device? On my dsl when i do the same ping test its pretty even across on each hop. This is what im seeing when i do a ping test on my 4G LTE device.
    tracert www.yahoo.com
    Tracing route to ds-any-fp3-real.wa1.b.yahoo.com [98.139.183.24]
    over a maximum of 30 hops:
      1     9 ms     3 ms     3 ms 
      2  1101 ms    34 ms    38 ms  193.sub-66-174-20.myvzw.com [66.174.20.193]
      3   169 ms    55 ms    52 ms  49.sub-69-83-13.myvzw.com [69.83.13.49]
      4    52 ms    56 ms    54 ms  194.sub-69-83-13.myvzw.com [69.83.13.194]
      5   179 ms    58 ms    57 ms  101.sub-66-174-17.myvzw.com [66.174.17.101]
      6   168 ms    57 ms    60 ms  TenGigE0-0-1-0.GW14.BOS4.ALTER.NET [152.179.2.97
      7   181 ms    58 ms    57 ms  0.ge-0-3-0.XL3.BOS4.ALTER.NET [152.63.17.130]
      8   170 ms    59 ms    58 ms  0.ae3.BR1.NYC1.ALTER.NET [152.63.18.222]
      9   177 ms    67 ms    59 ms  ae11.edge2.NewYork.Level3.net [4.68.62.41]
    10     *        *        *     Request timed out.
    11     *        *        *     Request timed out.
    12     *        *        *     Request timed out.
    13   192 ms    49 ms    60 ms  ae-4-90.edge4.NewYork1.Level3.net [4.69.155.210]
    14   185 ms    51 ms    60 ms  YAHOO-INC.edge4.NewYork1.Level3.net [4.28.130.82
    15   380 ms    73 ms   129 ms  ae-5.pat2.bfz.yahoo.com [216.115.96.67]
    16   339 ms    69 ms   146 ms  ae-4.msr2.bf1.yahoo.com [216.115.100.73]
    17   215 ms   152 ms    80 ms  xe-5-0-0.clr2-a-gdc.bf1.yahoo.com [98.139.128.11
    18   289 ms   217 ms   217 ms  et-17-1.fab6-1-gdc.bf1.yahoo.com [98.139.128.47]
    19   187 ms    87 ms    79 ms  po-14.bas2-7-prd.bf1.yahoo.com [98.139.129.227]
    20   474 ms   314 ms   294 ms  ir2.fp.vip.bf1.yahoo.com [98.139.183.24]
    Trace complete.

    hi there
    see you never got an answer from vzw
    take it you still have the high ping or no ping  in my cases
    your doing better then i
    seems the lte is getting bad with this problem over time as my connection times out causing half loaded web pages  and lots of buffering in video playback
    i have had 3 tickets in the past 3 month to try and get this fixed  but they say cant find anything wrong
    and noone else is having the problem here are a few things that seems to have helped me out
    1  increase your TTL  time till live 
    this tells your computer to wait a bit longer before giving up and requesting that a packet of info be resent
    2 in your computers network settings and reg you can change packet size to a smaller number and add a stop bit to the packet with a time stamp on each packet back in the old days  this was what v 90 modems did to get v92 speeds and did help with noisy copper wire dial up and seems to help here too
    http://www.dslreports.com/drtcp
    is a tool that will help you change your computer setting and reg
    use drtcp and the results from this page
    http://www.dslreports.com/tweaks
    xp win 7 win 8 this will help.  vista did it right and dynamically change these settings on the fly and all of step 2 is unnecessary
    remember your original settings write them down so you can go back to them should you have problems
    also make a restore point and backup your reg before tinkering with windows os
    send a private message to me if you need more help  or get a computer savvy friend to help you out if you need
    3  if your signal noise is under 4G  -100bd the lower the better,  there is a way to set your android to lte only  mode.
    more likely to lose data and voice in weak signal areas but great if tethering or hotspotting at home
    dail *#*#4636#*#* this is a back door to your android software and is open source code by android os  not vzw
    and will not unlock your phone voiding warranties. be warned though if you set things wrong in here
    you can make more problems  be very sure what you change  and do one thing at a time so you remember and can  dial in again and change it back should you disable something you need. fyi each time you dial in the number it resets to vzw default setting and may look like it did not save setting but it doz. till you dial in again.
    good luck
    ron
    ps im not a vzw rep in any way other then a fellow customer not getting answers.

  • How to set different renderers to different cells in same column of JTable?

    Hello Friends,
    I need your help again...
    Does any body knows, how to set different renderer's for different cells of same column in JTable..??
    For ex.
    Col1 Col2 Col3 Col4
    A       A       A      A
    A       A       A      B
    A       A       A      C
    A       A      A       D
    Where A B C D would be different Renderers.  I want set exactly same ,,, ie. one column with different renderer at different cell positions..
    Right now i m setting renderer using statement bellow :
    table.getColumnModel().getColumn(int).setCellRenderer(rederer_Instance);But with this, effect in the last renderer is applicable whole column....
    Can any body help me out ?????????
    please refer this thread for similar kind of discussion...
    http://forums.sun.com/thread.jspa?forumID=57&threadID=571445Thanks
    Suyog

    Please refer to the first reply of [this thread|http://forums.sun.com/thread.jspa?forumID=57&threadID=571445] for the answer. If you have a specific problem implementing it, post you code with a specific question.

  • JScrollPane scroll setting first column is not visible?

    Hi All,
    i am using jtable and jscrollpane. in this my first column of 70 pixal width and rest 125 pixal width.set focus is on 1 cell(1 row, 1 column) in table. but due to this scrollbar is slightly on right side whice coz first column not visible.(need to drag for visibility). i am trying to make it visible without dragging.
    how to do this? HELP.
    i tried with tableScroll.scrollRectToVisible(table.getCellRect(0,0,true )); & -1,-1
    but it didnt worked.
    Code is like this..........
    table = new JTable(64,65){
    table.setName("Table");
    ((DefaultTableModel)table.getModel()).setColumnIdentifiers(vectName.getColumnIdentifiers());
              for(int i = 0;i<table.getRowCount();i++) {
                   table.setValueAt((i+1)+"",i,0);
              table.setGridColor(Color.black);
              table.setSelectionBackground(new Color(125,255,144));
              table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
              table.setCellSelectionEnabled(true);          
              table.addKeyListener(listener);
              table.addMouseListener(listener);
              table.getSelectionModel().addListSelectionListener(this);
              table.getColumnModel().addColumnModelListener(this);
              table.getColumnModel().getColumn(0).setResizable(false);
              table.getColumnModel().getColumn(0).setMinWidth(70);
              table.getColumnModel().getColumn(0).setMaxWidth(70);
    JScrollPane tableScroll = new JScrollPane(table);
    Dimension minimumSize = new Dimension(100,25);
    tableScroll.setMinimumSize(minimumSize);          
    add(tableScroll);

    Hi weebib ,
    pls find some working code which shous my problame .
    you can see its coming to column 'B'. but i want it to show from column 'A' .
    sample working code : =
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.table.*;
    import java.util.*;
    public class myTable extends JFrame{
    JTable table;
    JScrollPane jsp;
    JPanel p1;
    int NO_OF_ROWS = 64;
    int NO_OF_COLUMNS = 65;
    Vector vColumnNames ;
    myTable(){
         Container con = getContentPane();
         p1 = new JPanel();
         table = new JTable(NO_OF_ROWS,NO_OF_COLUMNS){
              public boolean isCellEditable(int row, int column) {
                                  if(column == 0){
                                       return false;
                                  else{
                                       return true;
         setTableProperties();
         jsp = new JScrollPane(table);
         p1.add(jsp);
         con.add(p1);
         setSize(400,500);
         show();
         //System.out.println("Hello there");
    public Vector getColumnIdentifiers(){
         vColumnNames = new Vector();
                   for(int i=65;i<129;i++) {
                        vColumnNames.add(new String((char)i+""));// setting the alphabets for the column names //orig
         return vColumnNames ;
    public void setTableProperties(){
         ((DefaultTableModel)table.getModel()).setColumnIdentifiers(getColumnIdentifiers());
         for(int i = 0;i<table.getRowCount();i++) {
                        table.setValueAt((i+1)+"",i,0);// Set the names of the row
         table.setGridColor(Color.black);
         table.setSelectionBackground( Color.RED);
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         table.setCellSelectionEnabled(true);
         table.addMouseListener(new myMouseListener());
         table.addKeyListener(new myKeyListener());
    //     table.getColumnModel().getColumn(0).setPreferredWidth(125);
    //     table.getColumnModel().getColumn(0).setResizable(false);
    //     table.getColumnModel().getColumn(0).setMinWidth(70);
    //     table.getColumnModel().getColumn(0).setMaxWidth(70);
         for(int u =0 ;u<64;u++){
              if(u==0){
                   table.getColumnModel().getColumn(u).setPreferredWidth(70);
              else{
                   table.getColumnModel().getColumn(u).setPreferredWidth(200);
         table.changeSelection(0,1,false,false);
    static public void main(String[] args){
              myTable test = new myTable();
              test.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){System.exit(0);}
    public class myMouseListener extends MouseAdapter{
         public void mousePressed(MouseEvent me){
                   table.repaint();
    public class myKeyListener extends KeyAdapter{
         public void keyPressed(KeyEvent ke){
                   if(table.getSelectedColumn() == 0){
                        ke.consume();
    ================
    pls do reply back if i am doing something wrong.
    == Gavin

  • Disable some columns in table control?

    Hi
    How to disable some columns in table control.
    For Ex: Table contains 8 columns.
    In first row i want to disable first 4 columns.
    in second row i want to disable last 4 columns.
    In third row i want to disable first 2 and last 2 columns.  How can we achieve this?
    Cheers,
    Venkys.

    HI
    Create three attribute in the node which is bound to the table as data source.
    say pro1, pro2, and pro3 of type wdy_boolean.
    now bind the enabled property of first four column editors to pro1, last 4 to pro2, and pro3 to first 2 and last 2.
    then go to the eventhandler of the dropdown 's on select event and write the code to update the values of these attribute
    according to the selected value.
    here is the sample code.
    DATA lo_nd TYPE REF TO if_wd_context_node.
        DATA lo_el TYPE REF TO if_wd_context_element.
            DATA lv_VAL LIKE ls_dealer1-name.
        lo_nd = wd_context->get_child_node( name = 'DEALER1').
      lo_el = lo_nd->get_element(  ).
        lo_el->get_attribute(
          EXPORTING
            name =  `NAME`
          IMPORTING
            value = lv_VAL ).
         DATA PR1 TYPE WDY_BOOLEAN.
         DATA PR2 TYPE WDY_BOOLEAN.
         DATA PR3 TYPE WDY_BOOLEAN.
      PR1 = ABAP_TRUE.
    PR2 = ABAP_TRUE.
    PR3 = ABAP_TRUE.
    IF LV_VAL EQ 'A'.
    PR1 = ABAP_FALSE.
    PR2 = ABAP_TRUE.
    PR3 = ABAP_TRUE.
       ELSEIF LV_VAL EQ 'B'.
    PR1 = ABAP_TRUE.
    PR2 = ABAP_FALSE.
    PR3 = ABAP_TRUE.
    ELSEIF LV_VAL EQ 'C'.
      PR1 = ABAP_TRUE.
    PR2 = ABAP_TRUE.
    PR3 = ABAP_FALSE.
      ENDIF.
       LO_EL->SET_ATTRIBUTE(
       NAME = 'PRO1'
       VALUE = PR1
       LO_EL->SET_ATTRIBUTE(
       NAME = 'PRO2'
       VALUE = PR2
       LO_EL->SET_ATTRIBUTE(
       NAME = 'PRO3'
       VALUE = PR3
    thanks
    sarbjeet singh

  • Setting Editable columns in JTable

    I am trying to create a table where the first column is uneditable. I am guessing I want to use the isCellEditable method at some point? But this seems to be a query, rather than a setting. Can somebody explain how I go about making this setting?

    Swing related questions should be posted in the Swing forum.
    I am guessing I want to use the isCellEditable method at some point?Good guess. If you don't know how to use a method, then search the forum using the method name as a keyword to find postings that already have example of using this method. A lot faster then posting a question and hoping someone answers it.
    I prefer overriding the isCellEditable method of JTable.

  • Hidden Columns in JTable

    I wont to hide a elected column in JTable.
    how it to do

    TableColumnModel tcm = table.getColumnModel();
    TableColumn column = tcm.getColumn( modelIndex );
    tcm.removeColumn( column );
    Next time, search the forum first. There are at least a dozen posts asking this question.

Maybe you are looking for

  • Check boxes are not being displayed in any list applets

    Issue: The check boxes are not being displayed in any list applets in the system. A check box can be marked in the system, but as soon as the user clicks anywhere outside of the box the check box 'visibly' disappears. If you hover the mouse over the

  • Raid 0 Stripe 16k, on SATA 3+4 or 1+2?

    Ok, im about to reinstall everything as soon as my Videocard comes off the Fedex truck. I had my Raptor at raid 0 and at 64k stripe and HDtach was all over the place. I seen highscores with 16k stripe so going to go that route. Now i always had my Ra

  • How do I reset a scroll bar in a table?

    I have a table that fills up and the scroll bar becomes active in my application. When I start the test over I want to automatically go back to the top of the table as if I scrolled up using the scrollbar. Is there any way to do this? I have been mes

  • Buffering fullscreen is now hopeless with BT

    What is going on?????!!!!! When i try to watch a movie FULL SCREEN even after it has FULLY LOADED the sounds stays ok but the picture staggers. As if people are walking robot like. Anyone have this problem im explaining it as simple as possible. PLEA

  • R12 TOI documents for AR, AP and GL

    Hi, From where do I download R12 TOI documents for AR, AP and GL modules? Any pointers in this regard would be helpful. Thanks.