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 / December 2003

Tip: Looking for answers? Try searching our database.

Using JLabel getLabelFor()  for calling respective text input elements.selectAll()  by mousePressed event on that JLabel

Thread view: 
Marek Mänd - 16 Dec 2003 20:38 GMT
public void mousePressed(MouseEvent e){
  if (e.getSource() instanceof JLabel){
    JLabel labelFor = ((JLabel)e.getSource());
    if ( labelFor.getLabelFor() instanceof JTextField
      ||labelFor.getLabelFor() instanceof JTextArea
      ||labelFor.getLabelFor() instanceof JEditorPane
      ||labelFor.getLabelFor() instanceof JPasswordField
      ||labelFor.getLabelFor() instanceof JTextPane
    )
    {
      (labelFor.getLabelFor()).requestFocus();
      (( WHAT TO WRITE HERE? )   labelFor.getLabelFor() ).selectAll();
    }
  }
}

The problem is that I have Swing JLabel's and I want
to focus and select the contents of varios art text input
control contents when user "clicks" mouse on that JLabel.
The focusing code works fine
but the selectAll() part somehow seems to differ from focusing part
and probably needs downcast. WHY?

I am a java newbee and I have really no idea what to do do make
selecting of text elements work in the "generic" manner above tried.

The code above was the relevant part of
5 kilobyte sized 200 lines code file available at
http://hammer.prohosting.com/~cador/java/NiceJLabels.java

TIA =D
VisionSet - 17 Dec 2003 11:00 GMT
> public void mousePressed(MouseEvent e){
>    if (e.getSource() instanceof JLabel){
[quoted text clipped - 8 lines]
>        (labelFor.getLabelFor()).requestFocus();
>        (( WHAT TO WRITE HERE? )   labelFor.getLabelFor() ).selectAll();

WHAT TO WRITE HERE? = JTextComponent

but make sure only JTextComponents are registered with the listener, or have
another if(component instanceof JTextComponent)

Signature

Mike W

Marek Mänd - 17 Dec 2003 14:17 GMT
> "Marek Mänd"
>>public void mousePressed(MouseEvent e){
[quoted text clipped - 12 lines]
> but make sure only JTextComponents are registered with the listener, or have
> another if(component instanceof JTextComponent)

Thank you for answering!
I have located the error and fixed it =D
    I have sort of "generic" event listener for all elements.
I tried in my foolishness before posting here with JTextComponent, but
it didnt worked for me at that place. As a quite total Java newbee I
thought maybe the downcast didnt work with JTextComponent because of
JTextComponent is described with  *abstract* keyword...
But the whole issue it didnt worked for me in first place was, that
I had to add at the beginning of file:

import javax.swing.text.JTextComponent;

which solved the problem. As a Java newbee I thought that
import javax.swing.*;
includes all "Swing stuff" but it seems not be the case.
Hope someone will onde day benefit from my explenation.

End of thread.


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



©2009 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.