2011-06-23 8 views
0

2つの異なるチャートを作成するために2つのアプレットクラスを作成しました。 最初のアプレットでは、アクションを実行したメニュー項目を含むポップアップメニューボックスを1つ作成しました。メニュー項目をクリックした後、他のアプレットに移動する必要があります&はそのグラフを表示する必要があります。実行中は、マウスクリックアクションを実行している間は他のアプレットに行きますが、グラフは表示されません。 また、私は2番目のグラフを開いた後、最初のものを閉じてはいけないという意味で、2つのグラフが生きている必要があります。別のアプレットからアプレットを呼び出す

まずアプレット:

public class MouseClass extends JApplet implements ActionListener { 
    /** Time series for total memory used. */ 
    JTextArea textArea; 
    JFreeChart localJFreeChart; 
    ChartPanel chartpanel; 
    int time; 
    double channel1; 
    boolean chartClicked; 
    final JPopupMenu cutpasteMenu = new JPopupMenu(); 
    JMenuItem DwellMenuItem = new JMenuItem("Dwell Id"); 
    JMenuItem TargetMenuItem = new JMenuItem("Target Id"); 
    JMenuItem TrackMenuItem = new JMenuItem("Track Id"); 
    XYSeries series1 = new XYSeries("channel1"); 
    XYSeries series2 = new XYSeries("channel2"); 
    XYSeriesCollection dataset = new XYSeriesCollection(); 

    public MouseClass() { 
     try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/wbrdatabase", "root", "ronanki"); 
     Statement st = con.createStatement(); 
     System.out.println("data base created"); 
     ResultSet rs = st.executeQuery("select * from wbrdb"); 
     while (rs.next()) { 
      double time = rs.getDouble(1); 
      double channel1 = rs.getDouble(2); 
      series1.add(time, channel1); 
     } 
     dataset.addSeries(series1); 

     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
     localJFreeChart = ChartFactory.createXYLineChart(
       "graph for particular id", "time", "amplitude", dataset, 
       PlotOrientation.VERTICAL, true, true, false); 
     chartpanel = new ChartPanel(localJFreeChart); 
     MouseEvents(); 
     chartpanel.setHorizontalAxisTrace(true); 
     chartpanel.setMouseWheelEnabled(true); 
     chartpanel.setPreferredSize(new Dimension(400, 300)); 
     getContentPane().add(chartpanel); 
    } 

    public void MouseEvents() { 
     DwellMenuItem.addActionListener(this); 
     TargetMenuItem.addActionListener(this); 
     TrackMenuItem.addActionListener(this); 

     cutpasteMenu.add(DwellMenuItem); 
     cutpasteMenu.add(TargetMenuItem); 
     cutpasteMenu.add(TrackMenuItem); 

     chartpanel.addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e, Cursor cursor) { 
      setCursor(cursor); 

      switch (e.getModifiers()) { 
      case InputEvent.BUTTON1_MASK: { 
       cutpasteMenu.show(e.getComponent(), e.getX(), e.getY()); 
       System.out.println(e.getSource().getClass().getName()); 
       break; 
      } 
      } 
     } 
     }); 

     chartpanel.addMouseListener(new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
      switch (e.getModifiers()) { 
      case InputEvent.BUTTON1_MASK: { 
       cutpasteMenu.show(e.getComponent(), e.getX(), e.getY()); 
       cutpasteMenu.setInvoker(e.getComponent()); 
       break; 
      } 
      } 
     } 
     }); 
    } 

    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 
     System.out.println("came to perform action"); 
     Object source = e.getSource(); 
     if (source == DwellMenuItem) { 
     System.out.println("calling dwell"); 
     sqlvaluesinhorizantal ob = new sqlvaluesinhorizantal(""); 
     // getAppletContext().showDocument(order1); 

     } 
     if (source == TargetMenuItem) { 
     System.out.println("calling target"); 

     getAppletContext().showDocument(order2); 
     } 
     if (source == TrackMenuItem) { 
     System.out.println("calling track"); 

     getAppletContext().showDocument(order3); 
     } 
    } 
} 

第二アプレット:

@SuppressWarnings("serial") 
public class sqlvaluesinhorizantal extends ApplicationFrame implements ChangeListener { 

    int time; 

    public sqlvaluesinhorizantal(String title) { 
     super(title); 

     JPanel localjpanel=createpanel(); 
     localjpanel.setPreferredSize(new Dimension(400,300)); 
     localjpanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4)); 
     // make the slider units "minutes" 

     add(localjpanel, BorderLayout.SOUTH); 
     //setContentPane(localjpanel); 

    } 
     private JFreeChart createChart(XYDataset Dataset) 
     { 
      JFreeChart localJFreeChart = ChartFactory.createXYLineChart("graph for Dwellid", "time", "amplitude", Dataset, PlotOrientation.VERTICAL, true, true, false); 

      return localJFreeChart; 
      } 

    private static XYDataset createDataset() { 
     XYSeries series1 = new XYSeries("channel1"); 

     XYSeriesCollection dataset=new XYSeriesCollection(); 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/wbrdatabase","root","ronanki"); 
     Statement st=con.createStatement(); 
     System.out.println("data base created"); 
     ResultSet rs=st.executeQuery("select * from wbrdb"); 

    while(rs.next()) 
    { 

       double time=rs.getDouble(1); 
     double channel1=rs.getDouble(2); 
     series1.add(time,channel1); 
    } 
    dataset.addSeries(series1); 
    } 
    catch(Exception e) 
    { 
      e.printStackTrace(); 
    } 
    return dataset; 
    } 
    private JPanel createpanel() { 
     JFreeChart chart=createChart(createDataset()); 
     ChartPanel chartpanel=new ChartPanel(chart); 

     chartpanel.setHorizontalAxisTrace(true); 
     chartpanel.setMouseWheelEnabled(true); 

     return chartpanel; 
    } 



    public static void main(final String[] args) { 

     final sqlvaluesinhorizantal demo = new sqlvaluesinhorizantal("hi hello"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 

    } 
    @Override 
    public void stateChanged(ChangeEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 

答えて

0

二アプレットが代わりのJDialogも持っていないのはなぜ?例えば

(コードテストされていません):

if (source == DwellMenuItem) { 
    System.out.println("calling dwell"); 
    SqlValuesInHorizantal ob = new SqlValuesInHorizantal(); // 
    Window parentWindow = SwingUtilities.getWindowAncestor(this); 
    JDialog dialog = new JDialog(parentWindow, "Dialog Title"); 
    dialog.setModalityType(ModalityType.MODELESS); //!! or make this modal if desired 
    dialog.getContentPane().add(ob); 
    dialog.pack(); 
    dialog.setLocationRelativeTo(null); 
    dialog.setVisible(true); 
    // getAppletContext().showDocument(order1); 

    } 

とあなたのSqlValuesInHorizantalクラス(Java標準に準拠する名前の総額でノートの変更):

class SqlValuesInHorizantal extends JPanel implements ChangeListener { 

    int time; 

    public SqlValuesInHorizantal() { 
     setLayout(new BorderLayout()); 
     JPanel localjpanel=createpanel(); 
     localjpanel.setPreferredSize(new Dimension(400,300)); 
     localjpanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4)); 
     // make the slider units "minutes" 

     add(localjpanel, BorderLayout.SOUTH); 
     //setContentPane(localjpanel); 

    } 
+0

ありがとう、それはYAHワーキング。 – sankar

+0

ありがとう、よかった、それは働いています。しかし、アプレットをアプレットにすることはできません。そしてここで私はもう一つの疑問を持っています。つまり、グラフにスクロールバーを追加するにはどうすればいいですか?マウスのスクロールズームを実行した後、私はズームアウトする必要がある合計グラフを表示したい場合。しかし、私はスクロールバーを使用して全体のグラフを参照してください。どのように我々はそれを行うことができます。 – sankar

関連する問題