JDialog loses opacity after deiconify - java

Creating a transparent and undercoated JDialog with a JFrame as its parent
will lose transparency upon an iconify/deiconify sequence.
Example:
final JFrame f = new JFrame();
f.setSize(200, 200);
final JDialog d = new JDialog(f, false);
d.setSize(200, 200);
d.setUndecorated(true);
d.setOpacity(.8f);
f.setLocationRelativeTo(null);
d.setLocation(f.getLocation().x + 210, f.getLocation().y);
f.setVisible(true);
d.setVisible(true);
Before iconify, the JDialog is created fine. Here is a screenshot.
After an iconify/deiconify sequence, the JDialog is 100% opaque.
I'm looking for solution to this problem and thinking I'm doing something
incorrect or missing some code.
My environment is Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
running on Microsoft Windows [Version 6.1.7601].
2014-11-13: There is an open JDK bug for this problem
JDK-8062946 Transparent JDialog will lose transparency upon iconify/deiconify sequence.
https://bugs.openjdk.java.net/browse/JDK-8062946

Seems like a bit of a bug to me.
The following seems to work using JDK7 on Windows 7:
f.addWindowListener( new WindowAdapter()
{
#Override
public void windowDeiconified(WindowEvent e)
{
d.setVisible(false);
d.setVisible(true);
}
});

Try the following. I've had various problems with the undecorated JDialog and when I find a
bypass to a new symptom I add it to this method. This is a trimmed example. Add error checking, exception handling and modify as necessary to suit your needs ( ie add listener removal code if your app creates and destroys passed parentWindow objects).
package trimmed.stackoverflow.example;
import java.awt.Color;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
public class Helper_Swing {
private static Helper_Swing instance;
private Helper_Swing() {
instance = this;
}
public static Helper_Swing getInstance() {
return instance == null ? new Helper_Swing() : instance;
}
private static class J42WindowAdapter extends WindowAdapter {
final private Window window;
final private JDialog dialog;
private Color colorBG = null;
private float opacity = 0.0f;
private J42WindowAdapter(final Window window, final JDialog dialog) {
super();
this.window = window;
this.dialog = dialog;
this.colorBG = window.getBackground();
this.opacity = window.getOpacity();
}
#Override
public void windowIconified(final WindowEvent e) {
colorBG = dialog.getBackground();
opacity = dialog.getOpacity();
dialog.setOpacity(1.0f); // Must call 1st
dialog.setBackground(Color.BLACK); // Must call 2nd
}
#Override
public void windowDeiconified(final WindowEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog.setBackground(colorBG);
dialog.setOpacity(opacity);
dialog.setVisible(true);
dialog.repaint();
}
});
}
}
public void bugFix_transparentJDialog(final Window parentWindow, JDialog childWindow) {
if (parentWindow != null && childWindow != null && childWindow.isUndecorated()) {
final WindowListener[] listeners = parentWindow.getWindowListeners();
for (int x = 0; x != listeners.length; x++) {
if (listeners[x] instanceof J42WindowAdapter) {
final J42WindowAdapter adapter = (J42WindowAdapter) listeners[x];
if (adapter.window == parentWindow && adapter.dialog == childWindow) {
childWindow = null;
break;
}
}
}
if (childWindow != null) {
parentWindow.addWindowListener(new J42WindowAdapter(parentWindow, childWindow));
}
}
}
}
Usage example:
Helper_Swing.getInstance().bugFix_transparentJDialog(parentWindow, dialog);

Related

What does this warning mean in Vaadin: "Ignoring RPC call for disabled connector com.vaadin.ui.Window"?

I have tried to google to find some hints and look at the Vaadin's website, but I didn't find anything related to this king of issue:
In the console when I launch the application, I see this warning several times:
Ignoring RPC call for disabled connector com.vaadin.ui.Window, caption=Window's caption
I am using the Refresher addon which polls my server at an interval of 2000 millisec. and the ICEPush addon which implements pushing to the Vaadin UI.
I think that this is related somehow to the Refresher addon, because if I interact with a component I have created for test (below the code), warnings are added to the console.
Here is the code:
package com.example.events;
import java.util.ArrayList;
import java.util.List;
import com.github.wolfie.refresher.Refresher;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.event.LayoutEvents.LayoutClickNotifier;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
public class Noticeboard extends VerticalLayout {
/**
*
*/
private static final long serialVersionUID = -6023888496081433464L;
private static List<Note> notes = new ArrayList<Note>();
private List<Window> windows = new ArrayList<Window>();
private static int userCount;
private int userId;
private static int noteId;
private Refresher refresher = new Refresher();
private static final int UPDATE_INTERVAL = 2000;
private Note currentlyFocusedNote;
public class NoticeboardUpdater extends Thread {
#Override
public void run() {
while (true) {
try {
Thread.sleep(UPDATE_INTERVAL);
} catch (Exception e) {
e.printStackTrace();
}
getUI().getSession().getLockInstance().lock();
try {
updateNoticeboard();
} finally {
getUI().getSession().getLockInstance().unlock();
}
}
}
}
public Noticeboard() {
refresher.setRefreshInterval(UPDATE_INTERVAL);
userId = ++userCount;
setSpacing(true);
setMargin(true);
addComponent(new Label("Logged in as User " + userId));
Button addNoteButton = new Button("Add note");
addNoteButton.addClickListener(new ClickListener() {
/**
*
*/
private static final long serialVersionUID = -4927018162887570343L;
#Override
public void buttonClick(ClickEvent event) {
Note note = new Note(++noteId);
note.setCaption("Note " + note.getId());
notes.add(note);
Window window = createWindow(note);
windows.add(window);
UI.getCurrent().addWindow(window);
}
});
addComponent(addNoteButton);
addExtension(refresher);
new NoticeboardUpdater().start();
}
private Window createWindow(final Note note) {
final Window window = new Window(note.getCaption());
VerticalLayout layout = new VerticalLayout();
layout.addComponent(createContentNote(note, window));
window.setContent(layout);
window.setWidth(300, Unit.PIXELS);
window.setResizable(false);
window.setPositionX(note.getPositionX());
window.setPositionY(note.getPositionY());
window.setData(note);
window.addBlurListener(createBlurListener(window));
window.addFocusListener(createFocusListener(window));
LayoutClickNotifier mainLayout = (LayoutClickNotifier) getUI().getContent();
mainLayout.addLayoutClickListener(e -> {
if (note.isNoteFocusedWindow() || note.isNoteFocusedTextArea()) {
if (note.getLockedByUser() > -1 && note.getLockedByUser() == userId) {
unlockNote(getWindow(currentlyFocusedNote));
note.setNoteFocusedWindow(false);
note.setNoteBlurredWindow(false);
note.setNoteFocusedTextArea(false);
note.setNoteBlurredTextArea(false);
currentlyFocusedNote = null;
}
}
});
return window;
}
private TextArea createContentNote(final Note note, final Window window) {
TextArea contentNote = new TextArea();
contentNote.setSizeFull();
contentNote.setValue(note.getText());
contentNote.setImmediate(true);
contentNote.setTextChangeEventMode(TextChangeEventMode.EAGER);
contentNote.addBlurListener(createBlurListener(window));
contentNote.addFocusListener(createFocusListener(window));
contentNote.addTextChangeListener(new TextChangeListener() {
/**
*
*/
private static final long serialVersionUID = 8552875156973567499L;
#Override
public void textChange(TextChangeEvent event) {
note.setText(event.getText());
}
});
return contentNote;
}
private BlurListener createBlurListener(Window window) {
return e -> {
Component blurredComponent = e.getComponent();
if (blurredComponent == window) {
currentlyFocusedNote.setNoteBlurredWindow(true);
}
else if (blurredComponent == (((Layout) window.getContent())).iterator().next()) {
currentlyFocusedNote.setNoteBlurredTextArea(true);
}
};
}
private FocusListener createFocusListener(Window window) {
return e -> {
Component focusedComponent = e.getComponent();
Note note = (Note) window.getData();
if (currentlyFocusedNote != null && currentlyFocusedNote != note) {
unlockNote(getWindow(currentlyFocusedNote));
currentlyFocusedNote.setNoteFocusedWindow(false);
currentlyFocusedNote.setNoteBlurredWindow(false);
currentlyFocusedNote.setNoteFocusedTextArea(false);
currentlyFocusedNote.setNoteBlurredTextArea(false);
}
currentlyFocusedNote = note;
if (focusedComponent == window) {
Notification.show("Focused Note Window");
currentlyFocusedNote.setNoteFocusedWindow(true);
}
else if (focusedComponent == (((Layout) window.getContent())).iterator().next()) {
Notification.show("Focused Note TextArea");
currentlyFocusedNote.setNoteFocusedTextArea(true);
}
if (currentlyFocusedNote.isNoteFocusedWindow() && currentlyFocusedNote.isNoteBlurredTextArea() ||
currentlyFocusedNote.isNoteFocusedTextArea() && currentlyFocusedNote.isNoteBlurredWindow()) {
// Lock is already set here, skipping
return;
}
lockNote(window);
};
}
private void lockNote(Window window) {
Note note = (Note) window.getData();
note.setLockedByUser(userId);
String caption = "Locked by User " + userId;
note.setCaption(caption);
window.setCaption(caption);
}
private void unlockNote(Window window) {
Note note = (Note) window.getData();
note.setLockedByUser(-1);
note.setPositionX(window.getPositionX());
note.setPositionY(window.getPositionY());
note.setCaption("Note " + note.getId());
window.setCaption("Note " + note.getId());
}
private void updateNoticeboard() {
for (Note note : notes) {
Window window = getWindow(note);
if (window == null) {
window = createWindow(note);
windows.add(window);
UI.getCurrent().addWindow(window);
}
if (note.getLockedByUser() > -1) {
if (note.getLockedByUser() != userId) {
// If the note is locked by another user, then we disable this window.
window.setEnabled(false);
updateTextArea(window, note);
updateWindowPosition(window, note);
}
else {
// Otherwise the window is enabled.
window.setEnabled(true);
Note focusedNote = (Note) window.getData();
updateFocusedNotePosition(focusedNote, window);
}
}
else {
window.setEnabled(true);
updateTextArea(window, note);
updateWindowPosition(window, note);
}
}
}
private void updateTextArea(Window window, Note note) {
Layout layout = (Layout) window.getContent();
TextArea area = (TextArea) layout.iterator().next();
area.setValue(note.getText());
}
private Window getWindow(Note note) {
for (Window window : windows) {
if (window.getData().equals(note))
return window;
}
return null;
}
private void updateWindowPosition(Window window, Note note) {
window.setPositionX(note.getPositionX());
window.setPositionY(note.getPositionY());
window.setCaption(note.getCaption());
}
private void updateFocusedNotePosition(Note focusedNote, Window window) {
focusedNote.setPositionX(window.getPositionX());
focusedNote.setPositionY(window.getPositionY());
focusedNote.setCaption(window.getCaption());
}
}
And inside the UI's init method I simply use this Noticeboard class:
#Override
protected void init(VaadinRequest request) {
setContent(new Noticeboard());
}
When I move a window created with the "Add note" button or change the focus from a window to another, I experience the warning.
What could be the reason of such an issue?
I know, code is not of the better ones, it is just to see how the this Vaadin addons behave.
When you disable a component, the disabled state is handled both on the client and the server side, meaning, a component isn't just visually disabled, but the server also refuses to handle any requests to a disabled component.
The warning you are seeing means, that there is an RPC request (HTTP request from the client-side) that is targeted for a disabled component. Since the component is disabled, the RPC request will be ignored.
This typically happens when you have a background thread that disables a component and then also do polling from the client-side.

JDesktopPane resize

We have a application with two JFrames with two JDesktopPanes.
We need to move an internal frame from one frame to another.
The problem we have is that after we move the internalframe from first window to the second window, when we resize the fist window, the internal frame of the second window also gets resized.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
class FirstFrame extends JFrame
{
JDesktopPane desktopPane = new JDesktopPane();
SecondFrame secondFrame;
public FirstFrame(SecondFrame secondFrame)
{
this.secondFrame = secondFrame;
setTitle("FirstFrame example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(desktopPane);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem item = new JMenuItem("Move");
item.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent actionevent)
{
moveFrame();
}
});
menu.add(item);
menuBar.add(menu);
setJMenuBar(menuBar);
}
public void addAnInternalFrame()
{
JInternalFrame frame = new JInternalFrame();
frame.setTitle("An Internal Frame");
desktopPane.add(frame);
frame.setVisible(true);
frame.setMaximizable(true);
try
{
frame.setSelected(true);
frame.setMaximum(true);
}
catch (PropertyVetoException e)
{
e.printStackTrace();
}
}
public void moveFrame()
{
JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
desktopPane.remove(selectedFrame);
desktopPane.repaint();
secondFrame.addInternalFrame(selectedFrame);
}
}
class SecondFrame extends JFrame
{
JDesktopPane desktopPane = new JDesktopPane();
public SecondFrame()
{
setTitle("SecondFrame example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(desktopPane);
}
public void addInternalFrame(JInternalFrame frame)
{
desktopPane.add(frame);
}
}
public class DesktopPaneExample
{
public static void main(String args[]) throws PropertyVetoException
{
SecondFrame secondFrame = new SecondFrame();
FirstFrame firstFrame = new FirstFrame(secondFrame);
firstFrame.setSize(400, 400);
firstFrame.setLocation(100, 100);
firstFrame.setVisible(true);
firstFrame.addAnInternalFrame();
secondFrame.setSize(400, 400);
secondFrame.setLocation(520, 100);
secondFrame.setVisible(true);
}
}
In the above sample application, to reproduce
1) click menu File>move
2) resize the first window
NOTE: This is reproducable in Java 1.7 only. I use jdk1.7.0_03.
Update: add more information
This was not reproducible on Java 1.6 (jdk1.6.0_21)
The issue is due to Java 7's tweaking on javax.swing.plaf.basic.BasicInternalFrameUI implementation.
Java 1.6 Code
public void propertyChange(PropertyChangeEvent evt) {
if ((frame.getParent() != null) && !componentListenerAdded) {
f.getParent().addComponentListener(componentListener);
componentListenerAdded = true;
} else if ((newValue == null) && componentListenerAdded) {
if (f.getParent() != null) {
f.getParent()
.removeComponentListener(componentListener);
}
componentListenerAdded = false;
}
Java 1.7 Code
public void propertyChange(PropertyChangeEvent evt) {
if ((frame.getParent() != null) && !componentListenerAdded) {
f.getParent().addComponentListener(componentListener);
componentListenerAdded = true;
}
NOTE: The else if condition was removed. This is the culprit.
I suggest you 2 options:
Option one
JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
desktopPane.remove(selectedFrame);
desktopPane.repaint();
secondFrame.updateUI(); // The magic part, less expensive execution.
secondFrame.addInternalFrame(selectedFrame);
Option two
You may need to recompile javax.swing.plaf.basic.BasicInternalFrameUI.java with above "else if" condition and add to your rt.jar library's javax.swing.plaf.basic location.
I have attached the recompiled files for Java 1.7.0_25 at http://www.datafilehost.com/d/dfb7238c
Hope this helps!!!
Regards,
Nilindra
It seems as if adding the frame while in it's maximum state is the culprit. To maintain it's current size on the 1st frame over to the 2nd frame, try this:
public void moveFrame()
{
JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
Dimension currentSize = selectedFrame.getSize();
try
{
selectedFrame.setMaximum(false);
}
catch (PropertyVetoException ex)
{
ex.printStackTrace();
}
selectedFrame.setSize(currentSize);
desktopPane.remove(selectedFrame);
desktopPane.repaint();
secondFrame.addInternalFrame(selectedFrame);
}
EDIT:
After reading the API for Container#remove(Component c), I got this idea that seems to work:
public void moveFrame()
{
final JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
desktopPane.remove(selectedFrame);
desktopPane.repaint();
SwingUtilities.updateComponentTreeUI(selectedFrame);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
secondFrame.addInternalFrame(selectedFrame);
}
});
}

In Java how do you trap the event before the new JTab is switched to?

how do you trap the event before the new tab is switched to?
In every Tab I have JTable and i do something with it's data(delete, add , update). I would like to do data validation(save or cancel changes) before switching to the new tab. I use Java 1.5.
class ViewPanel extends JPanel
{
private void Components() {
setPreferredSize(new Dimension(700, 400));
tabbedPane.addTab("DC", ANSFER.getIcon(),new DcTabPanel(this), "DC");
tabbedPane.addTab("PC", THUMB4.getIcon(),new PcTabPanel(this), "PC");
tabbedPane.addChangeListener(this);
add(tabbedPane);
}
public void stateChanged(ChangeEvent e) {
}
}
JTabbedPane is backed by a SingleSelectionModel. If you extend DefaultSingleSelectionModel, you can override the setSelectedIndex method and implement your logic.
// in new selection model:
public void setSelectedIndex(int index) {
// do pre-switch things here
super.setSelectedIndex(index);
}
// in ViewPanel, on tabbedPane create:
tabbedPane.setModel(newSelectionModel);
The reason you can't simply use a ChangeListener is because that fires on change. By extending the selection model, you fire before the tab change.
You can prevent tab switching by extending JTabbedPane and override setSelectedIndex(int). Here is a small example illustrating that. It simply prevents from switching between non-contiguous tabs:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class Test2 {
private static class BlockingTabbedPane extends JTabbedPane {
public static interface TabSwitchAllower {
public boolean allowTabSwitch(int from, int to);
}
private TabSwitchAllower allower;
public BlockingTabbedPane(TabSwitchAllower allower) {
super();
this.allower = allower;
}
#Override
public void setSelectedIndex(int index) {
if (allower == null || allower.allowTabSwitch(getSelectedIndex(), index)) {
super.setSelectedIndex(index);
}
}
}
protected static void initUI() {
final JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BlockingTabbedPane.TabSwitchAllower allower = new BlockingTabbedPane.TabSwitchAllower() {
#Override
public boolean allowTabSwitch(int from, int to) {
if (Math.abs(from - to) == 1) {
return true;
} else {
JOptionPane.showMessageDialog(frame, "You can only switch between contiguous tabs");
}
return false;
}
};
JTabbedPane tabbedPane = new BlockingTabbedPane(allower);
for (int i = 0; i < 10; i++) {
tabbedPane.addTab("Tab-" + i, new JLabel("Hello tab " + i));
}
frame.add(tabbedPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initUI();
}
});
}
}
java actionlistener on a tab
How to Write a Change Listener (Oracle Docs)
JTabbedPane API (Oracle Docs)
Those two links should help you out. I haven't really worked with tabbedPanes, but I am assuming that the getSelectedComponent() will return the current selected tab. So you can have a handle to the currentTab which will be set during instantiation. Then you can have something like this.
class TabListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
// Replace JSlider with whatever your tab's data type is
JSlider source = (JSlider)e.getSource();
// Use the 'currentTab' handle to do what you want.
currentTab = getSelectedComponent();
// I'm assuming that the 'selected component' by the time this stuff
// runs is going to be the new selected tab.
}
}
I am not too confident about my answer, but I certainly hope that this will point you towards the right direction! Please say if you need any clarification or anything! If I happen to discover anything that I think might be useful, I'll be certain to edit my answer!

Java LostFocus and InputVerifier, moving in reverse-tab-order

I have a GUI application that uses an InputVerifier to check the content of text fields before yielding the focus. This is all very normal. Yesterday, however, discovered a problem - it seems to be a bug, but I cannot find any mention of it anywhere. Before I report this as a bug, I thought I would ask: am I missing something obvious here?
Situation:
A set of text fields with InputVerifiers.
Listeners for FocusLost and FocusGained on all controls, so I can see what is happening.
A separate thread uses a DefaultKeyboardFocusManager to report (every 2 seconds) which control has the focus.
I place invalid data in a JTextField in the middle of the form, and try to leave the control.
If I try to move away from this control using the mouse, or using the tab-key, I cannot. The FocusLost event does not fire and the control properly retains the focus.
However, if I try to move away from the control in reverse tab order, using Shift-Tab, sometimes the FocusLost event fires. If this happens, the separate thread reports that no control has the focus, i.e., getFocusOwner() returns null.
Edit: below is a small sample program that shows the problem. The problem has nothing to do with the extra thread - the thread is just there to make the problem more obvious. If there is a race-condition, it is somewhere in Swing.
To see the problem, go to the second text box and empty it. The control should retain the focus, and does so unless you leave it by pressing shift-tab. Unlike the full application, the error seems to occur here 100% of the time. This is true both under OpenJDK 6 and under Oracle Java 7.
This is almost too obvious to be a bug, plus it happens in multiple Java environments. Hence, my suspicion that I am missing something obvious. Anyone?
public class FocusBugDemo extends JFrame {
static JTextField txtOne = new JTextField("Ignore this control");
static JTextField txtTwo = new JTextField("Delete this text, then press shift-tab");
static JLabel lblFocus = new JLabel("");
static KeyboardFocusManager kfm = new DefaultKeyboardFocusManager();
public static void main(String[] args) {
new FocusBugDemo();
Thread t = new Thread() {
#Override
public void run() {
while(true) {
Component c = kfm.getFocusOwner();
String focusInfo = "elsewhere";
if (c == null) { focusInfo = "null";
} else if (c == txtOne) { focusInfo = "txtOne";
} else if (c == txtTwo) { focusInfo = "txtTwo";
}
lblFocus.setText(System.currentTimeMillis() + " - Focus owner " + focusInfo);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
};
t.start();
}
private FocusBugDemo() {
super("Focus bug demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300,100));
setLayout(new GridLayout(3,1));
NotEmpty validator = new NotEmpty();
txtOne.setInputVerifier(validator);
txtTwo.setInputVerifier(validator);
add(txtOne);
add(txtTwo);
add(lblFocus);
pack();
setVisible(true);
}
private class NotEmpty extends InputVerifier {
#Override
public boolean verify(JComponent input) {
JTextField txtField = (JTextField) input;
return (txtField.getText().length() > 0);
}
}
}
Now reported to Oracle as bug 7167871.
Using your sscce, I am unable to reproduce the effect you describe on Mac OS X, Java 6, which supports #CatalinaIsland's observation. In particular, focus never leaves an empty text field using either tab or shift-tab; focus becomes null only when the frame is deactivated.
I see two threads accessing multiple fields with no synchronization at all. At a minimum, you should use EventQueue.invokeLater() in t to update the GUI, as described in Concurrency in Swing and show below.
The broader question is this: What focus problem are you trying to solve using t?
import java.awt.Component;
import java.awt.DefaultKeyboardFocusManager;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FocusDemo {
private static final JTextField txtOne =
new JTextField("Ignore this control");
private static final JTextField txtTwo =
new JTextField("Delete this text, then press shift-tab");
private static final JLabel lblFocus = new JLabel("");
public static void main(String[] args) {
new FocusDemo();
Thread t = new Thread() {
#Override
public void run() {
while (true) {
EventQueue.invokeLater(new Runnable() {
KeyboardFocusManager kfm =
new DefaultKeyboardFocusManager();
#Override
public void run() {
Component c = kfm.getFocusOwner();
String focusInfo = "elsewhere";
if (c == null) {
focusInfo = "null";
} else if (c == txtOne) {
focusInfo = "txtOne";
} else if (c == txtTwo) {
focusInfo = "txtTwo";
}
lblFocus.setText(System.currentTimeMillis()
+ " - Focus owner " + focusInfo);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}
};
t.start();
}
private FocusDemo() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Focus bug demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(300, 100));
f.setLayout(new GridLayout(3, 1));
NotEmpty validator = new NotEmpty();
txtOne.setInputVerifier(validator);
txtTwo.setInputVerifier(validator);
f.add(txtOne);
f.add(txtTwo);
f.add(lblFocus);
f.pack();
f.setVisible(true);
}
});
}
private class NotEmpty extends InputVerifier {
#Override
public boolean verify(JComponent input) {
JTextField txtField = (JTextField) input;
return (txtField.getText().length() > 0);
}
}
}

How do you hide a Swing Popup when you click somewhere else

I have a Popup that is shown when a user clicks on a button. I would like to hide the popup when any of the following events occur:
The user clicks somewhere else in the application. (The background panel for example)
The user minimizes the application.
The JPopupMenu has this behavior, but I need more than just JMenuItems. The following code block is a simplified illustration to demonstrate the current usage.
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class PopupTester extends JFrame {
public static void main(String[] args) {
final PopupTester popupTester = new PopupTester();
popupTester.setLayout(new FlowLayout());
popupTester.setSize(300, 100);
popupTester.add(new JButton("Click Me") {
#Override
protected void fireActionPerformed(ActionEvent event) {
Point location = getLocationOnScreen();
int y = (int) (location.getY() + getHeight());
int x = (int) location.getX();
JLabel myComponent = new JLabel("Howdy");
Popup popup = PopupFactory.getSharedInstance().getPopup(popupTester, myComponent, x, y);
popup.show();
}
});
popupTester.add(new JButton("No Click Me"));
popupTester.setVisible(true);
popupTester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Use a JPopupMenu. You can add any component to it, not just menu items.
As pajton noted in a previous comment, Popup is not a JComponent to which listeners can be readily bound. But, as its documentation states, "implementations of Popup are responsible for creating and maintaining their own Components to render [its subject] to the user."
So in using it as your presentation mechanism, your Popup is going to have to present itself as an actual Swing component anyway. Have it register itself to that component. Have it hide itself when the component loses focus.
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Popup;
public class PopupTester extends JFrame {
private static class MessagePopup extends Popup
implements WindowFocusListener
{
private final JDialog dialog;
public MessagePopup(Frame base, String message) {
super();
dialog = new JOptionPane().createDialog( base, "Message" );
dialog.setModal( false );
dialog.setContentPane( new JLabel( message ) );
}
#Override public void show() {
dialog.addWindowFocusListener( this );
dialog.setVisible( true );
}
#Override public void hide() {
dialog.setVisible( false );
dialog.removeWindowFocusListener( this );
}
public void windowGainedFocus( WindowEvent e ) {
// NO-OP
}
public void windowLostFocus( WindowEvent e ) {
hide();
}
}
public static void main(String[] args) {
final PopupTester popupTester = new PopupTester();
popupTester.setLayout(new FlowLayout());
popupTester.setSize(300, 100);
popupTester.add(new JButton("Click Me") {
#Override
protected void fireActionPerformed(ActionEvent event) {
Point location = getLocationOnScreen();
MessagePopup popup = new MessagePopup( popupTester, "Howdy" );
popup.show();
}
});
popupTester.add(new JButton("No Click Me"));
popupTester.setVisible(true);
popupTester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You can add MouseListener to your background panel and hide the popup when somebody clicks on the panel.
To react on application minimization, use WindowListener attached to a JFrame.
Etc, etc. May seem tedious, but surely will work.
Thanks pajton and Noel Ang for getting me pointed in the right direction! Here is the solution that I ended up with. I'm just including it here so that others may benefit from it.
I ended up going with a JWindow since it doesn't get the window decorations but does get focus events.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopupTester extends JFrame {
private static class MessagePopup extends Popup implements WindowFocusListener {
private final JWindow dialog;
public MessagePopup(Frame base, JLabel component, int x, int y) {
super();
dialog = new JWindow(base);
dialog.setFocusable(true);
dialog.setLocation(x, y);
dialog.setContentPane(component);
component.setBorder(new JPopupMenu().getBorder());
dialog.setSize(component.getPreferredSize());
dialog.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
dialog.setVisible(false);
}
}
});
}
#Override
public void show() {
dialog.addWindowFocusListener(this);
dialog.setVisible(true);
}
#Override
public void hide() {
dialog.setVisible(false);
dialog.removeWindowFocusListener(this);
}
public void windowGainedFocus(WindowEvent e) {
// NO-OP
}
public void windowLostFocus(WindowEvent e) {
hide();
}
}
public static void main(String[] args) {
final PopupTester popupTester = new PopupTester();
popupTester.setLayout(new FlowLayout());
popupTester.setSize(300, 100);
popupTester.add(new JButton("Click Me") {
#Override
protected void fireActionPerformed(ActionEvent event) {
Point location = getLocationOnScreen();
int x = (int) location.getX();
int y = (int) (location.getY() + getHeight());
JLabel myComponent = new JLabel("Howdy");
MessagePopup popup = new MessagePopup(popupTester, myComponent, x, y);
popup.show();
}
});
popupTester.add(new JButton("No Click Me"));
popupTester.setVisible(true);
popupTester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You could add a FocusListener to your popup-window, and dispose it when it loses focus. However, that will cause you some troubles when the focus loss is due to some other application (new windows comes to the foreground, you switch virtual desktops, etc.)
But perhaps you (a) know that that cannot happen in your case or (b) would want to close the popup in such cases anyway, a focus-based approach may still be interesting to you.
I know this is an old question but I really needed the Popup to work in my case. So I tried a few things and the following is my solution.
Add a FocusListener to the component you add to the popup and program the focusLost event on that component to hide the popup when focus is lost. Call the requestFocus method on your component just after showing the popup.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.*;
public class PopupTester extends JFrame {
JButton myButton = new JButton("Click Me");
JLabel myComponent = new JLabel("Howdy");
Popup popup = null;
public PopupTester() {
setLayout(new FlowLayout());
setSize(300, 100);
add(myButton);
add(new JButton("No Click Me"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myComponent.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
if (popup != null) {
popup.hide();
}
}
});
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (popup != null) {
popup.hide();
popup = null;
}
Point location = myButton.getLocationOnScreen();
int y = (int) (location.getY() + myButton.getHeight());
int x = (int) location.getX();
popup = PopupFactory.getSharedInstance().getPopup(PopupTester.this, myComponent, x, y);
popup.show();
myComponent.requestFocus();
}
});
}
public static void main(String[] args) {
PopupTester popupTester = new PopupTester();
popupTester.setVisible(true);
}
}

Categories