JDK-4273908 : Selection not shown when focus lost
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.6
  • CPU: unknown
  • Submitted: 1999-09-21
  • Updated: 1999-11-19
  • Resolved: 1999-11-19
Related Reports
Relates :  
Description
Text in a JTextArea that is selected has a different background color.
When that text area loses focus, either because a button is pushed or
the window loses focus, the background color of the text area selection
reverts to the nonselected background color.

Very similar to 4244100

JDK 1.2.2 reference Solaris and 1.2.2 Win32, not happening with 1.2.1.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TestSelect extends JFrame implements ActionListener {

 JCheckBox ckbox;
 JRadioButton radio;
 JScrollPane panel;
 JTextArea ta;
 Container cp;

 public TestSelect() {
  super ("Test Select");
  cp = getContentPane();
  cp.setLayout (new GridLayout(2,2));

  ckbox = new JCheckBox("check Box");
  ckbox.addActionListener(this);
  cp.add(ckbox);

  radio = new JRadioButton("radio Button");
  radio.addActionListener(this);
  cp.add(radio);

  ta = new JTextArea();
  panel = new JScrollPane (ta);
  cp.add(panel);

  setSize(250, 200);
  setVisible(true);
 }

 public void actionPerformed (ActionEvent evt) {
  System.out.println (">>action " + evt);
 }

 public static void main (String[] args) {
  new TestSelect();
 }
}

Comments
WORK AROUND In the end, the developer can control whether or not a selection is displayed by the setting of the selectionVisible property on the Caret, and the developer is free to install their own Caret implementation which is done as a subclass of DefaultCaret. Below is an example. *** Diff of supplied TestSelect.java *************** *** 2,7 **** --- 2,8 ---- import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; + import javax.swing.text.*; public class TestSelect extends JFrame implements ActionListener { *************** *** 10,15 **** --- 11,17 ---- JScrollPane panel; JTextArea ta; Container cp; + DefaultCaret dfltCaret; public TestSelect() { super ("Test Select"); *************** *** 25,30 **** --- 27,34 ---- cp.add(radio); ta = new JTextArea(); + dfltCaret = new MyCaret(); + ta.setCaret(dfltCaret); panel = new JScrollPane (ta); cp.add(panel); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ carolyn.lowenthal@Eng 1999-10-11 New MyCaret.java: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import javax.swing.text.DefaultCaret; public class MyCaret extends DefaultCaret { // Always leave the selection highlighted public void setSelectionVisible(boolean ignoredChoice) { super.setSelectionVisible(true); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ carolyn.lowenthal@Eng 1999-11-10
10-11-1999

PUBLIC COMMENTS Text in a JTextArea that is selected has a different background color. When that text area loses focus, either because a button is pushed or the window loses focus, the background color of the text area selection reverts to the nonselected background color. Very similar to 4244100 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The behavior in JDK1.2.1 maintained a global selection which was package private, and a source of memory leaks. Setting a new selection somewhere caused the selection to be removed on the old selection owner. This was behavior you could not remove and was destructive, making it a bad behavior for a toolkit. It was also different than what you get with Windows. On Windows, changing focus between two windows causes the selection highlighting to move to the current window with focus without destroying the selection between focus moves. Selecting a menu item however does not remove the selection highlighting even though focus is temporarily lost. This is handled in AWT by marking the focus lost event as a temporary loss of focus. If you used the swing text components with native menus you would get behavior associated with Windows. Unfortunately, there is no way for the java-based swing menus to get a focus lost event that is temporary to be generated. carolyn.lowenthal@Eng 1999-11-10
10-11-1999

EVALUATION The current behavior regarding whether the selection is shown is more correct than previous behaviors which had a number of bugs. The Java developer can control whether or not a selection is displayed by the setting of the selectionVisible property on the Caret. The developer is free to install their own Caret implementation which can be done as a subclass of DefaultCaret. carolyn.lowenthal@Eng 1999-10-04
04-10-1999