How to get active (having focus) frame (JInternalFrame) that is inside JDesktopPane? I need it for my MDI notepad (not that anybody would use that, just a training project). Looking at api, I see only functions to get all JInternalFrames, not active one.
Use JDekstopPane.getSelectedFrame() method (From doc: currently active JInternalFrame in this JDesktopPane, or null if no JInternalFrame is currently active.) or JDesktopPane.getAllFrames() to get list of all JInternalFrames currently displayed in the desktop and check isSelected() method.
Make a List<JInternalFrame> and check isSelected() as you iterate though it.
Addendum: See also this example that uses Action to select an internal frame from a menu.
Have you looked at the Java tutorial titled How to Use Internal Frames? In your code you need an InternalFrameListener (API) (Tutorial) and listen to activate/deactivate events. Activated means the internal frame was brought to the top; deactivated means it's no longer on top. Since JDesktopPane extends JLayeredPane you can also set the z-order of components added to it.
Don't iterate over all the panes - use events.
If for some reason you prefer to poll your UI rather than use an event-driven approach you can call getSelectedFrame which returns the active JInternalFrame. I'm not sure why no one else mentioned it.
Related
I have a general question that is Java related.
I am writing an application that has a GUI menu. I am trying to change one part of the GUI menu based on the selection of a Radio button.
Do I need to:
Redraw the whole window or just update that part with:
setVisible(true)?
If I just use the statement from #1 above .. the GUI is fine -- until I move the mouse over it and then I see the previous button choice. What am I doing wrong?
Swing components have a repaint(), revalidate(), and doLayout() method. One of those should probably be able to redraw whichever pieces you want. However, doLayout is not something that you should be taking responsibility for, that's the layout engines responsibility.
You may also want to check out this post, the first response has a pretty good explanation, and links to an article with more detail.
In terms of the second part of your question, I'm not sure, but we may need to see some code to get an idea. Is the 'replaced area' actually being removed from the view?
..in my application the user select which device platform type they want top test (that choice is a set of two radio buttons on the left). When the user selects either Android or iOS, the center grouping of check boxes changes to reflect a group of android devices they can test or a group of iOS devices that they can test.
Put a panel in the 'center grouping'.
Use a CardLayout for the panel.
Add both iOS & Android controls to the panel with the card layout.
Flip between them as needed.
Call revalidate() on the top level component.
So when I start up Netbeans, they create a little panel on the desktop for showing the progress of loading. I'm pretty sure Microsoft Office 2010 uses this too. I was curious how to make one of those in java?
I looked through the API and saw JDesktopPane. But I don't think that's what I'm looking for unless you can take that and put in on the actual desktop, but I'm unsure. Thanks in advance!
you can actually do it using JPanel.. you don't need to do anything else..no need of desktop pane
all you need to do is design a JPanel and put a progress bar inside it that will link to a process and show how much it has been completed.
JPanel doesn't have normal frame functionality like minimize, Close etc and will act exactly as you are trying to make up.
Update : Just tried doing what you wanted.
You need to start working on JFrame. and set its decoration to false.
in Netbeans, you can just go to Frame properties and set Undecorated to true
or inside code you can just write setUndecorated(true);
then you have to design your frame, put a progress bar inside it, link it to a function, set its onTop value to true (which means it will always be on top) and set its position to center of screen. done!! you are ready with your window!!
I have developed my Java code in Netbeans, and now I want to develop the GUI for my application.
The application communicates with a server, so it's going to have a login frame for sure. After that there will be a main frame. From the main frame the user can choose where to go and as you can understand there will be a lot of frames.
I have already developed a version of the application where there are a lot of frames and using the "setVisible()", but I want something better looking. I want a stable frame and inside it, changing the panels or something similar.
How would I do this?
You might use JInternalFrames if you like them, or simply use a main panel with a CardLayout, and display the appropriate card depending on the clicked menu item, or the selected JTree node (as it's done in Windows Explorer and similar applications).
Use the Swing tutorial to get you started.
You can, at any time, make any Container object a JFrame's ContentPane. You can also add and remove Containers from any other Container. If you want a user to be able to jump to any of a dozen panels at any time, CardLayout, as suggested in another answer, is easily the best route. If, however, you intend to lead the user along a somewhat controlled path, you can start with a login JPanel. When that's done, you can create the next panel (a JPanel or something else), add it, and dispose of the first one. And so on until the user exits.
If the transition from one panel to another affects nothing else in the program besides the two panels and the parent Container (JFrame or descendant), this is probably the way to go. If a bunch of other places in the program need to know about the change, you'll want a more centralized mechanism, maybe using CardLayout.
In my program I have two JFrame instances. When I click next button I want to show next frame and hide current frame. So I use this.setVisible(false) and new Next().setVisible(true). But in Next window if I click back button I want to set previous frame to be visible again and next frame must be ended (which means it must be exited).
Is there any special method(s) to do this? How can I do it?
Consider using CardLayout instead of hunting for how many JFrames there are. Then..
only one JFrame would be needed
any of Next/Back Actions will be only switching between cards
There are lots of examples in this forum - e.g. as shown here.
That is an odd & quirky GUI. I suggest instead to run a JFrame for the main GUI, and when the user wants to search, pop a JOptionPane (or modal JDialog) to accept the details to search for. This will not have the effect described above, but will follow the 'path of least surprise' for the end user.
If you want to destroy a JFrame releasing all associated resources you shold call dispose() method on it.
You may place your JFrames on a list data structure and keep a reference to current position according to the window you are displaying. In that way it will be easy to move to next and previous. But note that each frame added to the list will use memory and will have its state as you placed it in to the list.
If you are trying to create a wizard like UI, you should look up Sun(oracle)tutorial here.
create the instance of your main window in next() window.. and use same method which you chosed befoe to hide your main window, for example if your main window is named as gui then what we have to do is.
gui obj = new gui();
and if you click on back button now than do these also
this.setVisibility(false);
obj.setVisibility(true);
that's all you need.
good luck.
I am trying to implement a search results popup list similar to the style found here:
http://www.inquisitorx.com/
(I'm not trying to implement a Google search, I'm just using this as a rough example of the style I'm working on.)
In any event, I am implementing this by using a JList contained within a JPopupMenu which is popped up underneath a JTextField.
When a user enters search terms, the list changes to reflect different matching results. I then call pack on the JPopupMenu to resize it.
This works, however, it creates a slight flicker effect since it is actually hiding the popup and showing a popup. (See the private method getPopup in JPopupMenu where it explicitly does this.)
Is there any way to just get it to just resize itself (aside from using a JWindow)?
to me work :
popup.pack();
popup.validate();
this work in linux with java version "1.6.0_34". I don't know is that work on windows.
The setSize() method didn't work in my case. The popup.pack() approach did work, but also resulted in a flicker. The best solution that I have found was the one found here. In short:
Window window = SwingUtilities.getWindowAncestor(popupMenu);
window.pack();
window.validate();
Have you tried setSize()? It looks like that gets handled in JComponent and might avoid the repaint issues.
I think the issue that getPopup is addressing is what to do when the dimensions of the popup will not fit within the window. When that happens, it drops back from a lightweight component to a heavyweight and that definitely requires a hide and show. So, I think if you can guarantee your popup won't extend beyond the window the setSize might do the trick.