0

I have a method which takes a list of Tasks. It populates the name of the Tasks in a JCombo with initial selection as blank.

When a user selects a Task, another method getGraph is called with that Task. getGraph returns a javax.swing.JPanel Object which could be added into the JFrame. If I am successful in adding this panel into the frame, it should show a graph.

However, I do not know how to show a blank frame initially, and how to refresh the graph on the frame with the new graph in the new panel returned by JCombo ActionListener.

The method drawGraph is as below:

public void drawGraph(final List<MyDomain> tasks) {

    // generate combo box panel
    JComboBox combo = new JComboBox();
    combo.addItem("");
    for (MyDomain job : tasks) {
        combo.addItem(task.getTaskName());
    }

    combo.setSelectedIndex(0);

    combo.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {

            JComboBox jcmbType = (JComboBox) e.getSource();
            String selectedTask = (String) jcmbType.getSelectedItem();
            System.out.println(selectedTask);

            // GraphZoomScrollPane extends javax.swing.JPanel
            GraphZoomScrollPane graphPanel = getGraph(selectedTask, tasks);

            // how to add this panel to the main frame and refresh it with new graph
        }
    });

    JPanel taskNamePanel = new JPanel();
    jobNamePanel.setBorder(BorderFactory.createTitledBorder("Select Task"));
    jobNamePanel.add(combo);

    // create a frame and add panels
    JFrame frame = new JFrame("My Visual");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(taskNamePanel, BorderLayout.SOUTH);

    // What will go here so that intially the frame is blank ?

    frame.pack();
    frame.setVisible(true);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Vicky
  • 16,679
  • 54
  • 139
  • 232
  • 2
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html), as shown [here](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Aug 15 '14 at 14:01
  • 1
    From, your description, IMO, it seems like a flawed design - the fact the each task holds a panel. You should just use one main panel, and just have a method to swap out the state used to draw/paint/display the graph. – Paul Samsotha Aug 15 '14 at 14:09
  • I would always start with the Swing tutorial on [How to Use a CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) from the Swing tutorial. It has an example that swaps panels based on a selection from a combo box. – camickr Aug 15 '14 at 14:09
  • 2
    Disclainer to my previous comment: it was said without know exactly what a "graph" _is_, in the context of your program. If it's a panel with a bunch of components, then yea CardLayout is the way to go. On the other hand if you painting a graph or using something like JFreeChart, then I would suggest considering the previous comment – Paul Samsotha Aug 15 '14 at 14:16
  • @peeskillet: GraphZoomScrollPane is a JUNG object which internally draws a graph. JUNG is a java graph API. Probably the best. – Vicky Aug 15 '14 at 15:12
  • I shall check it out. Thanks for the heads up. Seems like CardLayout may be the way to go (without extending the component), as the component doesn't offer any api method to switch out it's internals (assuming it's the VisualizationViewer) :-) – Paul Samsotha Aug 15 '14 at 15:24
  • @peeskillet: Yes. VisualizationViewer it is! – Vicky Aug 15 '14 at 17:27

0 Answers0