Results 1 to 12 of 12
  1. #1
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

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

  2. #2
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default 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?

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    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
    Java Code:
    JFreeChart chart = ChartFactory.createPieChart(
    	"Memory Information(in %)", dataset, true, true, false);
    To finish sooner, take your own time....
    Nivedithaaaa

  4. #4
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

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

  5. #5
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    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);

  6. #6
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  7. #7
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    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.

  8. #8
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  9. #9
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    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

  10. #10
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    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.

  11. #11
    harrier is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    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.

  12. #12
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Thumbs up

    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);
    This code saved me ! Thank you very much djc !!!

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  2. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  3. Genarate JPanel
    By Bill in forum AWT / Swing
    Replies: 6
    Last Post: 03-24-2008, 07:38 PM
  4. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 PM
  5. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2007, 11:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •