Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31002 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-02-02 13:24 +0100 |
| Last post | 2015-02-02 23:32 -0500 |
| Articles | 15 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-02-02 13:24 +0100 |
| Subject | Disabling generated buttons |
| Message-ID | <874mr41ox9.fsf@Equus.decebal.nl> |
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?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 07:47 -0500 |
| Message-ID | <manrr7$nqe$1@dont-email.me> |
| In reply to | #31002 |
On 02/02/2015 07:24 AM, Cecil Westerhof wrote: > How would I do this? > MyButton.setEnabled(false); +.5s MyButton.setEnabled(true);
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-02-02 14:03 +0100 |
| Message-ID | <87zj8wzcr2.fsf@Equus.decebal.nl> |
| In reply to | #31004 |
Op Monday 2 Feb 2015 13:47 CET schreef Jeff Higgins: > On 02/02/2015 07:24 AM, Cecil Westerhof wrote: > >> How would I do this? >> > MyButton.setEnabled(false); > +.5s > MyButton.setEnabled(true); That is for buttons that are stored in a variable, but these are not stored in a variable but generated. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 08:26 -0500 |
| Message-ID | <manu4u$1e1$1@dont-email.me> |
| In reply to | #31005 |
On 02/02/2015 08:03 AM, Cecil Westerhof wrote: > Op Monday 2 Feb 2015 13:47 CET schreef Jeff Higgins: > >> On 02/02/2015 07:24 AM, Cecil Westerhof wrote: >> >>> How would I do this? >>> >> MyButton.setEnabled(false); >> +.5s >> MyButton.setEnabled(true); > > That is for buttons that are stored in a variable, but these are not > stored in a variable but generated. > Shrug? Generate a list of references to the buttons you wish to disable? Use MagicButtons?
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 08:48 -0500 |
| Message-ID | <manvcr$6aa$1@dont-email.me> |
| In reply to | #31006 |
On 02/02/2015 08:26 AM, Jeff Higgins wrote: > On 02/02/2015 08:03 AM, Cecil Westerhof wrote: >> Op Monday 2 Feb 2015 13:47 CET schreef Jeff Higgins: >> >>> On 02/02/2015 07:24 AM, Cecil Westerhof wrote: >>> >>>> How would I do this? >>>> >>> MyButton.setEnabled(false); >>> +.5s >>> MyButton.setEnabled(true); >> >> That is for buttons that are stored in a variable, but these are not >> stored in a variable but generated. >> > Shrug? > Generate a list of references to the buttons you wish to disable? > Use MagicButtons? > Walk the containment hierarchy, call .setEnabled(false) on pertinent buttons +.5s Walk the containment hierarchy, call .setEnabled(true) on pertinent buttons
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 09:06 -0500 |
| Message-ID | <mao0fn$bau$1@dont-email.me> |
| In reply to | #31008 |
On 02/02/2015 08:48 AM, Jeff Higgins wrote: > On 02/02/2015 08:26 AM, Jeff Higgins wrote: >> On 02/02/2015 08:03 AM, Cecil Westerhof wrote: >>> Op Monday 2 Feb 2015 13:47 CET schreef Jeff Higgins: >>> >>>> On 02/02/2015 07:24 AM, Cecil Westerhof wrote: >>>> >>>>> How would I do this? >>>>> >>>> MyButton.setEnabled(false); >>>> +.5s >>>> MyButton.setEnabled(true); >>> >>> That is for buttons that are stored in a variable, but these are not >>> stored in a variable but generated. >>> >> Shrug? >> Generate a list of references to the buttons you wish to disable? >> Use MagicButtons? >> > Walk the containment hierarchy, > call .setEnabled(false) on pertinent buttons > +.5s > Walk the containment hierarchy, > call .setEnabled(true) on pertinent buttons > See the documentation for your Swing GUI Builder.
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-02-02 15:17 +0100 |
| Message-ID | <87r3u8z9bf.fsf@Equus.decebal.nl> |
| In reply to | #31006 |
Op Monday 2 Feb 2015 14:26 CET schreef Jeff Higgins: > On 02/02/2015 08:03 AM, Cecil Westerhof wrote: >> Op Monday 2 Feb 2015 13:47 CET schreef Jeff Higgins: >> >>> On 02/02/2015 07:24 AM, Cecil Westerhof wrote: >>> >>>> How would I do this? >>>> >>> MyButton.setEnabled(false); >>> +.5s >>> MyButton.setEnabled(true); >> >> That is for buttons that are stored in a variable, but these are >> not stored in a variable but generated. >> > Shrug? > Generate a list of references to the buttons you wish to disable? That is what I have done. See other reply. > Use MagicButtons? What do you mean with that? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-02-02 15:16 +0100 |
| Message-ID | <87vbjkz9df.fsf@Equus.decebal.nl> |
| In reply to | #31002 |
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);
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkColour(colour);
}
});
gbc.gridx++;
if( gbc.gridx == 3 ) {
gbc.gridy++;
gbc.gridx = 0;
}
}
And in checkColour I put the following code:
javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
setEnabledColourButtons(true);
}
});
setEnabledColourButtons(false);
timer.setRepeats(false);
timer.start();
This does what I want.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | FredK <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2015-02-02 10:24 -0800 |
| Message-ID | <c11fb336-efde-4772-a5ae-1c0889dcd47e@googlegroups.com> |
| In reply to | #31011 |
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()
--
Fred K
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-02-02 20:25 +0100 |
| Message-ID | <87bnlcyv2h.fsf@Equus.decebal.nl> |
| In reply to | #31016 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 19:29 -0500 |
| Message-ID | <map4ub$b3g$1@dont-email.me> |
| In reply to | #31018 |
On 02/02/2015 02:25 PM, Cecil Westerhof wrote: > Op Monday 2 Feb 2015 19:24 CET schreef FredK: >> >> 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. > Another shrug. Component.getClientProperty(Object key) Component.putClientProperty(Object key, Object value)
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 19:35 -0500 |
| Message-ID | <map5ah$c6j$1@dont-email.me> |
| In reply to | #31022 |
On 02/02/2015 07:29 PM, Jeff Higgins wrote: > On 02/02/2015 02:25 PM, Cecil Westerhof wrote: >> Op Monday 2 Feb 2015 19:24 CET schreef FredK: >>> >>> 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. >> > Another shrug. > Component.getClientProperty(Object key) > Component.putClientProperty(Object key, Object value) > But probably as expensive memory-wise as an array of references. And certainly more expensive code-complexity-wise.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 20:42 -0500 |
| Message-ID | <map98s$nkf$1@dont-email.me> |
| In reply to | #31023 |
On 02/02/2015 07:35 PM, Jeff Higgins wrote:
> On 02/02/2015 07:29 PM, Jeff Higgins wrote:
>> On 02/02/2015 02:25 PM, Cecil Westerhof wrote:
>>> Op Monday 2 Feb 2015 19:24 CET schreef FredK:
>>>>
>>>> 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.
>>>
>> Another shrug.
>> Component.getClientProperty(Object key)
>> Component.putClientProperty(Object key, Object value)
>>
> But probably as expensive memory-wise as an array of references.
> And certainly more expensive code-complexity-wise.
>
You could use the button's text as a distinguishing feature,
but the way that you've shown:
new JButton("" + colour);
is guaranteed by the api to be implementation dependent.
My implementation sets the button text to "java.awt.Color[r=0,g=0,b=255]".
The complexity of a "smart" solution vs the small memory
overhead of the "dumb" solution.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 21:10 -0500 |
| Message-ID | <mapasn$rac$1@dont-email.me> |
| In reply to | #31024 |
On 02/02/2015 08:42 PM, Jeff Higgins wrote:
> On 02/02/2015 07:35 PM, Jeff Higgins wrote:
>> On 02/02/2015 07:29 PM, Jeff Higgins wrote:
>>> On 02/02/2015 02:25 PM, Cecil Westerhof wrote:
>>>> Op Monday 2 Feb 2015 19:24 CET schreef FredK:
>>>>>
>>>>> 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.
>>>>
>>> Another shrug.
>>> Component.getClientProperty(Object key)
>>> Component.putClientProperty(Object key, Object value)
>>>
>> But probably as expensive memory-wise as an array of references.
>> And certainly more expensive code-complexity-wise.
>>
> You could use the button's text as a distinguishing feature,
> but the way that you've shown:
> new JButton("" + colour);
> is guaranteed by the api to be implementation dependent.
> My implementation sets the button text to "java.awt.Color[r=0,g=0,b=255]".
> The complexity of a "smart" solution vs the small memory
> overhead of the "dumb" solution.
>
This is what I meant by "MagicButton".
Perhaps I should have used "SmartButton".
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-02 23:32 -0500 |
| Message-ID | <mapj7k$dn7$1@dont-email.me> |
| In reply to | #31025 |
On 02/02/2015 09:10 PM, Jeff Higgins wrote:
> On 02/02/2015 08:42 PM, Jeff Higgins wrote:
>> On 02/02/2015 07:35 PM, Jeff Higgins wrote:
>>> On 02/02/2015 07:29 PM, Jeff Higgins wrote:
>>>> On 02/02/2015 02:25 PM, Cecil Westerhof wrote:
>>>>> Op Monday 2 Feb 2015 19:24 CET schreef FredK:
>>>>>>
>>>>>> 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.
>>>>>
>>>> Another shrug.
>>>> Component.getClientProperty(Object key)
>>>> Component.putClientProperty(Object key, Object value)
>>>>
>>> But probably as expensive memory-wise as an array of references.
>>> And certainly more expensive code-complexity-wise.
>>>
>> You could use the button's text as a distinguishing feature,
>> but the way that you've shown:
>> new JButton("" + colour);
>> is guaranteed by the api to be implementation dependent.
>> My implementation sets the button text to
>> "java.awt.Color[r=0,g=0,b=255]".
>> The complexity of a "smart" solution vs the small memory
>> overhead of the "dumb" solution.
>>
> This is what I meant by "MagicButton".
> Perhaps I should have used "SmartButton".
And, after all of this, is there a gate at the other end of
the pipeline you can close? Let your user click as furiously
as he wishes; just don't accept his clicks?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web