Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-04-2008, 03:57 AM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
.add to a JPanel
I am having trouble programmatically adding (panel.add) objects to a JPanel in NetBeans 6.1. The problem surfaced attempting to add JFreeChart to a JPanel. To isolate the problem I created a Frame with 3 panels (header, left, main). In the left panel I added several buttons.
The first button creates the chart and displays it in a new Frame (ChartFrame) separate from my defined Frame. This works fine, so we know the chart is created.
The second button .add-s a Label to the jPanelMain. This also works fine (although attempting to .add a JLabel does NOT work.) So, the event logic apparently is capable of adding to the panel.
The 3rd button creates the chart and attempts to .add the chart to the jPanelMain. This does NOT work. The chart does not display in the panel, nor does it display in a separate Frame.
Here are a few (of many) things I’ve tried:
1. Attempting to move the ChartFrame to the panel does not work (not really expected to work.)
2. Adding an AWT Panel – The chart does not display. It does not matter if the panel is predefined or added (.add) in the program.

To test if it was possible to add other objects to the panel I tried adding a Button and a CheckBox; neither was added. The only object successfully added is a Label. The panel.removeAll() also works.

Summary, I am having trouble .add-ing objects to a JPanel (or Panel) within NetBeans 6.1. There should be a simple solution. Your help understanding the solution is appreciated.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2008, 02:52 AM
djc djc is offline
Member
 
Join Date: Jul 2008
Posts: 10
djc is on a distinguished road
Oh my
I barely noticed this post. This is exactly what I'm having issues with. How were you able to get the second button to add the label to the panel if you weren't .add-ing it?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-18-2008, 11:01 AM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
There is a jfreechart jar available are you using that?
if so, i dont think you have to struggle so much,you just have to pass some values to it rite?
its just importing "import org.jfree.chart.JFreeChart;" like this
Code:
JFreeChart chart = ChartFactory.createPieChart( "Memory Information(in %)", dataset, true, true, false);
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-18-2008, 07:48 PM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
.Adding to JPanel
Quote:
Originally Posted by djc View Post
I barely noticed this post. This is exactly what I'm having issues with. How were you able to get the second button to add the label to the panel if you weren't .add-ing it?
djc,

Adding a Label works fine, but adding a JLabel does not. The following code works fine, but change it to JLabel testlabel = new JLabel("test label"); and nothing appears.

private void labelButtonActionPerformed(java.awt.event.ActionEv ent evt) {
// TODO add your handling code here:

Label testlabel = new Label("test label");
jPanelMain.add(testlabel);
return;
}

I don't understand why these objects are treated differently.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-18-2008, 08:02 PM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
Creating a chart is, apparently, separate from DISPLAYING a chart. The following code creates a chart and displays it in a New Window. The problem is getting it to appear in a particular programmer defined panel.

JFreeChart has a ChartPanel function that creates a chart within a panel. The JFreeChart demo program uses this function to create a window with multiple panels. I've tried a number of different ways to add the ChartPanel to an existing panel without success.

private void chartWindowActionPerformed(java.awt.event.ActionEv ent evt) {
// TODO add your handling code here:
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Category 1", 43.2);
data.setValue("Category 2", 27.9);
data.setValue("Category 3", 79.5);
// create a chart...
JFreeChart chart = ChartFactory.createPieChart(
"Sample Pie Chart",
data,
true, // legend?
true, // tooltips?
false // URLs?
);

ChartFrame frame = new ChartFrame("First", chart);
frame.pack();
frame.setVisible(true);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-19-2008, 01:27 AM
djc djc is offline
Member
 
Join Date: Jul 2008
Posts: 10
djc is on a distinguished road
harrier,

What I've found is that you can manually add components to containers by overriding their layouts. You can override their layouts by creating your own layout object, and then container.setLayout(yourLayout).

Documentation on layouts can be found at: ://java.sun.com/javase/6/docs/api/ under the javax.swing package and GroupLayout class. I tried creating what you wanted in a JPanel, but I guess JFreeChart is not a standard java class. I'll have to download those libraries sometime. Nevertheless, this is the code that I came up with:

DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Category 1", 43.2);
data.setValue("Category 2", 27.9);
data.setValue("Category 3", 79.5);
// create a chart...
JFreeChart chart = ChartFactory.createPieChart(
"Sample Pie Chart",
data,
true, // legend?
true, // tooltips?
false // URLs?
);

ChartFrame frame = new ChartFrame("First", chart);
frame.pack();
frame.setVisible(true);

GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setAutoCreateContainerGaps(true);
mainPanelLayout.setAutoCreateGaps(true);
GroupLayout.SequentialGroup hGroup = mainPanelLayout.createSequentialGroup();
hGroup.addComponent(frame);
mainPanelLayout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = mainPanelLayout.createSequentialGroup();
vGroup.addComponent(frame);
mainPanelLayout.setVerticalGroup(vGroup);

Realize that you can set that layout to whatever panel you want, not just "mainPanel". So if you have a jPanel1 or something, you can just change mainPanel to jPanel1. Let me know if this works.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-21-2008, 07:30 PM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
Thank you for your help with this problem. Unfortunately, the code you suggested is giving pre-compile errors - 'cannot find symbol' for the following 4 lines;
setAutoCreateContainerGaps, setAutoCreateGaps, hGroup.addComponent(frame) and vGroup.addComponent(frame).

I tried Fix Imports and Fix Code. Not sure what to do now. This should be easier. Thank you for trying.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-21-2008, 09:37 PM
djc djc is offline
Member
 
Join Date: Jul 2008
Posts: 10
djc is on a distinguished road
All of those methods come from the GroupLayout class. Either try import javax.swing.*; or import javax.swing.GroupLayout.*;. Also, the two gap methods aren't really necessary, especially setAutoCreateGaps if you only have one component (a ChartFrame) in the container. setAutoCreateGaps auto creates gaps between components and the other method auto creates gaps between the component and your container. That one might be useful for the aesthetics of your layout, but probably not setAutoCreateGaps.

If it's still giving you trouble, please let me know where I can find the necessary jars to install chartframes, JFreeCharts, DefaultPieDataSets etc, unless some of those are standard java classes already. Thanks.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-22-2008, 11:51 PM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
A description of JFreeChart is available www jfree.org/jfreechart/
The class libraries are downloaded from SourceForge at sourceforge.net/project/showfiles.php?group_id=15494
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-23-2008, 10:27 PM
djc djc is offline
Member
 
Join Date: Jul 2008
Posts: 10
djc is on a distinguished road
Well it looks like you were right, it shouldn't be this hard to manually add components to containers. Apparently all we had to do was to choose Set Layout after right clicking a container.

I haven't learned what the difference is between all of the layouts, but apparently you can use panel.add(component) with a BoxLayout. Unfortunately, I've attempted both ways to add your chart to a panel, and although it is displayed on the screen, it is not inside of a panel. I'm getting this error: java.lang.IllegalArgumentException: adding a window to a container

When I have time I'll look into what that means and if there is a way to avoid it.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-25-2008, 09:24 PM
Member
 
Join Date: Jun 2008
Posts: 6
harrier is on a distinguished road
I think the problem is that Java (or the NetBeans program construction) cannot find the address of the chart object within JFreeChart. JFreeCharts constructs a Frame or a Panel ok (it knows where its objects are), but using the chart object outside of JFreeCharts is a problem. Each Layout may require its own syntax, but I'm not sure this is the root problem.
Still looking for a solution.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
refresh JPanel olesja AWT / Swing 1 04-16-2008 05:58 PM
Genarate JPanel Bill AWT / Swing 6 03-24-2008 09:38 PM
JPanel won't update ibanez270dx New To Java 2 11-19-2007 06:42 AM
Problem with JPanel ibanez270dx New To Java 2 11-09-2007 07:04 PM
JPanel Problems Riftwalker AWT / Swing 6 10-16-2007 01:16 AM


All times are GMT +3. The time now is 02:04 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org