I have about 3 frames in my java swing application. What it the correct way how to handle with these frames? I mean some pattern or something else. Now I have always one class which represent frame and one class for panel which is main in this frame. Now I have defined frames as static variable and when I wanna hide them I call
classname.frameName.setVisible(false);
is this the correct solution?
Besides the (excellent) suggestions of a CardLayout or JFrame with multiple JDialog instances, here are some other strategies which might work singly or in combination, to collapse a variety of content panes into a single frame.
JDesktopPane/JInternalFames (Tut.).
JSplitPane (Tut.).
JTabbedPane (Tut.).
JLayeredPane, if you're feeling brave (Tut.).
JToolBar - floatable if needed (Tut.).
Different constraints of a JPanel in a nested layout.
There are probably more..
Of course, as Adamski pointed out, there are some further quirks to consider..
What if each frame has JMenuBars or JMenus?
Possibly combine them as sub-menus.
Take a look at a decent docking framework such as MyDoggy. This allows you to display all three components in a single JFrame, but is very flexible in that you can view the data side-by-side, resize and maximise components.
This design seems flawed. Instead of having multiple containers, you should use an appropriate layout manager. In this case, I recommend using CardLayout. This way, you would have a single container with multiple exchangeable views.
Controlling frames through static references seems to be a very fragile solution. What if the reference is null? What if the frame isn't in a completed state when setVisible() is called on it?
It would probably be a better idea to separate this logic out into a separate class and either have the frames register themselves to it , or construct everything up front.
Related
I have read many different topics and information sources but I can't really understand the differences of containers in java I know JPanel, JFrame and Container are similar and should be used at different levels within the construction of a program and I know there is JWindow and acts relatively the same. I am fairly new to programming so I am just unfamiliar with the setup. Here is what I have found out/assumed about each one, please correct me if i'm wrong.
JPanel is for the integration between JFrame or a Container typically separating different sections for labels, buttons, sliders etc...
JFrame acts as a receiver of JPanels and can construct them based on instructions from the coder, JFrame can also take the same labels, buttons, sliders etc....
Container is the same as JFrame though I am assuming container is a parent of JFrame.
I'm unsure of JWindow I just found out about this one.
Both JPanel and JFrame are Containers, yet more specialized for specific purposes. And yes, you have to assemble them correctly so they act as your user interface.
But as Container does not do much on it's own you probably never put that into your UI directly. Use it as base class to construct more specialized other classes.
To move along get some simple example running, you could follow the Java Tutorials.
I was googling around for a while looking to imitate what our usual applications have in their designs: Multiple Frames (if I got that correctly)
I was wondering how can I achieve the same thing? I get the concept of (assuming I was able to accomplish making them) having layouts and resize managers inside my frame so that everything will still fit, but how can I add frames inside a jframe? how can I attach, detach, resize, turn them into tabbed frames?
anyone got a lead I can start reading about?
What you are looking for is called a cardLayout, and those are not Multiple frames inside a frame, they're basically multiple JPanels inside a JFrame.
When you use a cardLyout you can switch between panels inside your frame without needing to make another frame, this is very usefull and user-friendly compared to having multiple frames.
You could have a closer look at the Eclipse IDE source-code itself for example.
Find another starting point here http://www.vogella.com/tutorials/EclipsePlatformDevelopment/article.html
I am creating simple application in java - Eclipse - WindowBuilder Editor. JFrame`s contentPane has JGoodies FormLayout in which I have to place 3 or 4 Panels - depending on mode.
It is proper way to make if construction that decides if content pane will be divided 1x3 or 1x4(facilitation because between all I use relatedgaps and so on..)?
I am not sure if this is good approach but I do not know how can I do this in other way than if construction. It has to be practical and flexible approach - to handle resizing the window, et cetera..
Common approaches for dynamic layout include these:
Use revalidate(), and possibly repaint(), to layout a Container again after adding or removing components, as shown here.
Replace the layout and validate() the Container, as shown here.
Use CardLayout to replace one panel with another, as shown here and here.
I wanted to ask for a tip. How would you struct your application if you had a JFrame with many JPanels which have like ten JTextFields Labels, and an Edit Button.
Would you put all the code in the JFrame or would you extend from JPanel and include all of them in the JFrame?
What about the buttons? I want to use the MVC pattern, how would you handle the actionlisteners? Are each of them an own controller or would you use only one controller for all of them?
I think it's first of all a question of style and personal preference. It also depends on what your application should do and how.
If your JPanels all are very similar in a certain way it would probably make sense to extend from JPanel. For example if every panel had 10 Buttons where Button 1 does always action xyz() for its corresponding model-object (especially when this object is the same for all buttons of 1 JPanel) and Button 2 does abc() ...
If the JPanels are not strongly correlated in such a way I would place my code rather in the JFrame or in a third Object which sets the whole GUI up from outside.
It's similar with the actionlisteners. If you have very few actions I would probably go for just one controller-object for ease of use (not many files). If you have a lot of different actions I would group similar actions into one controller-object per group of actions.
That said it probably is best to start with a simple approach (YAGNI) where you do everything from the JFrame / third object and have one actionlistener and then refactor when you feel that splitting things up gives you a cleaner or more flexible design.
I've been staring at Oracle's JLayeredPane tutorials but they are laid out in a manner that is confusing to me and doesn't get at what I am trying to do.
I have an application that up to now has had no concept of layers. Everything is laid out in a single layer, inside a JFrame.
I now want to introduce a component that appears sporadically, as needed, in a certain location, overlaying existing components that stay there normally. Do I have to modify my existing application JFrame so that all its top-level contents (that is, the components that are directly added to the JFrame) are instead added to the JFrame's JLayeredPane?
Or what, exactly?
I'm looking for an easy way to adapt this gui to use layers with the minimum rework of the existing GUI.
Thanks in advance for any help here.
You may want to instead consider drawing your overlay element on the glass pane. That way you can leave the underlying structure completely as-is.