I've been searching the web for ages and found a lot of people with similar problems; however, none of them have the solution to this (unless I"m missing something).
Here's the problem:
I'm trying to make a basic graph program that plots data. There are going to be different types of graphs and I want the user to be able to switch between them using radio buttons. There is a JPanel that marks the position of the Graph in a JFrame (In netbeans), then, at runtime, I add a series of JPanels to that panel which override the PaintComponent method. The first JPanel draws the axis, lines and labels; the other panels are the actual graphs and use lines to plot the data. All the 'graph panels' are added serially using the 'panel.add()' method and user can switch between them using the radio buttons which call the 'setVisible(false)' method on all the panels, except the one that's been selected.
All this works perfectly - the axis and lines are painted and the 'graph' panels sit on top and the user can switch between them. However, I would like to add a feature where the user can 'underlay' graphs by adjusting the opacity, so they can compare different results. I thought this would be a simple affair by calling the 'setOpaque(false)' method of all the panels and setting the alpha value of the line colour to show the lines below, but it doesn't work - It will only display one panel at a time. If I set the top panel's 'setVisible()' method to false, I can see the panel beneath, but the moment it's visible its hidden again as if it's opaque.
I've done a bit of experimenting and found that if I add panels directly to a JFrame, I can achieve transparency throughout. It doesn't seem to like it when I add the graph panels to an existing JPanel, though?
I'm very confused and nothing in any of the books I own have the answer.
The axis lines and the red graph lines are two different classes that extend JPanel and are added to the base JPanel using the 'add()' method. Other Graph panels can be added and I can switch between them by calling the setVisible(x) method to display/hide different panels. However, I can't display two panels at the same time on top of the panel with the Axis lines and labels, etc.
Hope this helps!
Joined: Jun 13, 2009
Hope this helps!
You haven't addressed any of my concerns:
1) layout managers are generally 2 dimensional unless you are using a custom layout. Panels just don't paint on top of one another unless you have a hierarchy of panels, which means you keep adding child panels to a different parent panel. In your code is looks like you are adding 3 child panels to the same parent so I don't see how you expect them to paint on top of one another.
2) I don't see a SSCCE anywhere. How does the code you posted compile? I don't care about your application. Your question is about the concept of painting panels on top of one another. Try doing something simple without all you comlex charting code to see if you can get that to work first.
Joined: May 07, 2010
As I mentioned in the last message - the code is pseudo code, it has been written to give you a rough idea of what I'm trying to do. It is NOT meant to compile. The pseudo code IS the SSCCE; if I posted the actual code for the program it would take several pages.
Thanks for the info about layout managers; however, I am adding three panels to the parent (without a layout manager) and I CAN see each one, providing I make the others invisible. Logic would suggest that because of this behaviour, the panels ARE layered on top of each other and making them non-opaque would allow one to see the panels underneath. Easy mistake, I think.
Thanks for your attempt at answering my question. However, I think your curt tone is unnecessary, especially for a forum that is meant to be a 'FRIENDLY place for programming greenhorns'.
Joined: Jun 13, 2009
if I posted the actual code for the program it would take several pages.
Your problem is display multiple panels on top of one another. How hard is it to create a simple demo with 3 panels and a label added to each panel. Then you attempt to display all three at one time. It should be about 20 lines of code. Once you get that simple demo working you use the knowledged gained to solve your real problem. Learn to walk before you run!
Joined: May 07, 2010
I see you still haven't read the SSCCE link.
The pseudo code is practically compilable, apart from condition in a loop. The code is very basic (as is the problem) and explains perfectly what I'm trying to do, so I really don't see why you are throwing your rattle out of your pram over it. You obviously know what I meant, you're just being pedantic for the sake of it.
I don't care about your application!!!
Yes, you've made that abundantly clear. Which makes me wonder... why do you even bother responding to peoples questions? I don't think someone with zero patience, or understanding for beginners, belongs on a forum designed for 'greenhorns'.
Richard, could you take a deep breath? You've got yourself into a position which happens quite often: you've got a complex bit of code and you can't get some feature to work.
I've found when that happens to me, what I have to do is to step back and start fresh. I write a small program which does nothing but the feature I'm trying to get to work. The small program is easier to work with because it doesn't have all the cruft of the big program. And when I get the small program working, I implement the code from it into the big program.
I know Rob Camick comes across as brusque and impatient, but that's what he is trying to say. Put the big program aside, we don't want to see that, and you can understand why. What we DO want to see is the small program which displays the problem you're having. The pseudo-code is sort of okay, but posting pseudo-code makes the forum helper have to do the work of making it into working Java code. I really think you should do that work.
I hope this helps; if you disagree with any of it then don't hesitate to say so and tell us why.
Joined: Jun 13, 2009
However, I think your curt tone is unnecessary
As I stated before I've tried to help but you aren't providing the neccesary information needed to help. You have been asked multiple times to post a SSCCE. Not only in this prosting, but also in a previous posting: http://www.coderanch.com/t/501589/GUI/java/Confused-over-InputMap. Yes, I get frustrated when people ask for help but make no effort to listen to the suggestions given.
The pseudo code is practically compilable,
Practically compileable doesn't help us.
You are not the only person in this forum or other forums that need help. Many of us here spend time on multiple forums. We help those who actually want help. We do not have time to guess what you are doing and fill in the code. The code you posted does in no way demonstrate the problem. Your code for the GraphAxis and Graph classes does nothing. What is the point of painting a gray background? Even if the multiple panels did paint properly on the top of one another there would be nothing to see so you couldn't tell if the code was working or not! You didn't even take the time to implement some simple logic like, for example, using the drawString(...) method.
why do you even bother responding to peoples questions?
I have responded to 10's of thousands of questions over the years. I have even helped you in the past. I think I have a good idea of what information is required to solve a problem. I always respond to a question on the assumption that the person asking the question wants an answer and is willing to provide the information necessary to solve the problem. Most people post the SSCCE right away and get their answer right away.
You obviously know what I meant
No I don't know what you meant, that is why I asked for the SSCCE.
I don't think someone with zero patience, or understanding for beginners, belongs on a forum designed for 'greenhorns'.
I have an incredible amount of patience. I have responded multiple times each time trying to pry more information out of you based on what I think is required to solve the problem. In fact, I'm the only person who has had the patience to attempt to solve your problem!
That is why I continue to suggest writing a SSCCE. Beginners are always in a hurry. They write a 200 line program and then when something doesn't work they have no idea how to debug it. The SSCCE is designed for you to step back and solve smaller problems.
I am adding three panels to the parent (without a layout manager)
No where in the pseudo code that you posted to you show code that indicates you are not using a layout manager. JPanels by default use a layout manager. Maybe this is the problem. I don't know. I don't have all the pieces to the puzzle and I don't have time to waste guessing, which is why a SSCCE is necessary.
To bring this back to the topic at hand: Richard, your three panels are not on top of each other; they're next to each other. The default layout for a JPanel is FlowLayout, which lays its children out in a row, and (unless your real code is significantly different from your sample), it's a FlowLayout that's managing your multiple child panels.
The only LayoutManager I know of which explicitly stacks children on top of one another is CardLayout, but it's I don't know whether this technique would work with CardLayout or not. I'm afraid I have to add my voice to the chorus that's suggesting you create one or more small, very simple toy applications with which to experiment and see if you can get the principle working.
If I understand your requirement clearly (I am not really sure, the SSCCE would have helped. I am going by the textual description given), you want to superimpose three panels, like transparencies. If yes, check out OverlayLayout.
In pseudo code
1) Set the layout of the base panel to OverlayLayout 2) Create your panels with custom painting. Don't forget to set them as transparant.
3) Add these panels to the base panel. Because of the OverlaLayout, they are superimposed.
4) Add the base panel to the content pane
Apologies to all for not posting the SSCCE. I hope everyone appreciates that I was just trying to express what I was trying to do as efficiently as possible, but I understand the need for a compilable version. Next time I'll make sure I'll post abstracted, compilable code in accordance with the SSCCE.
Rob: I appreciate you're trying to help. However, un-polite and impatient people grind my gears as much as newbies who don't post SSCCE's grind yours. If you can't be patient with a newbie then I would prefer you didn't respond to my posts.
Paul: Thanks, that's the kind of polite response I was hoping for.
Ernest and Maneesh: Your suggestions were very helpful, thank-you. I have yet to look into OverlayLayout but I will this week.
I have managed to achieve the desired functionality using GridBagLayout. It's not perfect, but the code is below for anyone who's trying to achieve the same thing.
Joined: Jun 13, 2009
If you can't be patient with a newbie then I would prefer you didn't respond to my posts.
If you look at my very first reply, based on the vagueness of your reply, I suggested the problem was with the layout of your components and prompted for more information. Every time I replied I expanded on what I thought the problem was and explained to you how providing a SSCCE would help in solving the problem. My suggestions to use a simple panel with a simple drawString enabled you to test your SSCCE.
Don't you dare accuse me of being un-polite and impatient. I provided you with a simple direct answer in you previous posting (I even suggested a SSCCE would be needed if you had further questions). My response here has been no different I tried to provide a suggestion but stated I needed more information. You are the one who has been stubborn and would not listen to the suggestions given. Now that you have ended up creating a SSCCE your problem has been solved, so my approach has been proven to be effective.
You thank Maneesh for his suggestions, yet you still haven't even read up on the OverlayLayout (which should be the better solution since this layout manager was designed for this purpose). Another example of you not listening to suggestions.
You are the one asking for help. The forum is not about you. We make every effort to privide reasonable answers but we also expect a resonable amount of effert from you. If you can't take the time to listen the the suggestions given and try to implement the suggestions, then please don't continue to post questions on the forum.