I want to hide and show FXPanel controls in javaFx application in swing
I want on clicking a button FXPanel control should be hidden and on clicking other the control should be again visible it is being hidden not being visible again.
using the following code.
public class abc extends JFrame
{
JFXPanel fxpanel;
Container cp;
public abc()
{
cp=this.getContentPane();
cp.setLayout(null);
JButton b1= new JButton("Ok");
JButton b2= new JButton("hide");
cp.add(b1);
cp.add(b2);
b1.setBounds(20,50,50,50);
b2.setBounds(70,50,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
fxpanel= new JFXPanel();
cp.add(fxpanel);
fxpanel.setBounds(600,200,400,500);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("OK"))
{
fxpanel.setVisible(true);
}
if(ae.getActionCommand().equals("hide"))
{
fxpanel.hide();
}
Platform.runLater(new Runnable())
{
public void run()
{
init Fx(fxpanel);
}}
);
}
private static void initFX(final JFXPanel fxpanel)
{
Group group = ne Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
WebView webview= new WebView();
group.getChildren().add(webview);
webview.setMinSize(500,500);
webview.setMaxSize(500,500);
eng=webview.getEngine();
File file= new File("d:/new folder/abc.html");
try
{
eng.load(file.toURI().toURL().toString());
}
catch(Exception ex)
{
}
}
public static void main(String args[])
{
abc f1= new abc();
f1.show();
}
}
Aside from a number of typos, there’s multiple issues with your code:
1) If you are using ActionEvent#getActionCommand to determine which button was clicked, you have to set the action command property on the button first. The action command ist not the same as the button’s text.
2) You are adding both buttons with the same coordinates, hence one will not show.
3) Don’t use the deprecated hide()-Method to hide the JFXPanel, use setVisisble(false)
Furthermore, a few general pointers:
4) Don’t use null layout for a normal UI. Ever.
5) Read up on the java naming convention. This is not just me being picky, it will help you better understand other people’s code and help other people maintain yours.
6) Invoke the code that displays your swing components from the EDT via
SwingUtilities#invokeLater, just as you used thePlatformclass. Invoking swing from the main thread like you did will work most of the times, but induce occasional errors that will be hard to track.