Set the colum in JTable to NotEditable

How can I set the colum in JTable so it's not editable. The table model is in the same class with the JTable.

    aTable.setModel(new DefaultTableModel(columnHeader, 0) {
      public boolean isCellEditable(int row, int col) {
        return (col != yourNonEditableColumn);
    });

Similar Messages

  • Setting the foreground in JTable cells conditionally.

    I have a JTable which contains two columns adjacent to each other, both containing numbers (Floats).
    If the number in the cell at a given row of column2 is greater than the number in the corresponding cell of column1, I want to set < row, column2 >'s text/foreground to red.
    i.e.: If 0, 2 value > 0, 1 value: Set 0, 2 foreground to red
    Also, I want to do this for all the cells in the second column whose values meet the criterion, simultaneously.
    My question:
    Am I right in assuming that this is a routine task in Java, and that there are well established ways of doing this?
    If so, can anyone post or point me to some example code? If this is a wheel, I would hate to reinvent it.
    Many thanks in advance.

    karen,
    I suggest you just make the whole cell the "select" colour, especially if it's a background colour... it gives the user a better impression of what it will look like in the "whatever".... even if you think it doesn't look so good "in itself".
    The trick, is to use contrastingly dark or light forground colour.
    If you really must have small region to the left containing a text colour then just make it another column in the table.
    keith.

  • How do you set the font color for a specific entire row inside a JTable?

    How do you set the font color for a specific entire row inside a JTable?
    I want to change the font color for only a couple of rows inside a JTable.
    I've seen some ways to possibly do this with an individual cell.
    Clarification on changing the font color in an individual cell would be helpful too if
    there is no easy way to do this for a row.

    hai,
    Try out with this piece of code.Create your table and assign the renderer to each column in the table.
    CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
    for(int i=0;i<your_JTable.getColumnCount();i++)
    your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
    class CellColorRenderer extends JLabel implements TableCellRenderer
    CellColorRenderer()     
    setOpaque(true);     
    setHorizontalAlignment(LEFT);
    setVerticalAlignment(CENTER);
    setBackground(Color.white);
    setForeground(Color.black);
    protected void setValue(Object value)
         setText((value == null) ? "" : value.toString());
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
         if(isSelected == true)
              setForeground(Color.red);
         else
              setForeground(Color.black);
         setValue(value);
         return this;
    regards,
    bala

  • How to set the Selected row and Column in JTable

    Hi,
    i have a problem like the JTable having one Method getSelectedRow() and getSelectedColumn to know the Selected row and Column but if i want to explicitly set the Selected Row and Column,then there is no such type of Methods.If anybody has any solution then i will be thankful to him/her.
    Praveen K Saxena

    Is that what you're looking for? :myTable.getSelectionModel().setSelectionInterval(row, row);
    myTable.getColumnModel().getSelectionModel().setSelectionInterval(column, column);

  • How to set the row height in a JTable

    Is there a way to set the height of a row (actually all rows) in a JTable?
    I have tried setting the font to be bigger but then the cells are not big enough.

    Use "rowHeight" method of JTable class.
    For more information about JTable class, please read the API documentation at http://java.sun.com/j2se/1.3/docs/api/javax/swing/JTable.html
    /Sreenivasa Kumar Majji.
    Is there a way to set the height of a row (actually
    all rows) in a JTable?
    I have tried setting the font to be bigger but then
    the cells are not big enough.

  • JTable - programmatically setting the left column and top row

    I have a JTable in a JScrollPane. I would like to be able to programmatically scroll the table so that a certain column is shown as the left most column in the table, and/or a certain row is shown as the first visible row in the table.
    This is to allow a user to enter a value for the column or row that the want to be on the left or top of the viewport (ie, it is up to the user how they want to move the initially see the table).
    Any help would be much appreciated,
    Cheers, Sean

    Here is one way to do it:
    1) use the getCellRect(row,column,false) to get a rectangle for the cell you wish to display in the upper left corner
    2) use the getVisibleRect() method to get the viewport
    3) set the viewport x and y coordinate to be the same as the cell x,y coordinate
    4) use the scrollRectToVisible method to scroll to the desired area
    For example:
    Rectangle cellRect=myTable.getCellRect(5,10,false);
    Rectangle vis=myTable.getVisibleRect();
    vis.x=cellRect.x;
    vis.y=cellRect.y;
    myTable.scrollRectToVisible(vis);
    Caveat: posted code has not been tested!
    ;o)
    V.V.

  • How to set the border of header of JTable

    I am using the following code to set the border of my JTable headers but it doesn't seem to work!! The headers do get CENTER aligned but somehow they do not show the right border. Please Help.
    class cellAlignRenderer extends DefaultTableCellRenderer
    cellAlignRenderer()
    setHorizontalAlignment(JLabel.CENTER);
    Border b = BorderFactory.createEtchedBorder() ;
    setBorder(b);
    jTable.getColumn(0).setHeaderRenderer( new cellAlignRenderer());

    try this
    public class YourTableHeaderRenderer extends DefaultTableCellRenderer
    public YourTableHeaderRenderer()
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column)
    JLabel comp = (JLabel) super.getTableCellRendererComponent(table,value,
    isSelected,hasFocus,row,column);
    try
    comp.setBorder(BorderFactory.createBevelBorder(0,Color.lightGray,Color.darkGray));
    setFont(new java.awt.Font("Dialog", 0, 15));
    setHorizontalAlignment(SwingConstants.CENTER);
    catch(Exception err)
    err.printStackTrace();
    return this;

  • Problem in setting the background color of jtable with Nimbus

    Hi
    I have created a java swing application. In which I am using JTable.
    When creating a simple JTable and displaying with Nimbus, the row background color alternates between white and a light blue-grey.
    I want the table with single colour. Only the first column should be grey colour and other should be white.
    I used this code to set the background and foreground colour.
    public Component prepareRenderer
        (TableCellRenderer renderer, int index_row, int index_col){      
                Component objComponent = super.prepareRenderer(renderer, index_row, index_col);         
                Color objGreyColour = new Color(240,240,240);
                Color objWhiteColour = new Color(255,255,255);
                if(index_col == 0){
                    objComponent.setBackground(objGreyColour);
                    objComponent.setFont(new java.awt.Font(CommonClass.getFontName(),java.awt.Font.BOLD, CommonClass.getFontSize()));
                    objComponent.setForeground(Color.BLACK);
                }else{               
                    setSelectionBackground(Color.BLUE);
                    objComponent.setBackground(objWhiteColour);
                    objComponent.setForeground(Color.BLACK);
                return objComponent;
            } Look wise it is fine but when i try to select the cell it is not highlighting the cell and also i m not able to select multiple cell with ctrl key.
    Any help would be appreciated
    Thanks
    Sonal

    Hello,
    1) if you want better help soone,r please post an SSCCE (http://sscce.org) instead of a code extract.
    2) selection highlighting is lost because your code... doesn't handle selection. To take selection into account, you can use <tt>if (super.isRowselected(index_row)) {...}</TT>.
    Regards,
    J.

  • How to set the width of a cell in JTable?

    I have created a JTable and I want to set the width of cells.How can I do that?

    This is now the third person to tell you that Swing questions should be posted in the Swing forum.
    You have several postings out there where you have been given an answer but you haven't bothered to respond to the posting. Indicating whether the suggestion was helpfull or not. I guess you really don't want help from people in the future.

  • Setting the Size of a Column in a JTable

    Hi there,
    does anybody knows a way how to set the size of a specified column in a JTable without changinge the size of the other columns?
    thx anyway
    Errraddicator

    You've got to change the JTable's column resizing policy to prevent the other columns resizing:
    JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)Otherwise all the columns will be adjusted to fit into the width of the table - changing one column would have to result in at least one other column changing!
    Hope this helps.

  • Setting the jTable column width

    Coming from VB6 to Java is hard for me. Syntax is so much different.
    Below is what I am doing to populate the jTable and the values are coming from MySQL. My problem is setting the column width but I am having difficult understanding how to do it. I looked on the website and downloaded some examples and clearly because I am new and I simply do not understand it. I was wonder if someone is willing to modify the codes below on how I should do it. Thanks
    private void populate_grid(){
    Vector dataVector = new Vector();
    Vector columnVector = new Vector();
    columnVector.add("ID#");
    columnVector.add("Account Description");
    columnVector.add("Contact");
    columnVector.add("State");
    while (myResult.next()) {
    Vector rowVector = new Vector();
    System.out.println(myResult.getString("acct_name"));
    rowVector.add(myResult.getString("cust_id"));
    rowVector.add(myResult.getString("acct_name"));
    rowVector.add(myResult.getString("main_contact"));
    rowVector.add(myResult.getString("main_state"));
    dataVector.add(rowVector);
    jTable1.setModel(new javax.swing.table.DefaultTableModel(dataVector, columnVector));

        int rows = 3;
        int cols = 3;
        JTable table = new JTable(rows, cols);
        // Disable auto resizing
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // Set the first visible column to 100 pixels wide
        int vColIndex = 0;
        TableColumn col = table.getColumnModel().getColumn(vColIndex);
        int width = 100;
        col.setPreferredWidth(width);

  • HELP!! set the cell color in jTable

    I need to set the color of a specific cell in jTable, but, I need to retain the old color in the jTable. It must not reset the whole table.
    would anyone help me in this problem... PLSSSSS its urgent

    A flexible variation on this is to specify formatting options in the JTable's client properties:
    // Use some sort of special "key" to indicate the cell and that you want to set the background color
    String key = "background:" + row + "," + column;
    table.putClientProperty(key, Color.green);You can then write a general table cell renderer that looks for these special formatting options and it doesn't disrupt your existing TableModel in any way:
    public class FormattingTableCellRenderer extends DefaultTableCellRenderer
      public Component getTableCellRendererComponent(JTable table, Object value, int row, int column, ...)
        Component component = super.getTableCellRendererComponent(table, value, row, column, ...);
        // See if the JTable specifies any properties to override the default format
        Color backgroundColor = (Color)table.getClientProperty("background:" + row + "," + column);
        if(backgroundColor != null)
          component.setBackground(backgroundColor);
        return component;
    }Any custom cell renderers you have then simply need to extend FormatTableCellRenderer instead of DefaultTableCellRenderer.
    It's very easy to add formatting options other than background, such as font, to the list of recognised attributes. You'll probably need to make some alterations to get it to recognise selection/focus but hopefully you get the idea.
    Hope this helps.

  • Setting the color  of a JTable

    1) How can I change the color of the box containing the colum title in
    a JTable
    2) How can I change the background of only one row in a JTable
    3) How can I change the color of a ScrollBar in a JComboBox
    Thanks!

    Check out the link shown below....it has numerous examples that answer some if not all of your questions.
    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
    Also, check out the tutorial at the link shown below:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
    ;o)
    V.V.

  • JTable Model   --  How to set the width of columns

    Hello,
    This project of mine is due in 2 days. :(
    I am using AbstractTableModel and I can't figure out how to set the columns' width inside my JTable?
    This following code does not work.
    int vColIndex = 1;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    int width=399;
    col.setPreferredWidth(width);Please remember, that I'm using AbstractTableModel and not just simple JTables.
    Thanks in advance.

    The DataModel (AbstractTableModel or otherwise) has nothing to do with the table column size. The DataModel is used to store data.
    When you create a JTable using a DataModel then a TableColumn is created for each column in the DataModel using a default column size. The normal order of creation would be:
    TableModel model = new DefaultTableModel(...);
    JTable table = new JTable(model);
    Now that the table and table columns are created you can change the size of the columns:
    int vColIndex = 1;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    int width=399;
    col.setPreferredWidth(width);
    So your code looks ok.
    However, if somewhere in you code you do:
    a) table.setModel(...)
    b) fireTableStructureChanged();
    then the table columns are recreated based on the current DataModel and the table column size would be reset to the default values.

  • HELP Friends!!! How to set the width of columns of JTABLE?

    Hi!
    How to set the width of a particular column in JTable?
    I tried using headerRenderers and though I'm able to set the color and font of the header (using getTableCellRendererComponent method)I can't change the size.
    Pls some one help me..it's very urgent.
    Thanks in advance.
    Regards,
    Naval

    JTable.getColumnModel().getColumn(i).setWidth(width). There are also setPreferredWidth, setMaxWidth and setMinWidth methods.

Maybe you are looking for

  • Is the latest ipod touch compatible with the 2011 Mitsubishi Lancer Ralliart?

    I recently replaced an older iPod touch with a new iPod touch. The older touch synced with my 2011 mitsubishi lancer. Granted it took the two awhile to communicate but they always synced. I just got the new iPod touch on Feb 3 and tried to sync it wi

  • Multiple monitor fullscreen support

    One of the features that was announced for Mountain Lion was Full screen support for multiple monitors, however while I can now full-screen to either of my monitors, it stiull blanks out the other monitor which is kinda useless. Any plans or ways to

  • DW PHP date update issue?

    When I use the DW8 PHP UPDATE behavior, I get an error reported back from the web server when the date value in the form is MM/DD/YYYY .. but if I change it to YYYY-MM-DD then it updates fine. What can I do to assure that we can use the user friendly

  • Material Management Material Needed Please help!

    Hi, I am new to SAP, have a MS in mech engineering. I am taking some classes but the class does not have any PDF tutorial material. I am looking for a tutorial which has all the steps with Screen dumps for complete MM implementation. I greatly apprec

  • Saving from PS to LR with Changes

    I recently started taking anamorphic still which I export to CS 5 from LR 5.7 to edit them.. I then "unstretch" from the 2X to render a normal perspective. Looks fine in CS5, but when I save as a TIF back to LR it doesn't retain the new aspect ratio.