How do I click through a JTextArea ?

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); }
}
}
 
A

Andrew Thompson

apchar said:
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.)

Yes, that is how it appears here.
...The idea is when you
click the disable button the JTextArea is disabled and you can click
on the button that's underneath it.

I do not know if such a thing is possible,
but I am wonderring why you would force
your users to suffer such a counterituitive GUI?

Is there some purpose to this that I missed
that makes it a good and sensible thing to do?
 
A

Andrew Hobbs

apchar said:
Consider the applet below. There is a small JTextArea sitting in the
snip

{
if (e.getSource() == enableButton) { jta.setEnabled(true); }
else if (e.getSource() == disableButton) { jta.setEnabled(false); }

The applet works exactly as you have written it. Disabling and Enabling a
component is not going to make it go away. It makes it unresponsive to
commands.

If you want it to go away use jta.setVisible(false) and it works as you
suggest it should.

*However* I presume this is just a test to see how to get something to work.
Because if it isn't (as Andrew Thompson suggested) you need to do some
serious reading of articles on GUI design.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top