JDK-4309156 : Text highlight disappears when menu selected - non Windows standard behaviour
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 2000-02-02
  • Updated: 2000-12-08
  • Resolved: 2000-12-08
Related Reports
Duplicate :  
Description

Name: skT88420			Date: 02/01/2000


java version "1.3.0rc1"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

We have an app where text can be selected and copied.  After the highlight,
selecting a menu causes the highlight to disappear.  The SwingSet demo has this
behaviour also when viewer the "Plain Text".  I believe this issue has been
brought to your attention before, but I could not find it in the bug lists.

Here is some sample code to reproduce the issue:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public final class SelectionBug extends JFrame {
	public static void main(String argv[]) {
		SelectionBug app = new SelectionBug();
	}
	public SelectionBug() {
		super("Selection Bug");
		
		final JTextArea area = new JTextArea();
		JScrollPane sp = new JScrollPane(area);
		sp.setPreferredSize(new Dimension(400,300));

		area.setWrapStyleWord(true);
		area.setLineWrap(true);
		area.append("Highlight some of this text and then select the Edit menu.  Doing so causes the highlight ");
		area.append("to disappear.  Selecting copy will copy the previously highlighted text and when the menu ");
		area.append("disappears the highlight reappears.  ");
		area.append("This is confusing to our users.  Is there a way to leave the text visibly highlighted ");
		area.append("the whole time?");

		JMenu menu = new JMenu("Edit");
		Action copy = new AbstractAction("Copy") {
			public void actionPerformed(ActionEvent e) {
				area.copy();
			}
		};
		menu.add(copy);
		Action paste = new AbstractAction("Paste") {
			public void actionPerformed(ActionEvent e) {
				area.paste();
			}
		};
		menu.add(paste);
		JMenuBar menubar = new JMenuBar();
		menubar.add(menu);
		setJMenuBar(menubar);
		
		getContentPane().add(sp);
		pack();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		show();
	}
}
(Review ID: 100658) 
======================================================================
Additional Info from Cust: 2000-05-12
If the test area has the focus everything works fine.  This explains why using
a menu causes the text to disappear since the menu momentarily has the focus.

The following example is an extension to the example I created for bug
mentioned earlier.  It has a button that can be used to select text.  Since the
button retains the focus after the selecting is done, the highlight never
appears.  Press the Tab key until the text area has the focus and viola!

I hope this helps!

  Comments about the bug and reproducing it are included in the program.

--CODE


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public final class SelectionBug extends JFrame {
	JTextField start, end;
	public static void main(String argv[]) {
		SelectionBug app = new SelectionBug();
	}
	public SelectionBug() {
		super("Selection Bug");
		
		final JTextArea area = new JTextArea();
		JScrollPane sp = new JScrollPane(area);
		sp.setPreferredSize(new Dimension(400,300));

		area.setWrapStyleWord(true);
		area.setLineWrap(true);
		area.append("Highlight some of this text and then select the
Edit menu.  Doing so causes the highlight ");
		area.append("to disappear.  Copy and paste still work.  This is
confusing to our users.\n");
		area.append("Put a value in the start and end text fields.
Press the 'Select' button.  ");
		area.append("No text is visibly selected, yet copy and paste
still work.  Press the tab key");
		area.append(" until the text area has the focus, and the
highlight appears.\n");
		area.append("Uncomment the line that requests focus to work
around the issue.\n");

		JMenu menu = new JMenu("Edit");
		Action copy = new AbstractAction("Copy") {
			public void actionPerformed(ActionEvent e) {
				area.copy();
			}
		};
		menu.add(copy);
		Action paste = new AbstractAction("Paste") {
			public void actionPerformed(ActionEvent e) {
				area.paste();
			}
		};
		menu.add(paste);
		JMenuBar menubar = new JMenuBar();
		menubar.add(menu);
		setJMenuBar(menubar);
		JToolBar toolbar = new JToolBar();
		JButton b = new JButton("Select");
		b.addActionListener( new ActionListener()  {
			public void actionPerformed(ActionEvent e)  {
				System.out.println("Selecting Text");
				area.setSelectionStart(Integer.parseInt (start.getText()));
				area.setSelectionEnd(Integer.parseInt
(end.getText()));
//				area.requestFocus();
			}
		});
		start = new JTextField(2);
		start.setText("0");
		end = new JTextField(2);
		end.setText("10");
		
		toolbar.add(b);
		toolbar.add(new JLabel("Start"));
		toolbar.add(start);
		toolbar.add(new JLabel("End"));
		toolbar.add(end);
		getContentPane().add(toolbar,BorderLayout.NORTH);
		
		getContentPane().add(sp);
		pack();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		show();
	}
}
----2000-05-12

Name: ks88420			Date: 08/30/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


// The program below works okay with 1.2.1 but not 1.3
// Compile and run the program ... the following results:

// 1.2.1   the textArea to the left has some "blue" selected text
//   select the "flip" button and the other textArea has "red" selected text
//   the first textArea having correctly lost its selection.

// 1.3     the textArea to the left has some "blue" selected text
//   select the "flip" button and the other textArea has **NO** selected text
//   the first textArea having correctly lost its selection.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Test extends JFrame {
	// small program to demonstrate that 1.3 doesn't seem to handle
	// setSelection() method in JTextArea properly
	// ... works okay in 1.2.1

	//--------------------------------------
	static void t(String s){System.out.println("Test- "+s);}

	Container ct;
 	JTextArea ta,ta2;
	boolean flip;
 
	//--------------------------------------
	public Test(String title) {
		super(title);
		ct=getContentPane();
		ct.setLayout(new FlowLayout());
		ta = new JTextArea("freddy");
		ta.setSelectionColor(Color.blue);
		ta.setSelectionStart(0);
		ta.setSelectionEnd(4);

		ta2 = new JTextArea("freddy2");
		ta2.setSelectionColor(Color.red);

		JButton flipButton = new JButton("flip");
		flipButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if(flip) {
					ta.setSelectionStart(0);
					ta.setSelectionEnd(4);
				}
				else {
					ta2.setSelectionStart(0);
					ta2.setSelectionEnd(4);
				}
				flip=!flip;
			}
		});
		ct.add(ta);
		ct.add(flipButton);
		ct.add(ta2);
	}

	//--------------------------------------
	public static void main(String[] args) {
		final Test f = new Test("Test");
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
		f.pack();
		f.setVisible(true);
	}

}
(Review ID: 109118)
======================================================================