Making a component focusable

I made a class extending JPanel and I want it to be selectable/focusable. I've been looking through the source code of many other classes (like AbstractButton) that have this function, but I still don't really understand what I need to implement or code in my component. They implement quite much and I don't really know what to take. Anyone able to help me, maybe with an example class that implements the basic functions for selectability? Thanks in advance.

1) Run the code as is. Focus toggles between the 2 components
2) Remove the comment and retest. Focus now toggles between the 3 components.
import java.awt.*;
import javax.swing.*;
public class FocusTest extends JFrame
     public FocusTest()
          JPanel panel = new JPanel();
//          panel.setFocusable(true);
          getContentPane().add(panel);
          panel.add(new JCheckBox("One"));
          panel.add(new JCheckBox("Two"));
          getContentPane().add( panel );
     public static void main(String[] args)
          FocusTest frame = new FocusTest();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setLocationRelativeTo( null );
          frame.setVisible(true);
}

Similar Messages

  • How can I make a custom component focusable?

    Hi all,
    I'm using a custom made component that consists of a JLabel
    and a JTextField. I want to make this component focusable,
    and when it's in focus I want the JTextField to have the
    focus so that the user can start typing immediately.
    I've looked at the java tutorial
    (http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#focusable).
    When my component gets the focus I request the focus to the
    JTextField. The problem is: whenever i check if my custom
    component has the focus, it hasn't because JTextField has it
    (the focus, i mean :-) ) Which isn't strange i think, but
    how can i solve this problem?

    I want to make this component focusable, and when it's in focus I want the JTextField to have the focus
    The problem is: whenever i check if my custom component has the focus, it hasn't because JTextField has it (the focus, i mean :-) So whats the problem the textField has focus as desired.
    You don't need to make the containing panel focusable in order for the text field to receive focus. The FocusManager is smart enough to place focus on the first focusable component contained in the panel. If you make the panel focusable then you will need to use the tab key twice, once to move focus to the panel and a second time to move focus to the text field.

  • Making a component half way transparent(not fully opaque or nuopaque)

    Is it possible to make a component (frame, panel etc) half way transparent, I know that its possible to make it fully transparent but i would like to know of a way to go half way, thanks.

    I saw something recently about making a JFrame transparent in another question. You need to use a third party package. Sorry, I don't remember the posting. If you try searching the forum you might find it again. You can use my id as one of the keywords.
    Otherwise for JComponents you need to do three things:
    a) make the component non-opaque
    b) set the background color of the component using a Color object that has an "alpha" value specified.
    c) however you can't just set the background since it won't automatically be painted, so you need to override the paintComponent() method of the component to paint the background yourself. Something like:
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    super.paintComponent(g);Or instead of using a background with an alpha value I think you can use something like this to paint the background:
    Graphics2D g2 = (Graphics2D)g;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    g2.setComposite(ac);
    g2.setPaint(getBackground());
    g2.fillRect(0, 0, getWidth(), getHeight());Don't know which approach is better.

  • Making a component to view my Own Map !!

    Dear all,
    I am developing a website with jsf and richfaces, but now, I'm in a state that I need to show a detailed map of my city to the user. I looked around to find a component that make it work, but I couldn't find one!
    Is there anyway to show my own detailed map (except google map) in a jsf component and what is it?!?
    or, if there is not one, how can I make a component that gives an image as input map, and makes available zooming in and out on the map, and selecting a point.
    my second question is, how can I visualize addressing??! I mean that, how can I let user to make an area selection on the map and I write the address with the user's selection ?
    please help me! a component for viewing my own map that is an image ?!?!

    Such a JSF component doesn't exist yet. Look at the Google map API and integrate it. Basically JSF components does nothing much else than taking some variables and outputting some plain vanilla HTML, so it shouldn't be that hard to put all the HTML/JS of the Google API together in a JSF component.

  • Flash 8: Making a component.

    It's really odd. When I test my scene (press ctrl+enter) and
    then go back to the "component inspector" the second I change a
    field and press "backspace" to delete a number in a textfield, it
    RESETS!! It's driving me crazy. Is this a bug in Flash8 or a bug in
    my code?

    Well, it wasn't simple to do because I had som many
    variables. But it was a simple solution. Sorry for not leaving a
    longer reply, but it was very late in the evening when I figured it
    out.
    1.) My code was just sloppy. I didn't use get/set on any of
    the variables I declared in my Class. Like this:
    //The better way...
    [Inspectable (type="Number" , defaultValue="myParticle" )]
    public function set $particleName($particleName:String){
    particleName = $particleName;
    }//end set
    public function get $particleName(){
    return particleName;
    }//end get
    //the way I had it...
    [Inspectable (type="Number" , defaultValue="myParticle" )]
    var public particleName:String;
    2.) The sliders I made were reseting but the textfields were
    ok with my code like this in the Component_UI:
    //textfiled something like this
    this.onLoad = function(){
    ParticleID.text = xch.$particleName;
    }//end onLoad
    //slider I made something like this
    RateSlider.onMouseUp = function(){
    sx = this.sliderButton._x;
    Rate.text = sx;
    xch.$genRate = sx;
    }//end function
    The problem was, when I reselected the component on the
    stage, it would reset. Actually just about anything I did would
    reset the darn things. So I decided to see if setting thier current
    position to value of the xch.$variable onLoad would do the job.
    Like this:
    this.onLoad = function(){
    Rate.text = xch.$genRate;
    RateSlider.sliderButton._x = xch.$genRate;
    }//end onLoad
    Yup, that worked. So I did the same for the rest of all my
    sliders in my Component_UI and things seem to be running smoothly
    now.
    Does that make sense. Someone else might be able to see what
    I did and explain it better.
    Take care! I hope that helps someone.

  • Tab to change component focus not "real tab" in jtextarea

    Hi,
    Assume you have four text field in one panel. The first text field get focus. Then you press tab, the focus will change to next text field... and so on..... If you put one text area in that panel..... the story begins....
    When you press tab in text area, it will do "real tab" in that text area....
    How do I disable that? I want it so that when you press tab in text area, it will change the focus to next element not do "real tab"...
    Thank you.

    Feel free to remove parts of the code that you feel
    are not required. The code was only posted as a
    suggestion. If there are some lines of code that you
    don't want then please remove then. You are under no
    obligation to use the code as posted. To make the
    code shorter you could try shortening the method
    names. I used "tabForward" and "tabBackward". You
    could use "f" and 'b" to shorten the names.Hehe, nice to have you back.

  • TabBar click , making a Component Visible to true and false

    Hi
    I have a Tabbar with 4 tabs on it Say One , Two , Three and Four .
    <mx:TabBar  id="viewTab" itemClick="switchView();">
    <mx:dataProvider>{VIEW_ARRAY}</mx:dataProvider>
    </mx:TabBar>
    public function switchView():void
    if(tabselected == Two)
    ToggleButtonBar.visible = true;
    if(tabselected == Three)
    ToggleButtonBar.visible = False;
    As you can see
    On clicking of Tab Two , i am showing a ToggleButtonBar Visible to false and on click of Tab Three i am making it visible to False .
    Now the problem is that again on click of Tab Two , this ToggleButtonbar is not visible again ??

    Can you post a complete test case in 20 lines or so?

  • Making a component invisible...

    Hi,
    I'm trying to make a component in my movie invisible using
    the instance._visible = false ActionScript command. It's a button
    that was converted to a symbol from a series of filled rectangles
    and ovals. It will not go invisible although I can use the same
    script on a simple, independent text-box and it works fine. Am I
    missing something?
    Thanks in advance - Jim.

    Thanks for responding. It's working. But I still have some
    minor questions.
    1) When I put the code within the onMotionFinished function,
    it doesn't work. But when I place it at the very beginning (before
    the tweening), it works. Why is this?
    2) Also, what is the difference between using:
    this._visible = false; versus
    this.red_mc.triggerBox_red_btn._visible = false; or just
    red_mc.triggerBox_red_btn._visible = false;? The latter 2 do
    not work. Why is this?
    thanks you very much for your help.
    Here's the modified snippet with the new code:

  • ADF Faces: Making a component non-navigable. How?

    Hi,
    Is it possible to make an ADF Faces component non-navigable? Something like specifying
    tabindex="-1"in HTML.
    I need this most for buttons.
    Best regards,
    Bisser

    That's right. JSF produces such IDs. They are causing problems with JAAS login forms, so we should use plain JSPs for login, but for the rest of the tasks such IDs are okay.
    What I meant is that currenty the buttons are not rendered as
    <input type="button" ... />but as pictures embedded in hyperlinks (&lt;a>).
    It's possible, though not likely, that in some future version of ADF Faces some component will be rendered as an HTML element that doesn't support tab ordering (although I haven't really considered if there are such elements). Then the trick with manually setting the tabIndex of the parent HTML element won't work.
    Of course, I deeply appreciate the fact that you take the time to help us on this forum. In fact, I will go ahead and do just that -- I will dynamically set the tabIndex via JavaScript. I was simply wanting to make sure that I wasn't taking such a hack-like approach, if there was a better method.
    Thank you again.
    Best regards,
    Bisser

  • How to set focus on a jsf component of portlet based web page ?

    Hi,
    I have a jsf based portlet in a Websphere Portal environment. I have seen the javascript code document.getElementId(form.component).focus for the attribute onLoad in body tag of a page
    But in a portlet page, the body tag is somewhere in the Head page of the portal.
    How do I achieve this in another way? Is there that accesskey attribute for a component help me to set the focus on itself? HOw do I do that? Any help is appreciated
    Thanks

    Don't put it in the body. Put it below in the code, right before the closing body tag.
    Example<f:verbatim>
        <script>
            setFocus('</f:verbatim><h:outputText value="#{myBean.elementIdToFocus}" /><f:verbatim>'); // load on every view
            function setFocus(elementId) {
                var element = document.getElementById(elementId);
                if (element && element.focus) {
                    element.focus();
        </script>
    </f:verbatim>Where MyBean.getElementIdToFocus() returns the UIComponent.getId().

  • Problem gaining focus

    Hi all, Im writing an applicaiton and I'm having some trouble getting the focus where I want it and was wondering if anyone could give me a suggestion.
    Here's the setup: I have a JFrame with a JToolBar on it. One of the buttons on the toolbar fires an extention of PAction that I setup. This PAction brings up a JInternalFrame with an optionpane on it that allows the user to enter/edit several pieces of info (several JTextComponents). On this InternalFrame is an InternalFrameListener, on its internalFrameActivated event I do a requestFocus() on the first JTextComponent on the InternalFrame.
    Whats currently happening is that the internalframe is activating and being selected, but the focus seems to be remaining on the toolbar's button. If I fire the same action using a method other than the toolbar (like the menu), the focus goes exactly where I want it.
    Here's what I know so far from debugging:
    1) The listener is definetly firing.
    2) The component never gains focus (in otherwords, im not loosing it after the request)
    3) There is a thread running but it's sleeping until the ok or cancel button is pressed.
    Here's what I've tried so far:
    1) Removing the thread
    2) Requesting focus on the first component: after the frame is opened, after the frame is activated, at the end of the PAction, and having the action reselect the internalframe then give the component focus
    3) I verified there are no other action listeners on the button.
    4) Stared blankly at my screen for a long time lost in thought.
    Any help will be very appreciated,
    Kris

    I experienced the same problem and the way I resolved it
    was making the toolbar buttons non-focusable.
    But this may not be good for you if you need to navigate the
    toolbar with the keyboard.

  • Connecting iPOD classic to TV using Apple Component cable

    I have a 80g ipod classic and an apple component AV cable. I am trying to connect this to an TV with component video inputs. This TV is not an HDTV. I have attached the bl gr r cables in the correct position. I am getting a scrambled picture for NTSC and a split picture for PAL. based on my location I am sure that I should be using NTSC.
    I brought my iPOD into a local apple store and he was able to plug it in to an HDTV there with no problem so I'm pretty sure that niether the ipod or the cable is defective.
    My question is:
    Is the ipod classic only compatible with an HDTV using the component cable? If so, will it work using the composite cable?

    The dock is only good so you can use the remote. I don't know what the problem is with your Standard TV's with Component Inputs ... I've tried two different ones, also different brands, and they worked. I did make sure to change the iPod Video Settings to "TV Size = Standard". If it's set to Widescreen, that may account for the problem.
    Otherwise, I'd suggest returning the Component cables and getting the Composite ones ... or just get the Composite ones as well. Personally, I think Apple totally screwed us all over by not just adding ONE dang Composite Video out cable to the AV Cables and allowing the Component Out to have a HD/Standard switch, much like most of the Game Console AV cables available. I don't know why they didn't do this, especially since we've got about 2 years before EVERYONE starts upgrading to HD. I guess they're just hoping to rake in some extra money by not making the Component AV Cable more capable.

  • Announcement: Join us in a Flex-Related Focus Group

    Hi Everybody,
    Some of you may know me, but many probably not. I'm Jeffry
    Houser, producer of The Flex Show (
    http://www.theflexshow.com),
    a flex related podcast. I also run a DotComIt (
    http://www.dot-com-it.com ),
    an Adobe Solutions Partner specializing in Flex Development.
    DotComIt is working on a line of commercial Flex Components, and
    we'd like to get some feedback from the developer community. In
    order to do this, we will be conducting some focus groups over the
    course of the next few months and want to welcome all Flex
    developers to participate.
    What is a focus group? A focus group is a group of potential
    customers who are brought together to get their opinions,
    perceptions, and ideas that will help to shape the design,
    strategy, and direction of DotComIt's product development.
    Why Participate in a DotComIt Flex Component Focus Group?
    1. Get $50 for your participation (paid via PayPal or
    iTunes).
    2. An opportunity to provide your input, perspective, and
    opinion(s) about Flex and the use of components within Flex.
    3. Hear the opinions and perspectives of other developers
    like yourself, first hand.
    4. Make sure your needs and wants are heard and perhaps help
    shape DotComIt's component development road map.
    To be eligible, you'll just have to fill out a short survey
    at
    http://www.dot-com-it.com/survey.
    We want to talk to developer's with all levels of experience, so no
    matter how much, or how little, you know about Flex we want to hear
    from you. The survey will close on Friday April 4th, and we will
    contact selected participants the following week to discuss
    specific details.
    The focus groups are being conducted by a business
    development firm on DotComIt's behalf. So, for those that know me,
    you can participate w/o being fear of conflict of interests or
    upsetting me.
    Please feel free to pass this on to anyone who you think may
    be interested. Now back to your regularly schedule lists posts.
    Thank You!

    This sounds like bad table design. If you think of column B as a foreign key it looks like you have a many-to-many
    modelled with only two tables. The normal way of handling this in relational DBs is to have a third table as a "relator" table.
    So table 1 and table 2 would be related via a third table 3.
    Alternatively it shouldn't be duplicated and you have bad data.
    Anyway, if you provide the information requested by Roger we will be able to help more.

  • [CRM 5.2] Component Creation

    Hi Gurus,
    we encountered a problem during creation of a new component in SAP CRM 5.2!
    we've created a new ZComponent and generated a new overview page in view node.
    then we associated two external views of another component in Component Usages in Runtime Repository.
    After we associated them to the viewset, put them in the Configuration tab and finally we tested the component.
    We retrieve the followign error:
    Context initialization failed in view CRMCMP_GS_WC/SearchLinks
    An exception has occurred Exception Class  CX_SY_REF_IS_INITIAL - Dereferencing of the NULL reference 
    Method:  CL_CRMCMP_G_SEARCHLINKS_IMPL=>GET_NBS_LINKS 
    Source Text Row:  14
    Initialization of view CRMCMP_GS_WC/SearchLinks failed
    An exception has occurred Exception Class  CX_SY_REF_IS_INITIAL - Dereferencing of the NULL reference 
    Method:  CL_CRMCMP_G_SEARCHLINKS_IMPL=>GET_NBS_LINKS 
    Source Text Row:  14
    Cannot display view IFVSearchLinks
    An exception has occurred Exception Class  CX_SY_REF_IS_INITIAL - Dereferencing of the NULL reference 
    Method:  CL_CRMCMP_G_SEARCHLINKS_IMPL=>GET_NBS_LINKS 
    Source Text Row:  14
    Initialization of view IFVSearchLinks failed
    An exception has occurred Exception Class  CX_BSP_WD_RUNTIME_ERROR - View CRMCMP_GS_WC/SearchLinks could not be bound 
    Method:  CL_BSP_WD_VIEW_CONTROLLER=>BIND_VIEW 
    Source Text Row:  162
    Cannot display view ZCOCKPIT4/ZMYVIEW
    An exception has occurred Exception Class  CX_BSP_WD_RUNTIME_ERROR - View CRMCMP_GS_WC/SearchLinks could not be bound 
    Method:  CL_BSP_WD_VIEW_CONTROLLER=>BIND_VIEW 
    Source Text Row:  162
    Cannot display window ZCOCKPIT4/MainWindow.
    An exception has occurred Exception Class  CX_BSP_WD_RUNTIME_ERROR - View Search.IFVSearchLinks could not be bound 
    Method:  CL_BSP_WD_VIEW_CONTROLLER=>BIND_VIEW 
    Source Text Row:  162
    Do you know where is the issue? Do you know if there is a guide of component creation?  Can you help us?
    Thanks!

    Martijn,
    For questions on steps 1&3 they are related.  When you "enhance" a view on a component you actually perform the first step of making the component ready for enhancements.
    This is done by displaying the component and clicking on the "enhance component" button.  This creates a "Z" storage area for your enhancements.  I need to take a further look at the copy details to remember exactly where the copy should be stored.
    For your question 5, yes once you add it to the repository it will appear in your customizing.
    Well for that piece let's say it is a transaction list, that is an entirely new concept to make the list appear differently.  You would have to adjust the underlying bol objects to make that work.  By chance what is the name of the view that you are trying to copy and adjust?
    There are no books available, but you can buy electronic access to the OKP materials from SAP.  For more information goto service.sap.com/okp
    Good luck,
    Stephen

  • Trapped tab focus - Help!

    Hey all!
    I have several AS3 flash movies embedded in html pages, and
    for whatever reason, when you tab through the page, when you get
    into the flash movie, you can't tab out. The tab just goes in a
    loop through the tabindex of the swf.
    Is there a way that you can add a button or a little script
    in the swf, set it to the last in the taborder, and then make it
    set the focus on the page to a link/div/id/anchor in the html,
    (outside the swf)
    Thanks!
    Joe

    After a bunch of testing and research, it appears that one of
    the core issues that is causing flash focus getting trapped is when
    you use components. The minute I removed the components (TextArea
    Component) focus was no longer trapped.
    So, rule of thumb - don't use components, just build them
    yourself.
    Joe

Maybe you are looking for