gaining a focus of JInternalFrame object - java

What happens when you minimize JInternalFrame object using built-in icon located in the corner (next to maximizing and closing ones)?
I have a JInternalFrame in JDesktopPane and my problem is following:
When I minimize one frame it is being minimized. When I try to re-open the frame via using JMenu it works for first time. When I do this procedure again, the frame is being minimized but not re-opened, but when I click manually on a iconified frame, it shows properly.
I tried to do combination of setSelected, moveToFront, setIcon, setVisible, activateFrame, or requestFocus, but still no effects.
I wonder what happens when you minimize JInternalFrame, what is being set, what should do to get focus of a particular frame, and why the procedure it works for the first time and not others?
thanks

Seems to work fine for me
try {
if (myFrame.isIcon()) {
myFrame.setIcon(false);
miDoShowHide.setText("Hide");
} else {
myFrame.setIcon(true);
miDoShowHide.setText("Show");
}
} catch (Exception exp) {
exp.printStackTrace();
}
I set up a menu item that would call this functionality. I tested by using the menu to min, restore, min, restore. I minimized the frame by the frame control and restored via the menu, minimized by the menu and restored by the frame control.
Also make sure that the JInternalFrame is set to Iconifiable (setIconifiable(true)) otherwise you will experience some strange results

Related

How to close the active JFrame

I am trying to find a method that can close the active JFrame.
I am unable to use frame.dispose();, as I am declaring the action listener in a toolbar class and the frames I want to close are not static and are declared at runtime.
I have tried using:
java.awt.Window win[] = java.awt.Window.getWindows();
for(int i=0;i<win.length;i++){
win[i].dispose();
}
and whilst this does work, in certain circumstances it will close more than one window even though only 1 window appears to be open, so frames will flash open and closed many times depending on what actions the user has made.
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I am hoping someone will know of a more simple and reliable way of closing the active frame in the mould of acitveframe.dispose(); - which I now is not a real solution!!
What happens if you try to get the Window ancestor of the source of the action event? i.e.,
#Override
public void actionPerformed(ActionEvent actionEvent) {
Component comp = (Component) actionEvent.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
This won't work if the source is not a Component or if it is not contained within the top level Window of interest.
Regarding:
For me to fully recreate my problem would involve posting a significant amount of code which would not be in line with MCVE principles.
I'll bet with a bit of effort you could create and post something that comes close.
I am hoping someone will know of a more simple and reliable way of closing the active frame
In your loop you can add:
if (window.isActive())
// do something
Or maybe a simpler approach is:
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
Also, assuming your active window is a JFrame, instead of using window.dispose(), I have used code like:
WindowEvent windowClosing = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
frame.dispatchEvent(windowClosing);
this will simulate the user clicking on the "Close" button which means that any WindowListener you added to the frame will also be executed. See Closing an Appplication for more information and ideas.
When you are declaring your JFrames, declre them as final if you cannot use static :
final JFrame f = new JFrame();
It would solve the problem.

Java JFrame popup window

So I'm fairly new to Java Swing, and I stumbled upon a certain difficulty. I have a main Frame running (main part of the application that is visible throughout the app's execution) which has a button that once clicked, invokes a popup Frame (window) to collect user's information, and that frame has some Components. The problem being is that I don't really know the right approach to invoking the popup window and the main window freezing the execution and waiting until OK, or cancel button is clicked on the popup. Once this happens the main window code collects the returned values from the popup and resumes. I tried using synchronization to accomplish this, however the popup components don't even load, just the JFrame and JPanel (white background) and the popup freezes up on the wait() condition. I know that there is a way of doing it with JDialog and others, my main concern however, is to discover why the popup frame doesn't load the components and freezes up before the wait() condition. (when I get rid of wait() everything loads properly).
//in Main window Class:
frame.setEnabled(false);
Test test = getNewTest(); //should freeze on wait() in popup window
frame.setEnabled(true);
//in Popup Window Class
public Test getNewTest() {
addPanel.setVisible(true);
addFrame.setVisible(true);
synchronized(flag) {
try {
flag.wait();
} catch (InterruptedException e) {}
}
addPanel.setVisible(false);
addFrame.setVisible(false);
if(success)
return new Test(testName, date);
else return null;
}
//When OK or Cancel button clicked appropriate handler sets
//success value and invokes flag.notify();
Get all that synchronized and wait stuff out of your code. All that will do is freeze the Swing event thread, rendering your application useless.
You don't want to use a second JFrame, since an application typically has only one main window or JFrame, not multiple.
You want instead to use a modal dialog such as a JOptionPane (which can hold complex GUI's), or a modal JDialog (which also can hold a complex GUI). Be sure to associate the modal dialog with the parent JFrame. The Swing GUI library will then freeze the main window until the dialog has been dealt with, and the code from the main GUI will resume from the place the dialog was made visible after the dialog is no longer visible.

How to make a resizable JDialog?

I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:
set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
make the showed dialog resizable.
My code looks like:
JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
panel,
"titel",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]);
I know that I could obtain the desired result building a JDialog this way:
JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);
The problem with the last solution is that I can't get the return value.
EDIT:
in response to #camickr observations:
Why do you need to set the preferred size? If you build the panel
properly is should be displayed at its preferred size.
I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?
Read the JOptionPane API. Search for "Direct Use". It shows you how to
directly access the dialog used by the option pane and you can
I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.
This hack using a HierarchyListener to get access to the JOptionPane's also works:
http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.
Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can
With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.
What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.
If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.

Detecting Focus on (frame + components) in swing

I have a small dialog frame that appears, and within this frame are a series of buttons and a textbox.
I need the frame to be able to detect when the user has put focus on something else on the screen (being: anything besides the frame and its components), so I can close down the frame.
Any advice on how to go about this? I've been trying at focus solutions for hours, to no solution!
Try using a WindowStateListener
The WindowEvent parameter it provides can tell you if the window has lost focus through the getNewState() method.
class MyFocusLostListener implements WindowStateListener {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == WindowEvent.WINDOW_LOST_FOCUS) {
e.getWindow().setVisible(false);
}
}
}
need the frame to be able to detect when the user has put focus on something else on the screen
Use a WindowListener and listen for windowDeactivated.
listen to property changes of the property "permanentFocusOwner" of the KeyboardFocusManager. On being notified, check if the new focusOwner is in the child hierarchy under the frame, if not - close the frame.
Edit: seeing the answers suggesting a Window/StateListener - they are better than mine for a top-level window :-) Listening to the keyboardFocusManager is a good approach for containers deeper down in the hierarchy, implemented f.i. in the CellEditorRemover of a JTable (to decide if a pending edit should be terminated)

JFrame disappear behind applet's browser window

My app is a JApplet which opens a JFrame. the applet has a listener and a button, so that if the frame goes behind another window (looses focus), the user can simply click the button to get it to come to the front. frame.toFront(); This works fine.
But initially (in the applet's public void init() {}),
after calling frame.setVisible(true); I call frame.toFront(); to to make sure it starts in front. However, the frame then immediately goes behind the browser. Pressing the button calls it back, though.
I have tried a running a separate thread which repeatedly calls frame.toFront(); But as soon as this stops, the frame goes behind the browser anyways. Only when the button is pressed does it come to the front, and stay in front. Also, having a loop or time continually holding it in front is not a good option, because the user may need or want to have it go behind on purpose.
This "bug" is not present on the Mac (which runs Java 1.5), but on Windows (running 1.6) - including IE, FF, Chrome, Safari, but not Opera (strangely).
Possible cause and fix?
Have you tried setAlwaysOnTop(true) on the frame? I'm not sure however, if this is allowed on frames or windows created from an applet.
The setAlwaysOnTop(true) solves one problem, but create another, namely that now there is no way for the user to actually send the window to the back.
My sollution is a hack:
In the WindowListener attached to the JFrame, place this code:
#Override
public void windowDeactivated(WindowEvent e)
{
if(firstToBack) //firstToBack is an bloolean instance variable initialized to true
{
final JFrame f = frame;
new Thread() { public void run() {
try { Thread.sleep(300); } catch(InterruptedException ie) {}
f.toFront();
}}.start();
firstToBack = false;
}
}
This basically starts a new Thread first time, which waits a little and then calls the JFrame to the front. It only executes once, so the frame doesn't keep coming to the front every time the user sends it to the back. The 300 milliseconds is an arbitrary amount of time and perhaps not even necessary.
Perhaps someone can tell me why this works, but when the same kind of thread was started fron the applet's init() method, the window went to the back anyways, after the thread ended?

Categories

Resources