1

I have created a method using JFreeChart and SQL that essentially creates a graph based on the data present in the database. I have 3 methods, one particular example, the users weight vs time. I wish to display these graphs in a JTabbedPane and am unsure how. When I call the method on the one tab it tells me static/void methods are not allowed. My method has no paramters. Here is my initial attempt:

    panelWeight = new JPanel();
    panelWeight.setLayout(null);
    panelWeight.add(StatGraph.WeightGraph());

EDIT: I returned to the drawing board, after discovering a) I cannot call a void method to the component and B) a null layout does not scale. Here is my re-written method I wish to call to the JTabbedPane, making the method non-void.

 public static ChartFrame WeightGraph(){

    ChartFrame returnFrame = null;

     try{
        ConnectionManager connectionManager = ConnectionManager.getInstance();
        Connection connection = connectionManager.getConnection();
        UserInfoManager user = new UserInfoManager();
        int username = user.getId();

        String query = "SELECT DATE, WEIGHT FROM STATS WHERE ID=" + username;            JDBCCategoryDataset dataset = new JDBCCategoryDataset(connection, query);
        JFreeChart chart = ChartFactory.createLineChart("WEIGHTvsDATE Chart", "Date", "Weight", dataset, PlotOrientation.VERTICAL, false, true, true);
        BarRenderer renderer = null;
        CategoryPlot plot = null;
        renderer = new BarRenderer();
        ChartFrame frame = new ChartFrame("Progress Log", chart);
        returnFrame = frame;
        //frame.setVisible(true);
        //frame.setSize(750,400);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null,e);

    }

     return returnFrame;
}

I then return to the GUI Frame and add the following code to the Panel in the TabbedPane, activated by using a click listener. panelWeight.add(StatGraph.WeightGraph()); However, I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:488)
at java.awt.Container.addImpl(Container.java:1089)
at java.awt.Container.add(Container.java:415)
at GUI.PT.ProgressStatsPT.panelWeightMouseClicked(ProgressStatsPT.java:392)
at GUI.PT.ProgressStatsPT.access$600(ProgressStatsPT.java:26)
at GUI.PT.ProgressStatsPT$7.mouseClicked(ProgressStatsPT.java:138)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Ryan Teller
  • 33
  • 1
  • 6
  • See also these [examples](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20jdbcXYdataset) mentioning `JDBCXYDataset`, which is suitable for a time series. If this is _not_ a duplicate, please edit your question to include a [mcve] that shows your revised approach based not eh examples cited. – trashgod Aug 14 '16 at 21:58
  • @trashgod I cannot find resources pertaining to JFreeChart, SQL, and JTabbedPane. The resources available return issues when I run a SQL query. – Ryan Teller Aug 14 '16 at 23:27
  • A `null` layout is unlikely to scale; I sense that you overlooked this [example](http://stackoverflow.com/a/24762078/230513) among those cited. – trashgod Aug 15 '16 at 02:47
  • @trashgod I wrote an alternate method that is now value returning in an attempt to display it on one of the panels in the tabbedPane but am greeted with new errors – Ryan Teller Aug 15 '16 at 13:45
  • Absent your [mcve], I don;t know where you're stuck. – trashgod Aug 15 '16 at 20:14

1 Answers1

0

What does exactly Graph.weightLg() return? The add() method from JPanel takes an instance of Component or a subclass. If the Graph.weightLg() method doesn't return anything (which I suppose is your case) the compilation will fail.

Vicente Freire
  • 258
  • 1
  • 6