我有一个获取Tasks
列表的方法。它会在Tasks
中填充JCombo
的名称,初始选择为空白。
当用户选择Task
时,会使用getGraph
调用另一种方法Task
。 getGraph
会返回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);
}