Java Netbeans: How to layer panels on top of each other? - java

Please bear with me as I've just started using NetBeans for the first time! Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time. It'll start with one panel, and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I've tried looking into utilizing LayeredPanes since that's what I've come after hours of researching this only.. I don't understand how to do it! I think using different panels would be much easier than using different frames, so that's why I'm just going to stick with layering panels.
If anyone could explain LayeredPanes, I'd be very grateful! I'm not sure my coding will help here, but if anyone needs it I'll put it up.

This tutorial should point you in the right direction, however, if you want to have items positioned over each other, you might also want to take a look at the CardLayout:
The CardLayout class manages two or more components (usually JPanel
instances) that share the same display space.

Reading your question I dont think that what you need is a LayeredPane,
Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time.
Tell exactly what you want to achieve, I mean on what you are working on.
...and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I'd like to suggest a JDialog , show dialogs depending on the user inputs. To make sure if this is what you need, you want to provide us with more information. :)

Related

How to "merge" two JFrames together?

I am desinging a GUI in Java using Swing.
I have two JFrames in my class which I would like to merge into one window, so that when the program is launched, only one window appears. However, my specific needs impose a specific layout constraint: I need to have half of my GUI displaying two components (a JTextArea component, and a JTextPane component), and the other half displaying the third one (an Image).
I can get them to all work singly, but if I try to combine them into a "super-frame" to hold them all, it doesn't work.
I am wondering is there any other way to achieve this effect.
Any help is much appreciated, thank you!
I think it is not possible to merge JFrames and you can instead use JPanels

JPanel not updating, no matter what i try

Im making a small program that shows teams/ players/ then their twitter feeds, tweets, etc. My problem seems to be unrelated to that though, it just my jPanel isn't updating. I got pretty far in the project, and i keep throwing random code segments in my method to make it so once i click something, the "left" jpanel either is replaced with the jbuttons, or the jbuttons are put into it, ive tried both. Heres the code segment ive been trying to get to work, which it does, just not as intended.
public void mousePressed(MouseEvent e) {
System.out.println("You clicked on " + ap.getTeams() [addPlayers.OPTIC].getTeamName());
for(int i = 0; i<4; i++){
//JPanel temp = ap.makePanel(ap.getTeams()[ap.OPTIC].getPlayers().get(i).getTwitterScreenName());
//temp.setBounds(0,(i*125), 450,125);
//left.add(temp);
JButton b = new JButton("Test");
b.setBounds(30,30,30,30);
left.add(b);
left.revalidate();
add(left);
add(b);
right.add(b);
left.setVisible(false);
left.setVisible(true);
System.out.println(i);
}
}
}
the commented out code is my main code, but i am just using jbuttons to test. As you can see, i just keep adding random bits, hoping something will work. Ive also tried invoke later, but sadly, that didn't work either.Not sure why the jpanels not updating, but any feedback would be great, thanks.
Avoid null layout and setBounds since this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
The best way to swap JPanels or any component is to use a CardLayout.
If still stuck, then post a minimal example program. It's the best way for us to actually see and experience your problem.
Don't add a component to more than one container like you're doing with your b JButton variable. You're in fact adding it to three containers.
You seem to be trying to add four JButtons to containers, giving them all the same bounds, one exactly on top of the other?? Sorry, but the more I look at your code, the more screwy it becomes. Again, stop this nonsense, learn about the layout managers at the Swing tutorials, and use them.
"and i keep throwing random code segments in my method to make it so ..." -- throwing things at the wall to see what sticks is not a good heuristic for creating a program. Start with the knowledge base -- here the Swing tutorials and Java API, then plan your program structure, and then create your code.
Edit
You ask:
..... All im asking is whats the problem with the whole thing not updateing?
If you don't use a CardLayout and change components manually, then you would need to call revalidate() and repaint() on the container after the change, and the container should update with its new components. If this does not fix the problem, then likely you have an issue in code not shown us, and you will again then want to create and post a minimal example program. Again it's the best way for us to actually see and experience your problem.

Java gui that allows user to move fields

I'm trying to create a java gui, currently I'm playing around in the netbeans IDE using their gui creator, but I've also been reading a book about Swing and trying to learn it that way as well. Im hoping someone can help me with a problem I'm having. I'm attempting to allow users to have a "pool" of fields they can choose from (for instance a title, a paragraph, a text fiend, buttons, etc) and be able to move items from the pool into another potion of the window which would let them create their own layout. At this point I don't need these fields to DO anything, but I do want the user to be able to move them around and create their own layout. Is there any way to do this?
I think that to do this, you'd need to use a null layout on the container that would hold the movable components, and you'd have to give the components MouseListeners and MouseMotionListeners (conveniently combined into MouseAdapters) that are active when the program is in the set-up state, but then inactive when the program's components have been all set.
You could use the DragLayout from the tips4java website.

Creating a Grid in Java

As a way of learning Java, I'm writing this little application for grade schoolers to practice basic math. The idea is that the kid does any number of math problems, as long as they are in the app, it just continues to throw problems at them until they click a 'Done' button. When they decide to be done, I want a new JFrame to come up that will show them all of the problems they attempted, along with their answer, and whether they got the problem right or wrong.
The advice that I am looking for is what is the best way for me present these results. I looked into the GridLayout and the GroupLayout, but I don't think that these are exactly right. I did something similar in VBA for Excel, and there I just ran a for loop with one iteration for every problem they attempted. Each iteration would add a row of labels to the frame with the elements of the problem displayed in the various labels. I tried this in Java, but I'm not even able to get the labels to even display.
So before I get all specific and start posting my code, I want to ask a bigger question, which is "what is the best method to create a view like this?" Often, I go off in one direction only to waste time before somebody suggests a totally different (and better) approach.
Thanks!
(edit: here's an image of how I did this in Excel. I'm trying to repeat basically the same thing in Java)
One simple way to make that design would be to use a mix of components. You could have a bunch of JLabels and JPanels stacked in a vertical FlowLayout. The grid you have described would be best designed in a JTable, something like the below:
If you like tables like Excel then, Java provides JTable class to create tables, if you want.
Tutorial : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

Navigate between windows in a simple quiz app

I have to make a simple quiz application in JAVA and I have no idea on how to navigate between windows, for example, how to go to the next question or how to go back. I have a single question per window. Can you give me an idea on how to navigate? Should I use repaint() or a CardLayout or something else?
Thanks!
Yes CardLayout can be a good idea for this purpose, if you need some example take a look at the oracle how to.
If the quiz is multiple choice, you have other options: change the data that the questions (probably JRadioButtons) displays. Otherwise, I vote for a CardLayout. Also, if you want the user to be able to easily go back to other questions, you could display them all as JPanels in a GridLayout using JPanel held by a JScrollPane.

Categories

Resources