To reproduce run the app and push the "Show lightwieght popup" button.
Popup will appear with a text field. It is impossible to edit the text field.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.util.concurrent.CountDownLatch;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
public class JFXPanelApp {
public static String LIGHTWEIGHT_POPUP_BTN = "Show lightwieght popup";
public static int SCENE_WIDTH = 200;
public static int SCENE_HEIGHT = 200;
protected Scene scene;
Popup lightPopup = null;
final JFXPanelApp.TransparentJFXPanel lightweight_popup_fx_panel = new JFXPanelApp.TransparentJFXPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFXPanelApp();
}
});
}
public JFXPanelApp() {
final JFrame frame = new JFrame(this.getClass().getSimpleName());
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lightweight_popup_fx_panel.setBorder(BorderFactory.createLineBorder(Color.black));
final JToggleButton popup_button_light = new JToggleButton(LIGHTWEIGHT_POPUP_BTN);
popup_button_light.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if (lightPopup == null) {
Point location = frame.getContentPane().getLocationOnScreen();
lightPopup = PopupFactory.getSharedInstance().getPopup(popup_button_light, lightweight_popup_fx_panel, (int)location.getX(), (int)location.getY());
lightPopup.show();
} else {
lightPopup.hide();
lightPopup = null;
}
}
});
final JFXPanelApp.TransparentJFXPanel scrollJavafxPanel = new JFXPanelApp.TransparentJFXPanel();
scrollJavafxPanel.setBorder(BorderFactory.createLineBorder(Color.black));
frame.getContentPane().add(popup_button_light, BoxLayout.X_AXIS);
reset();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected final void reset() {
final CountDownLatch cl = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
lightweight_popup_fx_panel.setScene(createScene());
cl.countDown();
}
protected Scene createScene() {
final VBox pane = new VBox();
pane.setMinSize(SCENE_WIDTH, SCENE_HEIGHT);
pane.setMaxSize(SCENE_WIDTH, SCENE_HEIGHT);
pane.setPrefSize(SCENE_WIDTH, SCENE_HEIGHT);
Scene scene = new Scene(pane, SCENE_WIDTH, SCENE_HEIGHT);
final TextField text_input = new TextField("TextInput");
pane.getChildren().add(text_input);
HBox button_box = new HBox(5);
pane.getChildren().add(button_box);
return scene;
}
});
try {
cl.await();
} catch (Exception z) {
}
}
class TransparentJFXPanel extends JFXPanel {
float alpha = 1;
public TransparentJFXPanel() {
super();
setOpaque(false);
}
public void setAlpha(float alpha) {
this.alpha = alpha;
this.repaint();
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
super.paint(g2);
g2.dispose();
}
}
}
Popup will appear with a text field. It is impossible to edit the text field.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.util.concurrent.CountDownLatch;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
public class JFXPanelApp {
public static String LIGHTWEIGHT_POPUP_BTN = "Show lightwieght popup";
public static int SCENE_WIDTH = 200;
public static int SCENE_HEIGHT = 200;
protected Scene scene;
Popup lightPopup = null;
final JFXPanelApp.TransparentJFXPanel lightweight_popup_fx_panel = new JFXPanelApp.TransparentJFXPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFXPanelApp();
}
});
}
public JFXPanelApp() {
final JFrame frame = new JFrame(this.getClass().getSimpleName());
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lightweight_popup_fx_panel.setBorder(BorderFactory.createLineBorder(Color.black));
final JToggleButton popup_button_light = new JToggleButton(LIGHTWEIGHT_POPUP_BTN);
popup_button_light.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if (lightPopup == null) {
Point location = frame.getContentPane().getLocationOnScreen();
lightPopup = PopupFactory.getSharedInstance().getPopup(popup_button_light, lightweight_popup_fx_panel, (int)location.getX(), (int)location.getY());
lightPopup.show();
} else {
lightPopup.hide();
lightPopup = null;
}
}
});
final JFXPanelApp.TransparentJFXPanel scrollJavafxPanel = new JFXPanelApp.TransparentJFXPanel();
scrollJavafxPanel.setBorder(BorderFactory.createLineBorder(Color.black));
frame.getContentPane().add(popup_button_light, BoxLayout.X_AXIS);
reset();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected final void reset() {
final CountDownLatch cl = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
lightweight_popup_fx_panel.setScene(createScene());
cl.countDown();
}
protected Scene createScene() {
final VBox pane = new VBox();
pane.setMinSize(SCENE_WIDTH, SCENE_HEIGHT);
pane.setMaxSize(SCENE_WIDTH, SCENE_HEIGHT);
pane.setPrefSize(SCENE_WIDTH, SCENE_HEIGHT);
Scene scene = new Scene(pane, SCENE_WIDTH, SCENE_HEIGHT);
final TextField text_input = new TextField("TextInput");
pane.getChildren().add(text_input);
HBox button_box = new HBox(5);
pane.getChildren().add(button_box);
return scene;
}
});
try {
cl.await();
} catch (Exception z) {
}
}
class TransparentJFXPanel extends JFXPanel {
float alpha = 1;
public TransparentJFXPanel() {
super();
setOpaque(false);
}
public void setAlpha(float alpha) {
this.alpha = alpha;
this.repaint();
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
super.paint(g2);
g2.dispose();
}
}
}
- relates to
-
JDK-8125481 JFXPanel + Popup + ListView: scrolling by mouse wheel doesn't work
- Closed