Remove Cell Selection Border in JTable

Hi
I have multiple columns in JTable populated with data. When the row is selected, the row changes colour. It also places a border around the cell that is selected within the row.
Is there some way to remove this border?
Any help would be appreciated.
Thanks
Gregg

Hi,
you need a different tableCellRenderer. Read:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom
There are also code examples.
Phil

Similar Messages

  • Table cell selection border

    hi,
    how can you find out the default color of the table cell renderer border?
    when you select a cell in a JTable, there's (by default) a thin border drawn around it. since i use my own renderer for some columns, i need to find out the color of this border (it is differnt for different look & feels).
    thanksout the default

    you were right. thank you.
    just one more thing: where can i find the names of all those properties that the UIManager uses? like the one you used "Table.focusCellHighlightBorder"...

  • How to remove cells interection line in JTable

    Hello
    I am new to Java Programming
    I am facing a problem which i did not know how to solve so i need help from experts of this forum
    I have JTable of lets say 5 columns and from that i want to hide some columsn e.g i want to get only column 0(or ist column) and column 5(5th column) visible to me and not the inbetween columns(i.e 2,3,4) so i coded it like this:
    for(int count=0;count< ConnectedDrivestable.getColumnCount();count++)
                   tcm = ConnectedDrivestable.getColumnModel();
                   cm=tcm.getColumn(count);
                  if( count==0 || count==4)
                       cm.setPreferredWidth(15);
                   cm.setMinWidth(0);                   
                       cm.setMaxWidth(15);                   
                  else {
                       //ConnectedDrivestable.removeColumn(cm);
                           //ConnectedDrivestable.removeColumn(tcm.getColumn(count));
                       cm.setPreferredWidth(0);
                   cm.setMinWidth(0);          
                       cm.setMaxWidth(0);
              }where ConnectedDrivestable is my JTable of lets say 5 rows and 5 columns
    tcm is TableColumnModel and cm is TableColumn object
    But the problem is that i am getting vertical line in between and that is the intersection line of column 1 and column 5(i.e the intersection line of two columns that are visible) i dont want this in between vertical line to appear
    I really dont know how to fix this problem i have searched on internet but unable to find the appropriate solution to it
    Also as shown in my code i tried to work with removeColumn method which documentation says should remove the desired columns but i dont know why it is not functioing for my case the way i acpected am i doing something wrong or?
    So urgent help from people here is required
    Thanks in advance
    Imran

    Hello
    Thanks for reply and thanks for help
    Do please tell me that i am rendering cells of JTable with JLabel it is working fine but i want to span JLabel onto multicells in the same row
    Second i want to change the size of JLabel so that if not needed JLabel did not cover the whole Cell
    A bit of code which i am using to do cel rendering is attched for your kind considerations
    ConnectedDrivestable.getColumnModel().getColumn(1).setCellRenderer((new  DefaultTableCellRenderer ()
                   public Component getTableCellRendererComponent(JTable table, Object value,
                             boolean isSelected,
                             boolean hasFocus,
                             int row, int column)
                        JLabel Jlbl=new JLabel();
                        if(row==2)
                             ((JComponent)Jlbl).setOpaque(true); //if comp is a JLabel:Necessary
                             //Jlbl.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                             Jlbl.setBackground(Color.GREEN);
                             Jlbl.setLocation(row,column);
                             System.out.print("The Column number is : "+column);
                             System.out.print("The answer is: "+ column+2);
                             ((JComponent)Jlbl).setMinimumSize(new Dimension(1,1));
                             ((JComponent)Jlbl).setMaximumSize(new Dimension(0,0));
                        return Jlbl;
                        //return comp;
              }));Another piece of code which i got from internet for doing the same is
    ConnectedDrivestable.getColumnModel().getColumn(2).setCellRenderer((new  DefaultTableCellRenderer ()
                   public Component getTableCellRendererComponent(JTable table, Object value,
                             boolean isSelected,
                             boolean hasFocus,
                             int row, int column)
                        Component comp = super.getTableCellRendererComponent
                             (table, value, isSelected, hasFocus, row, column);                         
                        //((JComponent)comp).setBorder(new LineBorder(Color.BLACK));                         
                        if(row==2 && column==2)
                             //((JComponent)comp).setBorder(new LineBorder(Color.BLACK));
                             comp.setBackground(Color.GREEN);
                             ((JComponent)comp).setMinimumSize(new Dimension(1,1));
                             ((JComponent)comp).setMaximumSize(new Dimension(1,1));                                   
                        else
                             comp.setBackground(Color.white);
                        return comp;
              }));These both work but as i tried to change the size of JLabel in the Cells it did not work also if i wanted to span JLabels to multiple cells(Column) is it not working
    Also as in code snippet i am trying to set the border which works fine but what i want is that after spanning JLabel to multiple cells then i want border along this how to do this help in this regard too?might if i could span JLabel to diffrent columns might i able to set border aroung them too
    I am putting under old forum topic this question as it is realted to my same problem so do please apologize me.
    I do need urgent help in this regard so kind help is needed
    Regards

  • How to catch change cell selection in a JTable?

    I'm using a ListSelectionListener with a JTable to catch the list selection changes but it only detects row changes, and if i select a different cell but within the same row it does not trigger the event.
    Do you guys know a way to detect this event?
    Thanks!!!

    I'm using a ListSelectionListener with a JTable to catch the list selection changes but it only detects row changes,Try adding a listener for the columns as well:
    table.getTableHeader().getColumnModel().getSelectionModel().addListSelectionListener(...);

  • Multiple Cell Selection in a JTable (again...)

    Hi All -
    I'm trying to get a JTable working well with the ability to select multiple cells in the way you'd expect to be able to (in Excel for example). By this I mean, hold down ctrl to add to a selection, hold shift to select between the last selection and the new one, be able to drag multiple regions with the mouse and have them be selected.
    I've seen lots of talk about this, for example on this thread:
    http://forum.java.sun.com/thread.jspa?threadID=619580&start=0&tstart=0
    (the code here will not work in the 'general' case, for example it doesn't really support dynamic table resizing)
    ...and found some pretty extensive code from here:
    http://www.codeguru.com/java/articles/663.shtml
    ...that kinda half works. (but the selection model is very strange once you get it running. Not at all what the average user would be used to)
    Does anyone have this working 100% with code they can share? It is very surprising to me that this is not the default behavior in Swing to be honest.
    I'm sure that I can come up with a solution for my situation, but I may as well not waste the time if someone already has something working for this.
    Thanks!

    If you have columnSelectionAllowed,rowselectionAllowed and cellSelectionEnabled then you can select cell by cell if you use the control key,
    Now the problem is, if you don't have input by your keyboard, in my case, I have a big table and I need to select diferent cells that match a certain criteria,
    jTable2.setRowSelectionInterval(j,j); //but this only works not for different rows 5 - 120 - 595
    I'm trying to use:
    jTable2.changeSelection()
    but still don't have good results....
    If any one could help to this matter will be really appreciated...
    thanks...

  • Remove cell and border

    I am trying this to hide my table cell when the XML value is blank, but it is still printing the cell border etc.
    <xsl:if test="SCHOOL_YEAR_10[normalize-space(.) != '']">
    It there anyway to completely remove the cell from rendering base don that condition?

    Use @column
    eg <?if@column:FIELDNAME!=''?><?FIELDNAME?><?end if?>
    this will hide column if fieldname is null.
    Thanks

  • How to restrict the cell selection in JTable based on cell contents...

    Hi all,
    I have some problem related to table cell selection.
    I have a table that represets the calendar of some month and year.
    I have the restriction that at one time only one cell could be selected.
    Now i want the cell seletion like this,
    I want only those dates to be selected if that date is after 'today'.
    that is I want to restrict the selection of previous dates.
    how can i do this.
    I have overridden the table methods like this, still no use.
    table = new JTable(model) {
    setOpaque(false);
    setSurrendersFocusOnKeystroke(false);
    setRowHeight(20);
    JTableHeader header = getTableHeader();
    header.setOpaque(false);
    getTableHeader().setReorderingAllowed(false);
    setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    getTableHeader().setResizingAllowed(false);
    setCellSelectionEnabled(true);
    setFocusable(true);
    setBackground(new Color(247, 251, 255));
    setFont(newFont);
    public boolean isCellEditable(int row, int col) {
    return false;
    public boolean isCellSelected(int row, int col) {
    int r = getSelectedRow();
    int c = getSelectedColumn();
    String val = (String)getValueAt(r,c);
    Calendar cal = Calendar.getInstance();
    cal.set(currentYear+1900, month,Integer.parseInt(val));
    Date d = cal.getTime();
    Date currentDate = new Date();
    if (d.before(currentDate)) {
         return false;
    } else {
         return true;
    Someone please help...
    -Soni

    Try overriding the changeSelection(....) method of JTable.
    public void changeSelection(int row, int column, boolean toggle, boolean extend)
         if (d.after(currentDate)
              super.changeSelection(row, column, toggle, extend);
    }

  • How do I remove a selected set of cells?

    How do I remove or delete a selected set of cells (ex. rectangle of C1 to E26).
    In Excel, I can delete such a group of cells and all the data to the right of those cells shifts over.
    Is this possible in Numbers or should I just go back to Excel?

    Michael Force wrote:
    Thank you for your attempt to help. This does not work for me, perhaps because I failed to fully understand what Yvan is trying to say. You are saying I need to delete cells at the bottom of the column,
    It's not what I wrote.
    Assuming that you want to remove cells C12 thru C14, I asked you to select cells C15 thru C9999999 then move the selected block to cell C12.
    Old cell C15 will become cell C12
    Old cell C16 will become cell C13
    Old cell C17 will become cell C14
    Your problem is different than the original one which was:
    I want to delete a range of cells in a single column, say for instance 3 cells, and have the entire column shift up 3 cells.
    and I'm not sure that the "Delete cells" function of Excel will do the trick.
    I assumes (because you didn't give the complete info) that you want to remove cells C12 thru C14.
    Select cells C15 thru C40, move the block to C12.
    Old cell C15 will become cell C12
    Old cell C16 will become cell C13
    Old cell C17 will become cell C14
    Old cell 40 will become cell C37
    cells C38, C39, C40 will be empty.
    Yvan KOENIG (from FRANCE lundi 17 novembre 2008 11:59:29)

  • Problem in cell selection in JTable Java Swing

    hii
    I am using JTable that shows data from database. I have developed one functionality. I change one cell value and then select multiple cell. If I press F12 function key then it copies first selected value and past it to another selected cells. it is working properly but when I press F12 it sets the last selected cell in editable mode. I want that all cell which has been pasted should be selected (with default blue colour not in editing mode). I am using ListSelectionListener for JTable Cell Selection and KeyListener for F12 functionality.
    Please give some solution
    I can not override editCellAt Method because i have already extended JFrame. and i tried to do this with Jtable.getSelectedColumn and getSelectedRow it is displaying the same value, by this how can I get the particular area of selection means Row nd Column Selected.
    How would I know the current selection when user selects more than one cell???
    Thanks
    Edited by: 850979 on 11-Apr-2011 02:13
    Edited by: 850979 on 11-Apr-2011 03:59

    camickr wrote:
    Presumably, JTable installs an action for the F12 key that results in starting editing the last selected cell.
    There are no bindings for F12. See the [url http://www.camick.com/java/blog.html?name=key-bindings]Key Bindings listing. (...)
    Right.
    Of course it doesn't make sense that a F? key should invoke the editorI thought he meant F2 ("edit" on Windows), so that sounded natural.
    All KeyEvents are passed to the editor for the cell and the editor is psuedo invoked. That is, the caret is not placed on the text field used as the editor, but the character typed is added to the editor. You can then either use tab/enter to save the text or use the escape key to cancel the editing.Thanks for the explanation. I had never noticed (I kept on double-clicking my JTable cells when I wanted to edit them! :o)
    Of course it doesn't make sense that a F? key should invoke the editor, but this is a by product of all KeyEvents being forwarded so you don't have to have special Key Bindings for every character.You're right - as always.
    They (Sun) could have filtered out some of the keys such as those, though, because as noted by the OP the current behavior is unintuitive.
    To better control when the cell is editable based on the KeyEvent you can override the table with code like <snipped>
    As you suggested using a Key Binding will prevent this behaviour as well (but only for that key).Right again, but in this specific case that is acceptable (the developer wants to attach a specific behavior to a specific key only).
    OP here is an SSCCE of that second approach:
    public class TestKeyBindingsOnJTable {
        public static void main(String... args) {
            final DefaultTableModel dtm = new DefaultTableModel(new String[] {"A", "B"}, 0);
            dtm.addRow(new String[]{"A1", "B1"});
            dtm.addRow(new String[]{"A2", "B2"});
            dtm.addRow(new String[]{"A3", "B3"});
            JTable table = new JTable(dtm);
            table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0), "clear");
            table.getActionMap().put("clear", new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    dtm.removeRow(0);
            JOptionPane.showMessageDialog(null, table);
    }Edited by: jduprez on Apr 8, 2011 9:36 PM

  • How to do Multi Cell Selection in JTable

    Dear Friends,
    Any body know Multi cell selection in jtable......
    Thanks

    Try overriding the changeSelection(....) method of JTable.
    public void changeSelection(int row, int column, boolean toggle, boolean extend)
         if (d.after(currentDate)
              super.changeSelection(row, column, toggle, extend);
    }

  • Focus stays on cell when I select column in JTable

    Hi,
    I am using a JTable and I have defined a function key cntrl-D to perform a function on a selected column. This works fine if I have not yet edited one of the cells in the column, but if I edit any cell and then click the column header and hit cntrl-D, then the column is deselected, the top cell enters edit mode, and my function is called with only one cell selected (always the top cell) instead of the whole column.
    When I debug this, I find that my function is called first, and processKeyEvent in the cell editor is called later. The editor doesn't handle it and calls super().
    I think that somehow the cell is keeping the focus even though I try to deselect it when the column is selected in my column header listener. Any suggestions welcome.
    Here is the code I use to select the column in the column header listener. I have verified that it is called when I click the header:
    public void mouseClicked(MouseEvent e)
    table.requestFocusInWindow();
    Point thePoint = e.getPoint();
    int theColumnIndex = table.columnAtPoint(thePoint);
    int theLastRowIndex = table.getRowCount() - 1;
    if (theLastRowIndex > 0 && theColumnIndex > 0 && theColumnIndex < table.getColumnCount() )
    if ( e.isShiftDown() && table.getSelectedRowCount() == table.getRowCount() )
    table.editingStopped(null);
    if ( table.isEditing() )
    return;
    if ( theColumnIndex <= mColumnStart )
    table.setColumnSelectionInterval( theColumnIndex, mColumnStart);
    else if ( theColumnIndex >= mColumnStart )
    table.setColumnSelectionInterval( mColumnStart, theColumnIndex );
    else if ( e.isControlDown() && table.getSelectedRowCount() == table.getRowCount() )
    table.editingStopped(null);
    if ( table.isEditing() )
    return;
    if (table.isColumnSelected(theColumnIndex))
    table.removeColumnSelectionInterval(theColumnIndex, theColumnIndex);
    else
    table.addColumnSelectionInterval(theColumnIndex, theColumnIndex);
    else
    table.editingStopped(null);
    if ( table.isEditing() )
    return;
    table.clearSelection();
    table.setEditingRow(-1);
    table.setEditingColumn(-1);
    table.setRowSelectionInterval(0, theLastRowIndex);
    table.setColumnSelectionInterval(theColumnIndex, theColumnIndex);
    mColumnStart = theColumnIndex;
    SampleManager.getInstance().setupMenus();

    I found the answer. This is a feature that worked in 1.3.1 but broke in 1.4.1. It turns out that there is a new feature in JTable that jumps into edit mode when a key event is detected, and that causes the column selection to be canceled. To inactivate this feature I put the following line into the constructor of my JTable subclass:
    putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

  • Jtable in cell how to single mouse click make the cell selected.

    it seems need double click make cell selected.
    thanks!!!

    Hi,
    these link will help you.
    It has code for both examples, with 1 click and with 2 clicks:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=362073&tstart=0&trange=15
    sergio.

  • Fully independent cell selections

    I have been working thru the Swing Tables tutorial to learn how to select nonadjacent cells as would be needed in a week calendar (viewed as a 7 x 24 table) to select a 3rd shift � e.g., Monday 3rd shift is Monday 2300 thru Tuesday 0700. I see two notes in the tutorial that makes me think that is �not in the cards�, so to speak, with JTable. Here is the note from the tutorial:
    NOTE: JTable uses a very simple concept of selection, managed as an intersection of rows and columns. It was not designed to handle fully independent cell selections.
    NOTE: Setting cellSelectionEnabled to a value has the side effect of also setting both rowSelectionEnabled and columnSelectionEnabled to that value. Setting both rowSelectionEnabled and columnSelectionEnabled to a value has the side effect of also setting cellSelectionEnabled to that value. Setting rowSelectionEnabled and columnSelectionEnabled to different values has the side effect of also setting cellSelectionEnabled to false.
    Fair enough; JTable is not the answer. Unfortunately, the tutorial does not offer any hints about how to manage fully independent cell selection. So far, there does not appear to be a way to add that support by customizing a Table Model.
    Anybody have any ideas about what else to use?

    Use this code and change it as appropriate.
    <table style="width: 100%" border="1" class="style1">
                    <tr>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 34%"> </td>
                    </tr>
                    <tr>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 34%"> </td>
                    </tr>
                    <tr>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 33%"> </td>
                                    <td style="width: 34%"> </td>
                    </tr>
    </table>
    <table style="width: 100%" border="1" class="style1">
                    <tr>
                                    <td colspan="2" style="width: 50%"> </td>
                                    <td style="width: 50%"> </td>
                    </tr>
                    <tr>
                                    <td colspan="2" style="width: 50%"> </td>
                                    <td style="width: 50%"> </td>
                    </tr>
    </table>
    hth

  • Selection Problem with JTable

    Hello,
    i have a selection problem with JTable. I want to allow only single cell selection and additionally limit the selection to the first column.
    I preffered the style from MS Outlook Express where you can select the email accounts to edit.
    It is a table like this:
    Account name  |   Type  |   ...
    --------------|---------|---------------------
    Hotmail       |   POP3  |
    GMX           |   IMAP  |The selection should be only avaibable at 'Hotmail' or 'GMX' - not at 'POP3', 'IMAP' or as complete row selection.
    Please help me!
    Thanks.
    Warlock

    Maybe this will helpimport java.awt.*;
    import javax.swing.*;
    public class Test3 extends JFrame {
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One", "Two"};
        String[][] data = {{"R1-C1", "R1-C2"}, {"R2-C1", "R2-C2"}};
        JTable jt = new JTable(data, head);
        jt.getColumnModel().setSelectionModel(new MyTableSelectionModel());
        content.add(new JScrollPane(jt), BorderLayout.CENTER);
        jt.setCellSelectionEnabled(true);
        jt.setRowSelectionAllowed(false);
        jt.setColumnSelectionAllowed(false);
        setSize(300, 300);
        setVisible(true);
      public static void main(String[] arghs) { new Test3(); }
    class MyTableSelectionModel extends DefaultListSelectionModel {
      public void setSelectionInterval(int index0, int index1) {
        super.setSelectionInterval(0, 0);
    }

  • How to get the default selection color from JTable

    Hi, there,
    I have a question for how to get the default selection color from JTable. I am currently implementing the customized table cell renderer, but I do want to set the selection color in the table exactly the same of default table cell renderer. The JTable.getSelectionBackgroup() did not works for me, it returned dark blue which made the text in the table unreadable. Anyone know how to get the window's default selection color?
    Thanks,
    -Jenny

    The windows default selection color is dark blue. Try selecting any text on this page. The difference is that the text gets changed to a white font so you can actually see the text.
    If you don't like the default colors that Java uses then use the UIManager to change the defaults. The following program shows all the properties controlled by the UIManager:
    http://www.discoverteenergy.com/files/ShowUIDefaults.java
    Any of the properties can be changed for the entire application by using:
    UIManager.put( "propertyName", value );

Maybe you are looking for