> I think this is important because the user can type a new value
> thout mouse-click into cell or F2 (or arrows) to force editing.
Yes, but the text gets "appended" to the end of the cell. It does not "replace" the text in the cell. So selecting the cell is also not correct, because it implies that entire text will be replace when text is typed.
Here is a solution that uses a renderer to highlight the text and also changes the default mode of the table to replace the text rather than append to the text:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class TableSelectAll extends JFrame
{
public TableSelectAll()
{
JTable table = new JTable(5, 5)
{
// Select the text when the cell starts editing
// a) text will be replaced when you start typing in a cell
// b) text will be selected when you use F2 to start editing
// c) text will be selected when double clicking to start editing
public boolean editCellAt(int row, int column, EventObject e)
{
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor != null && editor instanceof JTextComponent)
{
if (e == null)
{
((JTextComponent)editor).selectAll();
}
else
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((JTextComponent)editor).selectAll();
}
});
}
}
return result;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
TableCellRenderer highlightRenderer = new SelectAll();
table.setDefaultRenderer(Object.class, highlightRenderer);
}
public static void main(String[] args)
{
TableSelectAll frame = new TableSelectAll();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
/*
** Highlight only the text, not the entire background
*/
class SelectAll 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);
}
}
}
Hi,
First of all, thanks for your great code and help. This code had worked fine for JTextField.
However, I have a DefaultCellEditor with a JComboBox too.
It works fine if I press F2 to edit or use mouse.
But, for example, if values in JComboBox are "aaa", "bbb" and "ccc", I would like to press "b" and it automatically shows popup of items with "bbb" line pre-selected, something like the default behaviour of normal JComboBoxes (out of JTables), without press F2 before "b".
And, if possible, is there some way to enable AutoCompletion in this JComboBox of JTable?
Other strange behaviour of JComboBoxes into JTables is that when user press F2, uses down-arrow to select a item, and after press ENTER, supposing it want to use that item, the focus is lost of JTable and no items become selected. In normal JComboBoxes, ENTER works fine.
I tried to get the EditorComponent for JComboBox, changing the code by camickr to support also JComBox editor component, but it didn't work.