根据在Combobox中选择的值刷新JFrame上的JPanel

时间:2014-08-15 13:57:59

标签: java swing jpanel jcombobox

我有一个获取Tasks列表的方法。它会在Tasks中填充JCombo的名称,初始选择为空白。

当用户选择Task时,会使用getGraph调用另一种方法TaskgetGraph会返回javax.swing.JPanel Object,可以将其添加到JFrame中。如果我成功将此面板添加到框架中,则应显示图形。

但是,我不知道如何最初显示空白框架,以及如何使用JCombo ActionListener返回的新面板中的新图表刷新框架上的图形。

方法drawGraph如下:

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

0 个答案:

没有答案