Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31018

Re: Disabling generated buttons

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!newsfeed.tele2net.at!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!feeder01.ams.xsnews.nl!frontend-F09-07.ams.news.kpn.nl
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: Disabling generated buttons
Organization Decebal Computing
References <874mr41ox9.fsf@Equus.decebal.nl> <87vbjkz9df.fsf@Equus.decebal.nl> <c11fb336-efde-4772-a5ae-1c0889dcd47e@googlegroups.com>
X-Face "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E
X-Homepage http://www.decebal.nl/
Date Mon, 02 Feb 2015 20:25:42 +0100
Message-ID <87bnlcyv2h.fsf@Equus.decebal.nl> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)
Cancel-Lock sha1:ku9YqSkc2SHGXWNB/h7czp9Vq0k=
MIME-Version 1.0
Content-Type text/plain
Lines 79
NNTP-Posting-Host 77.171.42.144
X-Trace 1422905334 news.kpn.nl 19973 77.171.42.144@kpn/77.171.42.144:38891
Xref csiph.com comp.lang.java.programmer:31018

Show key headers only | View raw


Op Monday 2 Feb 2015 19:24 CET schreef FredK:

> On Monday, February 2, 2015 at 6:35:48 AM UTC-8, Cecil Westerhof wrote:
>> Op Monday 2 Feb 2015 13:24 CET schreef Cecil Westerhof:
>>
>>> I defined the following class:
>>> private enum Colours {
>>> Blue(Color.BLUE),
>>> Red(Color.RED),
>>> Orange(Color.ORANGE),
>>> Yellow(Color.YELLOW),
>>> Green(Color.GREEN),
>>> Cyan(Color.CYAN),
>>> Magenta(Color.MAGENTA),
>>> Brown(new Color(0x9C, 0x5D, 0x52)),
>>> ;
>>>
>>> private final Color value;
>>>
>>> Colours(Color value) {
>>> this.value = value;
>>> }
>>>
>>> public Color value() {
>>> return value;
>>> }
>>> }
>>>
>>> I use it to add some (Swing) buttons:
>>> for(int i = 0; i < Colours.values().length; ++i) {
>>> final Colours   colour  = Colours.values()[i];
>>>
>>> JButton jButton = new JButton("" + colour);
>>>
>>> add(jButton, gbc);
>>> jButton.addActionListener(new ActionListener() {
>>> public void actionPerformed(ActionEvent e) {
>>> checkColour(colour);
>>> }
>>> });
>>> gbc.gridx++;
>>> if( gbc.gridx == 3 ) {
>>> gbc.gridy++;
>>> gbc.gridx = 0;
>>> }
>>> }
>>>
>>> Those buttons are used to select a colour. But when the wrong
>>> colour is selected I want to disable all the buttons for half a
>>> second. (Otherwise people could just click very fast at random.)
>>> How would I do this?
>>
>> I hoped that there was a 'smart' way, but probably not. So I
>> implemented it in the 'dumb' way.
>>
>> I created a variable to hold them all: private JButton
>> colourButtons[] = new JButton[Colours.values().length];
>>
>> I changed the generating into:
>> for(int i = 0; i < Colours.values().length; ++i) {
>> final Colours   colour  = Colours.values()[i];
>>
>> JButton jButton = new JButton("" + colour);
>>
>> colourButtons[i] = jButton;
>> add(jButton, gbc);
>
> The above add() saves the buttons, so now you are saving them in two
> places. Presumably "this" is some kind of JPanel. The buttons you
> added are available by calling this.getComponents()

The buttons are on a JFrame, but there are other buttons also on
the JFrame and only the Colour buttons should be disabled. That is why
I use the array. But I look into a cleaner solution.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Disabling generated buttons Cecil Westerhof <Cecil@decebal.nl> - 2015-02-02 13:24 +0100
  Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 07:47 -0500
    Re: Disabling generated buttons Cecil Westerhof <Cecil@decebal.nl> - 2015-02-02 14:03 +0100
      Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 08:26 -0500
        Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 08:48 -0500
          Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 09:06 -0500
        Re: Disabling generated buttons Cecil Westerhof <Cecil@decebal.nl> - 2015-02-02 15:17 +0100
  Re: Disabling generated buttons Cecil Westerhof <Cecil@decebal.nl> - 2015-02-02 15:16 +0100
    Re: Disabling generated buttons FredK <fred.l.kleinschmidt@gmail.com> - 2015-02-02 10:24 -0800
      Re: Disabling generated buttons Cecil Westerhof <Cecil@decebal.nl> - 2015-02-02 20:25 +0100
        Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 19:29 -0500
          Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 19:35 -0500
            Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 20:42 -0500
              Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 21:10 -0500
                Re: Disabling generated buttons Jeff Higgins <jeff@invalid.invalid> - 2015-02-02 23:32 -0500

csiph-web