Highlighting cell content on focusing in a JTable

Hi everybody, i've been searching the forum after loosing some time of my own, but I don't get the cell content of a (JTextField) CellEditor in a JTable highlighted on focusing.

I overwrote the changeSelection() method in JTable, used getEditorComponent() and select() in JTextComponent to select the text in the cell. The text is selected 'cause when I type a new character the old text disappears but I can't get the old text to be highlighted first so users realise that the cell is really selected an ready to edit.

Can anyone help with this?

[569 byte] By [tango1383a] at [2007-10-2 4:25:47]
# 1

You need to understand the difference between a renderer and and editor. When you tab from cell to cell the renderer is used to display the data. When you edit a cell the editor is used.

Just because you overwrote the changeSelection() method does not mean you placed the cell in edting mode. If you truly want to place the cell in editing mode then you should use something like:

public void changeSelection(int row, int column, boolean toggle, boolean extend)

{

super.changeSelection(row, column, toggle, extend);

if (editCellAt(row, column))

{

Component editor = getEditorComponent();

editor.requestFocusInWindow();

((JTextComponent)editor).selectAll();

}

}

If you just want it to look like you are in editing mode then you would need to use a custom renderer. Something like:

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.table.*;

public class TableSelectAllRenderer extends JFrame

{

public TableSelectAllRenderer()

{

JTable table = new JTable(5, 5);

table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

TableCellRenderer highlightRenderer = new SelectAllRenderer();

table.setDefaultRenderer(Object.class, highlightRenderer);

}

public static void main(String[] args)

{

TableSelectAllRenderer frame = new TableSelectAllRenderer();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

/*

** Highlight only the text, not the entire background

*/

class SelectAllRenderer extends DefaultTableCellRenderer

{

private Color selectionBackground = UIManager.getColor("TextField.selectionBackground");

private Border editBorder = BorderFactory.createLineBorder(Color.BLACK);

private boolean cellHasFocus;

public Component getTableCellRendererComponent(

JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

{

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

cellHasFocus = hasFocus;

return this;

}

protected void paintComponent(Graphics g)

{

if (cellHasFocus && !getText().equals(""))

{

setBorder( editBorder );

g.setColor( selectionBackground );

g.fillRect(0, 0, getPreferredSize().width, getSize().height);

}

super.paintComponent(g);

}

}

}

camickra at 2007-7-15 23:53:57 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for your help. Thought I got the difference between the renderer and the editor but didn't think that without calling requestFocus() on the editor the renderer still had the focus. Thanks a lot.
tango1383a at 2007-7-15 23:53:57 > top of Java-index,Desktop,Core GUI APIs...