|
|
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.
|
|

06-04-2008, 02:57 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
.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.
|
|

07-18-2008, 01:52 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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?
|
|

07-18-2008, 10:01 AM
|
 |
Senior Member
|
|
Join Date: May 2008
Posts: 299
|
|
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
JFreeChart chart = ChartFactory.createPieChart(
"Memory Information(in %)", dataset, true, true, false);
__________________
To finish sooner, take your own time....
Nivedithaaaa
|
|

07-18-2008, 06:48 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
.Adding to JPanel
Originally Posted by djc
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.
|
|

07-18-2008, 07:02 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
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);
|
|

07-19-2008, 12:27 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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.
|
|

07-21-2008, 06:30 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
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.
|
|

07-21-2008, 08:37 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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.
|
|

07-22-2008, 10:51 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
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
|
|

07-23-2008, 09:27 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 10
|
|
|
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.
|
|

07-25-2008, 08:24 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 6
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|