I am using the DJ Native Swing API to be able to use JWebBrowser.
public class GUI extends JPanel {
Robot robot;
JComponent glassPane;
public GUI(final Bot bot) {
super(new BorderLayout());
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
JPanel webBrowserPanel = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.constrainVisibility());
webBrowser.setBarsVisible(false);
webBrowser.setButtonBarVisible(false);
webBrowser.setMenuBarVisible(false);
webBrowser.navigate("http://google.com");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
final JButton button = new JButton("Start");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (bot.isRunning()) {
button.setText("Start");
} else {
button.setText("Stop");
}
bot.setRunning(!bot.isRunning());
}
});
buttonPanel.add(button, BorderLayout.WEST);
add(buttonPanel, BorderLayout.NORTH);
JFrame mainFrame = new JFrame("Bot");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(this, BorderLayout.CENTER);
glassPane = new JComponent()
{
public void paintComponent(Graphics g)
{
g.setColor(new Color(1, 0, 0, 0.5f));
g.fillRect(10, 10, 815, 775);
}
};
glassPane.setSize(815,775);
glassPane.setOpaque(false);
mainFrame.setGlassPane(glassPane);
glassPane.setVisible(true);
mainFrame.setSize(815, 775);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
}
When I run this, I expect to see a red rectangle over the whole GUI. This is not what happens. This is what I see (sorry, it won't let me post direct images): https://i.stack.imgur.com/ZAe51.png
Why is the JWebBrowser appearing over the glass pane? I thought the Glass Pane should be over EVERYTHING.
JWebBrowser extends JPanel. Here is the documentation: Javadoc
My goal for this glass pane is to be able to get the mouse position when the user is using the GUI. For some reason the mouse position is returning null (even though it is over the component) using webBrowser.getMousePosition() or webBrowserPanel.getMousePosition().
because you set
mainFrame.setGlassPane(glassPane);
GlassPane
by default to consumeMouseEvents
, but you can redispatch those eventsEDIT
if I use code example from codersource
with code by @camickr
then output will be
than I can't agreed that GlassPane is bellow any of JComponent, nor JWebBrowser, story ended,
EDIT Correction, I can't found an alternative (right alternative) for GlassPane for SWT, then you are right JWebBrowser is over the AWT / Swing GlassPane
output will be