I believe I have discovered a bug with focus lost events for a text field.  I have a text field with a focus event listener and a menu.  If I click into the text field, the text field correctly receives the focus gained event.  However, if I click on the menu item, the text field never receives a focus lost event.  This is different behavior than Swing.

 

Can you provide any workarounds?  I am trying to implement a solution where menu items become enabled or disabled based upon whether a text field has focus.

 

Also, Swing’s JMenu can have menu listeners that will receive menu selected, menu deselected and menu cancelled events.  Is there an equivalent in ULC?   If I can get the menu select event, then I could assume that the text field no longer has focus.  

 

Finally, is there any equivalent Swing UndoManager functionality in ULC?  Do you have any built in support for implementing undo functionality in ULC?

 

I have provided a test case for ULC and Swing to demonstrate the focus bug.

 

ULC Version: 6.1.1

JVM: Java 5 (1.5.0_06)

 

Kind regards,

 

Les

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ClientContext;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCComboBox;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCMenu;
import com.ulcjava.base.application.ULCMenuBar;
import com.ulcjava.base.application.ULCMenuItem;
import com.ulcjava.base.application.ULCProxy;
import com.ulcjava.base.application.ULCTextField;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.FocusEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IFocusListener;
import com.ulcjava.base.application.event.IKeyListener;
import com.ulcjava.base.application.event.KeyEvent;
import com.ulcjava.base.application.util.Color;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.shared.UlcEventCategories;
import com.ulcjava.base.shared.UlcEventConstants;

public class TestULCFocus extends AbstractApplication {
	private ULCTextField textField = null;

	public void start() {

		textField = new ULCTextField(20);
		textField.addFocusListener(new IFocusListener() {
			public void focusGained(FocusEvent event) {
				textField.setBackground(Color.gray);
				System.out.println("textfield Gained");
			}

			public void focusLost(FocusEvent event) {
				textField.setBackground(Color.white);
				System.out.println("textfield Lost");
			}
		});
		
		ClientContext.setEventDeliveryMode(textField, UlcEventCategories.FOCUS_EVENT_CATEGORY, UlcEventConstants.ASYNCHRONOUS_MODE);

		ULCBoxPane content = new ULCBoxPane();
		content.add(ULCBoxPane.BOX_EXPAND_CENTER, textField);

		ULCFrame frame = new ULCFrame("Test Focus Lost");

		ULCMenuBar menuBar = new ULCMenuBar();
		ULCMenu editMenu = new ULCMenu("Edit");
		editMenu.addActionListener(new IActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("editMenu actionPerformed");
			}	
		});
		
		ULCMenuItem cut = new ULCMenuItem("Cut");
		ULCMenuItem copy = new ULCMenuItem("Copy");
		ULCMenuItem paste = new ULCMenuItem("Paste");

		editMenu.add(cut);
		editMenu.add(copy);
		editMenu.add(paste);
		menuBar.add(editMenu);

		frame.setMenuBar(menuBar);

		frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
		frame.add(content);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		DevelopmentRunner.setApplicationClass(TestULCFocus.class);
		DevelopmentRunner.main(args);
	}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;

public class TestSwingFocus {

  public static void main(String args[]) {

    JFrame frame = new JFrame("Clipboard demo");
    JTextField tf = new JTextField();

    JMenuBar menuBar = new JMenuBar();
    JMenu edit = new JMenu("Edit");

    edit.addMenuListener(new MenuListener() {
      public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected...");
      }

      public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeselected...");
      }

      public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled...");
      }
    });

    edit.addKeyListener(new KeyListener() {
      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
        System.out.println("keyPressed...");
      }

      public void keyReleased(KeyEvent e) {
      }
    });

    edit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Edit menu action performed...");
      }
    });

    edit.add(new JMenuItem("Cut"));
    edit.add(new JMenuItem("Copy"));
    edit.add(new JMenuItem("Paste"));

    menuBar.add(edit);

    frame.setJMenuBar(menuBar);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    final JTextField textField = new JTextField(10);
    textField.addFocusListener(new FocusListener() {
      public void focusGained(FocusEvent e) {
        System.out.println("Focus gained..");
      }

      public void focusLost(FocusEvent e) {
        System.out.println("Focus lost..");
      }
    });

    panel.add(BorderLayout.NORTH, textField);
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setVisible(true);
  }
}

Reply via email to