because I am displaying multiple images in my java programm, each image into a new jframe.
I need to identify the selected jframe in oder to make changes on the displayed image and show it in the same jframe.
So, How could I recognize the last selected jframe ?
I suggest you to use some other layout such as CardLayout to share same display space for multiple panel instead of using multiple JFrame.
See The Use of Multiple JFrames, Good/Bad Practice?
If you want to stick with current approach then create a global static reference of type JFrame to keep the reference of selected JFrame.
Use FocusListener to keep track of the selected JFarme
So, How could I recognize the last selected jframe ?
Keep the references of all opened JFrame and iterate all to check for JFrame#isFocused() or JFrame#isActive()
Related
i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes
I'm trying to add multiple images to JLabel. The problem is that it is impossible to add more than one image, and I don't know how many images I will need in advance.
How can I overcome the problem? mabye not JLabel..
Thanks
1) Show us what you've done so far :)
2) Short answer is that you can't. JLabels and JButtons can only have one image associated. So you can either have multiple dynamically created JLabels or JPanels.
3) I don't understand why you want the image in a JLabel.
SOLUTION
Create a JPanel called container and give it a grid layout. Dynamically create more JPanels (pnl1, pnl2, etc) and add them to container. Each image should be added to pnl1, pnl2, etc.
Create a class image panel that extends jpanel as your container for each image.
If you don't know during compile time, I assume you know during runtime? Which means you just keep creating new panels until you run out of images.
In your Image Panel class you'll also want to assign an id for each image panel in the event that you want to be able to utilize or change the content/image later. So you'll find panels by id and not by variable name because those will all be dynamic and you won't have them.
How do I dynamically update a JFrame and replace it with another JFrame without seeing the action visually,
I usually use [this.dispose] to close the current window and call a new JFrame.
Is there a better way to go about the problem.?
Don't create a new JFrame.
Instead you update the components on the current frame. One common way to do this is to use a Card Layout.
I can replace any given JFrame icon in Swing with a call to JFrame.setIconImage(). However, I have an existing application that pops up lots of JOptionPanes and other JFrames/dialogs, and rather than track them all down I'm wondering if there is a way to switch these over to my custom icon all in one place? Also, in some instances (ProgressMonitor, for example) I don't have access to the actual JFrame to fiddle with.
You can store the JFrames you created on a List. Then, you can use a loop to get every frame on this list, and apply the icon.
I have a question, im making a application where you can first select a name from a jList in a jFrame. After you've selected a name and pressed the proceed button, a second jFrame pops up.
On this jFrame there are a couple of textfield which i want to automatically fill up with information about the selected name selected in the first jFrame.
And this information which is automatically filled in is different depending on the chosen name.
I already have the first jFrame with 4 names in a arraylist but now im stuck on the second part where the chosen name is transfered to the second frame along with the extra information which must be done automatically
I hope someone can help me, i would really appreciate it.
THank you.
Well, heres how I would do it:
Exit the current jframe
Pass the data in the arrayList to a function that creates a new JFrame
Fill the new JFrame
Creating a a Swing application with multiple JFrames can be problematic.
A better and simpler approach would be to use CardLayout and to have panels within a single frame. Your ArrayList could be a member variable in the main JFrame class and could be easily be updated with data as you progress.
See: How to Use CardLayout