Auto-highlighting (and selecting) editable table cell contents
From: JMC (jomccann40_at_hotmail.com)
Date: 03/31/04
- Next message: Jean-Marc Vanel: "[ANN] ecolosim 0.2"
- Previous message: dsp: "JAXB how to drop tag element in xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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....
- Next message: Jean-Marc Vanel: "[ANN] ecolosim 0.2"
- Previous message: dsp: "JAXB how to drop tag element in xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|