Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

Receiving MouseEvents from unused area in a container

Thread view: 
Bruce Sutherland - 10 Nov 2005 07:10 GMT
Hi.

In a Swing based application I am working on, I have a JTable inside a
JPanel.

Please see http://members.westnet.com.au/harveystreet/panel.png.

The panel uses a 1x1 GridLayout. I have set the background colour of the
JTable to blue, and the background colour of the JPanel to red.

I would like to clear the JTable's selection when the user clicks in the
gray area. I don't know how to add a MouseListener to this gray area, as I
don't know what Component it is / belongs to!

I tried adding the listener to the JPanel. Also I tried removing the border
in the panel, it made no difference.

If anybody can tell me how to add a MouseListener to this "gray area", or
can suggest a better way of achieving the desired behaviour, it would be
very much appreciated.

Bruce Sutherland.
Andrew Thompson - 10 Nov 2005 07:29 GMT
..
> If anybody can tell me how to add a MouseListener to this "gray area", ..

AFAIU (what you want)

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** A 1x1 GL results in the child component filling the entire
available space. ;-) */
class TableClick {

  public static void main(String[] args) {
    JPanel p = new JPanel(new GridLayout(1,1));

    JTable t = new JTable();
    t.addMouseListener( new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        System.out.println(me);
      }
    } );
    p.add(t);
    JOptionPane.showMessageDialog( null, p );
  }
}
</sscce>

HTH
Bruce Sutherland - 10 Nov 2005 08:32 GMT
Thanks for the reply Andrew.

I should have mentioned that I already tried adding a MouseListener to the
JTable, and I don't get any MouseEvents when I click that gray area.

You are right about the 1x1 GridLayout. When I call getHeight() on that
table, it returns 97 (pixels). So it seems that the JTable is taking up that
space but pretending that it doesn't exist.

I thought about subclassing JTable but I'd really rather not if I can avoid
it.

> ..
>> If anybody can tell me how to add a MouseListener to this "gray area", ..
[quoted text clipped - 26 lines]
>
> HTH
Andrew Thompson - 10 Nov 2005 09:40 GMT
> Thanks for the reply Andrew.

You're welcome, in return, please refrain from
top-posting, and trim earlier text no longer
relevant.

> I should have mentioned that I already tried adding a MouseListener to the
> JTable, and I don't get any MouseEvents when I click that gray area.

It seems like a problem in your code.

Note how the code I posted had no problems detecting
mouse events in the area of the table.

Add a 'new JTable( new DefaultTableModel(1,3) )' to that
constructor and you can also see that clicking in any cell
of any/the row, or the unfilled area, registers the click.

> You are right about the 1x1 GridLayout. When I call getHeight() on that
> table, it returns 97 (pixels). So it seems that the JTable is taking up that
> space but pretending that it doesn't exist.

JTable may be a quixotic component, but it does not enerally
rise to the form of pretence of which you 'accuse' it.

> I thought about subclassing JTable but I'd really rather not if I can avoid
> it.

Think about trimmming your code, one line at a time,
if necessary, back to the example I showed - that works.

Logically, at some point your code will start working,
and *that* line you had just removed, that will be the
(crux of the) problem.

Short of seeing your code* that does not work, I cannot
advise further.

* I am not asking to see your 'propietary and confidential'
code, nor examine a Zip archive of 100Kb of code, but a small
code example of the problem.  Something I refer to by it's
acronym.  An SSCCE.  More info. here..
<http://www.physci.org/codes/sscce.jsp>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.