A
apchar
Consider the applet below. There is a small JTextArea sitting in the
glass pane. There are 3 JButtons sitting in a JPanel that is sitting
in the content pane. One is completely under the JTextArea, one is
half in and half out, & the last is completely outside it (at least I
hope thats what it looks like on your machine.) The idea is when you
click the disable button the JTextArea is disabled and you can click
on the button that's underneath it.
But it doesn't work that way. even when the JTextArea is disabled the
mouse still cant click through it.
Is there some way to make the JTextArea completely invisible to the
mouse so I can click through it? I know setVisible(false) on the glass
panel will work but I only want it to be invisible to the mouse, not
my eyes.
Thanks.
/* <applet code="MyTest14" width="400" height="100"></applet> */
// testing glass panes
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class MyTest14 extends JApplet implements MouseListener
{
TextPanel textPanel;
JPanel buttonPanel;
JButton underButton, enableButton, disableButton;
JTextArea jta;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground(Color.WHITE);
underButton = new JButton("Under textarea");
underButton.addMouseListener(this);
enableButton = new JButton("Enable textarea");
enableButton.addMouseListener(this);
disableButton = new JButton("Disable textarea");
disableButton.addMouseListener(this);
buttonPanel = new JPanel(new BorderLayout());
buttonPanel.add(underButton, "West");
buttonPanel.add(enableButton, "Center");
buttonPanel.add(disableButton, "East");
jta = new JTextArea();
jta.setPreferredSize(new Dimension(200,80));
jta.setOpaque(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
textPanel = new TextPanel();
textPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
textPanel.add(jta);
jta.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.add(buttonPanel);
setGlassPane(textPanel);
textPanel.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == enableButton) { jta.setEnabled(true); }
else if (e.getSource() == disableButton) { jta.setEnabled(false); }
else if (e.getSource() == underButton) { System.out.println("You
reached the under button!"); }
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
class TextPanel extends JPanel
{
public TextPanel()
{
super();
setOpaque(false);
}
public void paintComponent(Graphics g) { super.paintComponent(g); }
}
}
glass pane. There are 3 JButtons sitting in a JPanel that is sitting
in the content pane. One is completely under the JTextArea, one is
half in and half out, & the last is completely outside it (at least I
hope thats what it looks like on your machine.) The idea is when you
click the disable button the JTextArea is disabled and you can click
on the button that's underneath it.
But it doesn't work that way. even when the JTextArea is disabled the
mouse still cant click through it.
Is there some way to make the JTextArea completely invisible to the
mouse so I can click through it? I know setVisible(false) on the glass
panel will work but I only want it to be invisible to the mouse, not
my eyes.
Thanks.
/* <applet code="MyTest14" width="400" height="100"></applet> */
// testing glass panes
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class MyTest14 extends JApplet implements MouseListener
{
TextPanel textPanel;
JPanel buttonPanel;
JButton underButton, enableButton, disableButton;
JTextArea jta;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.setBackground(Color.WHITE);
underButton = new JButton("Under textarea");
underButton.addMouseListener(this);
enableButton = new JButton("Enable textarea");
enableButton.addMouseListener(this);
disableButton = new JButton("Disable textarea");
disableButton.addMouseListener(this);
buttonPanel = new JPanel(new BorderLayout());
buttonPanel.add(underButton, "West");
buttonPanel.add(enableButton, "Center");
buttonPanel.add(disableButton, "East");
jta = new JTextArea();
jta.setPreferredSize(new Dimension(200,80));
jta.setOpaque(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
textPanel = new TextPanel();
textPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
textPanel.add(jta);
jta.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.add(buttonPanel);
setGlassPane(textPanel);
textPanel.setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == enableButton) { jta.setEnabled(true); }
else if (e.getSource() == disableButton) { jta.setEnabled(false); }
else if (e.getSource() == underButton) { System.out.println("You
reached the under button!"); }
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
class TextPanel extends JPanel
{
public TextPanel()
{
super();
setOpaque(false);
}
public void paintComponent(Graphics g) { super.paintComponent(g); }
}
}