Auto-highlighting (and selecting) editable table cell contents

From: JMC (jomccann40_at_hotmail.com)
Date: 03/31/04


Date: 31 Mar 2004 09:34:54 -0800

SHORT FORM OF QUESTION:
Can someone please supply a noddy program that displays an editable
table, such that as the user tabs through the cells, or clicks in a
new cell, that cell's contents becomes selected *and highlighted*!

LONGER FORM: Some folks have said here that since 1.3,
JTextField.selectAll only seems to highlight the text when it is
invoked upon receipt of a FOCUS_GAINED event (for the JTextField). I
have got that to work (example below).

However, I am having great difficulty in plugging
HighlightableTextField into an editable JTable on a per-cell basis
such that when the user tabs through the cells, or selects a cell, the
existing cell contents become highlighted. This must be something to
do with the fact that individual cells of a JTable don't get focus
gain/loss events (?).

This example shows it working fine when the "cells" are in fact
separate stand-alone components in a panel, but how to do it when the
cells are in a table?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public abstract class Tester {

    protected final JFrame frame;
    protected final Container fcp;
    
    protected Tester() {

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Tester");
        frame.setSize(400, 400);
        frame.setLocation(200, 100);

        fcp = frame.getContentPane();
        fcp.setLayout(new FlowLayout());
    }
    
    protected abstract void init();
    
    protected void go() {
        init();
        frame.pack();
        frame.show();
    }
    
    public static void main(String[] args) {
        new TableEditing().go();
    }
}

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableEditing extends Tester {

    class HighlightableTextField extends JTextField {
        public HighlightableTextField(int c) {
            super(c);
        }
        protected void processFocusEvent(FocusEvent fe) {
            if (fe.getID() == FocusEvent.FOCUS_GAINED) {
                selectAll();
            }
            else if (fe.getID() == FocusEvent.FOCUS_LOST) {
                select(0,0);
            }
            super.processFocusEvent(fe);
        }
    }

    protected void init() {
        fcp.add(new HighlightableTextField(6));
        fcp.add(new HighlightableTextField(6));
        fcp.add(new HighlightableTextField(6));
    }
}

Thanks for any help....



Relevant Pages

  • Re: darker highlighting
    ... Depending on the monitor the highlight of the selected cells in Excell 2007 ... Try adjusting the contrast of your monitor - either up or down. ... dingy - especially if you then adjust the contrast down. ...
    (microsoft.public.excel.misc)
  • RE: Select rows equal to filter and highlight row
    ... following example demonstrates how to apply color to just the visible cells ... Sub VisibleCellsDemo() ... Dim rngFiltered As Range ... I am trying to pick out rows after a certain date, then highlight them so ...
    (microsoft.public.excel.programming)
  • Re: darker highlighting
    ... Depending on the monitor the highlight of the selected cells in Excell 2007 ... Try adjusting the contrast of your monitor - either up or down. ... dingy - especially if you then adjust the contrast down. ...
    (microsoft.public.excel.misc)
  • Re: Check for duplicate entries???
    ... When you say make sure the cells within the values are highlighted, ... I went to his site and tried the "Highlighting Duplicate ... In Conditional Formatting ... This first example will highlight duplicate rows in the range B2:B11. ...
    (microsoft.public.excel)
  • Re: Check for duplicate entries???
    ... Share the values that you entered into the two cells. ... I went to his site and tried the "Highlighting Duplicate ... This first example will highlight duplicate rows in the range B2:B11. ... the cells that you wish to test and format, ...
    (microsoft.public.excel)