Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

Drawing on Tabs

Thread view: 
SkippyBoy - 02 Dec 2006 20:49 GMT
Hi all - I have an application where my main frame has a tabbed interface,
and each tab has a JPanel.

I would like to draw a line graph on the second tab (the first tab is where
data is entered) and a bar graph on the second tab.

The problem I have is how to draw on the second or 3rd tabs.

I googled and one solution was to subclass the JPanel and then override the
paintComponent() method. I tried but it doesn't seem to work. I am probably
just doing it wrong.

How would I go about defining/extending the JPanel class? or is there some
other container that would serve this purpose better?

Any help is appreciated.

(remove the +NOSPAM from my email address...)
Daniel Dyer - 02 Dec 2006 22:34 GMT
> Hi all - I have an application where my main frame has a tabbed  
> interface,
[quoted text clipped - 17 lines]
>
> Any help is appreciated.

Sub-classing JComponent directly is probably more appropriate.  Even  
easier is using a ready-made library such as JFreechart.

You might get better advice from comp.lang.java.gui (this reply is  
cross-posted with follow-ups set to comp.lang.java.gui only).

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Knute Johnson - 03 Dec 2006 00:05 GMT
> Hi all - I have an application where my main frame has a tabbed interface,
> and each tab has a JPanel.
[quoted text clipped - 14 lines]
>
> (remove the +NOSPAM from my email address...)

This is how I would do it.  You will need to make it look better but the
big pieces are all here.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class test {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                class ChartPanel extends JPanel {
                    private int value = 0;

                    public ChartPanel() {
                        setPreferredSize(new Dimension(200,150));
                    }

                    public void setValue(int v) {
                        value = v;
                    }

                    public void paintComponent(Graphics g) {
                        int w = getWidth();
                        int h = getHeight();
                        g.setColor(Color.WHITE);
                        g.fillRect(0,0,w,h);
                        g.setColor(Color.BLUE);
                        g.drawString("100%",0,10);
                        g.drawString("0%",0,h);
                        g.fillRect(w/3,h - (h * value / 100),
                         w/3,h * value / 100);
                    }
                }
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final ChartPanel cp = new ChartPanel();
                SpinnerNumberModel nm =
                 new SpinnerNumberModel(0,0,100,1);
                JSpinner sp = new JSpinner(nm);
                sp.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent ce) {
                        JSpinner sp = (JSpinner)ce.getSource();
                         cp.setValue(
                          ((Integer)sp.getValue()).intValue());
                    }
                });
                JTabbedPane tp = new JTabbedPane();
                tp.add(sp,"Changer");
                tp.add(cp,"Chart");

                f.add(tp);
                f.pack();
                f.setVisible(true);
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

SkippyBoy - 03 Dec 2006 17:51 GMT
<snip>

Thanks for the quick replies. I think I have it working now... It seems my
approach was wrong. Here is how I got it to work the way I needed.
1. Turned off the computer
2. Had <sufficient # of beers> to feel better about life.
3. Had a nice steak dinner.
4. Turned computer back on
5. It worked!

I guess what happened was I was running the wrong version or something. I
kept making changes, and I guess I saved them in the wrong place. Anyway -
once I rebooted and tried again - it just worked.

I think I will apply my new 5 step approach more often...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.