开发者

Calling an Applet from another Applet

开发者 https://www.devze.com 2023-03-15 02:35 出处:网络
I created two applet classes to create two different charts. In first applet I created one pop-up menu box with some menu items with action performed functions.After clicking the menu item I need to g

I created two applet classes to create two different charts. In first applet I created one pop-up menu box with some menu items with action performed functions.After clicking the menu item I need to go to other applet & has to display that graph. While running and performing the mouse click action it is going to the other applet, but graph is 开发者_JAVA百科not displayed. And also, I need two graphs should be alive I mean after opening the second graph, the first one should not be closed.

First applet :

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

Second applet :

    @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

    }
}


Why not have the second applet be a JDialog instead?

For example (code not tested):

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

  }

and in your SqlValuesInHorizantal class (note change in capitalization of name to conform to Java standards):

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

精彩评论

暂无评论...
验证码 换一张
取 消