Adesh singh
Adesh singh

Reputation: 2133

Swap between JavaFX & Swing control

I am working on a swing application using JavaFX controls. in this application i have three controls buttons WebView and JTable. on clicking on button1 table should be added on the screen and web view should be removed while on clicking on other button table should be removed and web view should be added.

I am using the following code.

  import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.web.*;
import javafx.stage.Stage;
import javafx.embed.swing.JFXPanel; 
import javafx.scene.Group;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
 public class webviewtestclass extends JFrame implements ActionListener {
 JFXPanel fxpanel,fxpanel1;
 static String filepath;
JTable table;
  public  webviewtestclass()
  {
    setLayout(null);
    JButton b1= new JButton("OK");
    JButton b2= new JButton("OKK");
    add(b1);
    add(b2);
    b1.setBounds(20,50,50,30);
    b2.setBounds(70,50,50,30);
   b1.addActionListener(this);
   b2.addActionListener(this);

   fxpanel= new JFXPanel();
   add(fxpanel);
   fxpanel.setBounds(10,50,1000,800);


   Object obj=new Object[50][20];
   String title[]={"head1","head2"};
    table=new JTable(title,obj);
   add(table);


 }

   public void actionPerformed(ActionEvent ae)
   {
   if(ae.getActionCommand().equals("OK"))
    {
     remove(fxpanel);
      add(table);   

     }
     if(ae.getActionCommand().equals("OKK"))
     {
      remove(table);
      add(fxpanel);   

    }
     Platform.runLater(new Runnable() {
     public void run()
      {
        initFx(fxpanel);
      }}
        );
     }



  private static void initFx(final JFXPanel fxpanel)
  {
    String htmlpath="d:/lcrux/html/";
    Group group= new Group();
    Scene scene= new Scene(group);
    fxpanel.setScene(scene);    
    WebView webview = new WebView ();
    group.getChildren().add(webview);
    webview.setMinSize(1200,800);
     webview.setMaxSize(1200,800);  

      webview.setVisible(true);
      final WebEngine eng = webview.getEngine();
      File htmlFile = new File(htmlpath+filepath);
        try
        {
        eng.load(htmlFile.toURI().toURL().toString());
        }
        catch(Exception ex)
        {
        }
    }
  public static void main(final String args[])
    {


         webviewtestclass frm=new webviewtestclass();
         frm.show();

    }


  }

Upvotes: 0

Views: 725

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168815

Put both components in a CardLayout to swap between them. Code using a CardLayout can be seen in this answer.

Upvotes: 2

Related Questions