• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

more tabbedpane - panel questions

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have a tabbed pane that has three panels on it. I am using these panels to plot some data. At first I wanted to place a "plot" button on this tabbed pane to invoke the plotting method but I didn't like the look of it.

So, now I was hoping to detect when my tabbed pane received focus and call a paint() method that would do the plotting.

The class that creates the three panels extends ComponentUI and I have a
"public void paint (Graphics g, JComponent j)" method in this class.

However, it never gets called.

Anyone have any ideas what I could be missing here?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what your problem is with the way you have things setup. But I'd like to offer a suggestion...

Extends JPanel for your 3 panels and override the paintComponent(Graphics g) method for your painting. What should happen is when you switch tabs the paintComponent method should be called automagically, thereby drawing your plots.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, the smoke seems to be clearing...

List you suggested, I extended JPanel and not ComponentUI



And this seem to get my Paint() method invoked.

This has created another, although much less daunting, problem.

The original three panels created in the constructor are no longer showing up.



It's not a big deal, because I will be doing all the actual work in my paint method anyway. I was just wondering why they wouldn't show up. It probably has something to do with my overridding the paint method.

Anyway, thanks for the input.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't tell from the code snipit you provided because I don't know what "this" is referring to.

You said you are overriding the paint method. Maybe you are calling it paint but what you mean is paintComponent? You need to override the paintComponent method of a JPanel, not paint.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is some code:

My class extends JPanel


So, in my constructor method "this" is referring to the panel. It's not needed, I know. It's there because the code is in a state of flux and at one point it was needed.

Here my constructor at the moment:


And here is my Paint() method.



Notice my comment. It was the only way I know to get my original three panels (from the contructo method) painted.

Intrestingly, I have also overridden the Paint( Graphics JComponent c) method (just for testing purposes) like this:



However, I have never seen this method invoked, and it still exists in my code at the moment.

Also, I am beginning to see another problem that you may be able to give me a hand with. If I have a lot of data to plot, I thought that I would simply resize my panels, plot my data on them, and use the scrollbars to view the plot.

I can't seem to get the scrollbars to appear. I have tried panel1.setSize() in my paint method but this did not cause the scrolls bars to appear.

Any suggestions?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to say that I am a bit confused as to what you are wanting to do now. I haven't run across a situation where I need to put 3 JPanels on a JPanel that I am overriding paint. But at any rate, here are my thoughts:

1. Override paintComponent(Graphics g), not paint(Graphics g). I don't remember why, I just know that this is the rule. It has something to do with when and how it gets called, you don't want to change when paint gets called. paintComponent will issue the command for you.

2. In paintComponent the first line should be super.paintComponent(g). This is not a hack. This is to clear the drawing area everytime before you draw on the area.

Here is the code I used and I can see all 3 JPanels on my custom panel (which still confuses me as to why you are doing this. )



 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help.

I'll modify my paint method, but not until tomorrow.

It's too close to miller time to do anything else today.

Anyway, my application is monitoring three servers that we are using on
our network. This part of the application will show simple line graphs of the
response times of the those three servers.

Basically, my requirement is that I need to show the three line graphs at
the same time. I suppose it could be drawn on one panel.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jefff willis:
I suppose it could be drawn on one panel.



No no no. What you need is a custom JPanel that you override the paintComponent method in and then place 3 of those on the Container (JFrame, JPanel, Window, whatever).

The way you were doing it is putting 3 JPanels on 1 custom JPanel. It should be the other way around. That's what was confusing me about your app. I assumed you wanted it the way you described. But the way you were coding, it didn't make sense.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, check out this complete class:



There are a few issues here.

1. this class's paintComponent is never called.
2. this panel is sufficiently large at creation time to display
about three hours of "server monitor" data. However if this application
were to monitor, say twenty four hours of "server monitor" data, then I
planned on simply calling this.reshape() inside the paint() method to give me the necessary space to draw my graph. However, doing this does not cause my scrollbars to appear. It does resize my panel, but that is not what I need.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason paintComponent is never called is because you are overriding paint as well.



I would guess that the reason your scrollbars never appear and your panel just gets bigger is because of the layoutmanager you are using in combination with the way JPanels work within JScrollPanes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic