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 / General / March 2007

Tip: Looking for answers? Try searching our database.

[JComboBox]Read the index of item with X value.

Thread view: 
Mariano - 26 Mar 2007 13:42 GMT
I've a ComboBox like the follow, obviously in the left column ther'are
index and in the right column there are values (for example):
0  32
1  45
2  46
3  58

Now I would to know the index of the determinated item in the combo,
(for example 46).
The element (46 in this case) is not selected, so I can't use methods
like getSelected...
So should be useful a method that accept in input a parameter (value
number) and return in output correspondent index number.

For Example: myMethod(46); that give me 2 in output.

How could I resolve?
Andrew Thompson - 26 Mar 2007 14:09 GMT
> I've a ComboBox ..

>From where?  There is no ComboBox in the JSE.
Are you perhaps referring to java.awt.Choice,
or javax.swing.JComboBox?
..
> How could I resolve?

1) Be specific about the classes used, rather
than simply 'making the names up, as you go along'
2) ..

Andrew T.
Andrew Thompson - 26 Mar 2007 14:17 GMT
In addition to my earlier advice..
2) Refrain from enclosing part of the subject in
delimiters like '['/']'.  Google Groups (which I
am not implying does 'the right thing') will not
display any such words in the title as shown on
the group page.
3) Do not assume that people who reply to a post
take any special notice of the subject, *always*
include all relevant parts of the problem in the
body.

But to your question.

A (javax.swing.)JComboBox constructor accepts
an array or Vector as an argument.  If you
keep a reference to the array or Vector, you
can perform all the normal operations such
structures offer, and that should provide
the functionality (as I vaguely recall)
that is needed.

Andrew T.
Mariano - 26 Mar 2007 14:22 GMT
> In addition to my earlier advice..
> 2) Refrain from enclosing part of the subject in
[quoted text clipped - 18 lines]
>
> Andrew T.

so there's no an existing method to do that. I've to do thease
operations directly on array of element. Right?
Andrew Thompson - 26 Mar 2007 14:35 GMT
(please trim replies)

> > A (javax.swing.)JComboBox constructor accepts
> > an array or Vector as an argument.  ...
...
> so there's no an existing method to do that.

Not to my knowledge.

>..I've to do thease
> operations directly on array of element. Right?

Not necessarily.  You might retrieve the
values back from the JCB and check each
one individually in a loop.  That would
end up being much the same as if an array
was used, but Vector has the indexOf method
that at least does the job in a single code
line.

I note in your next reply you mention using
a DefaultComboBoxModel - that class also has
getIndexOf(Object) that sounds useful.  Just
hold a reference to the DCBM, and query it
directly.

Even easier (no need to hold a reference)..
 theComboBox.getModel().getIndexOf("46");
..might do what is needed.

HTH

Andrew T.
Bent C Dalager - 26 Mar 2007 15:17 GMT
>Even easier (no need to hold a reference)..
>  theComboBox.getModel().getIndexOf("46");
>..might do what is needed.

Some care is needed here to get the data type correct. In the OP's
case, I'm guessing that
theComboBox.getModel().getIndexOf(new Integer(46));
would be more to the point.

Or, of you trust autoboxing enough,
theComboBox.getModel().getIndexOf(46);

(Also, some necessary casting to DefaultComboBoxModel is left out.)

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Mariano - 26 Mar 2007 18:15 GMT
> In article <1174916111.565152.257...@o5g2000hsb.googlegroups.com>,
>
[quoted text clipped - 17 lines]
> Bent Dalager - b...@pvv.org -http://www.pvv.org/~bcd
>                                     powered by emacs

All solutions posted gave me the error:
symbol  : method getIndexOf(java.lang.Integer)
location: interface javax.swing.ComboBoxModel
       System.out.println(cbxReparto.getModel().getIndexOf(new
Integer (3)));
1 error

What do you mean about 'some necessary casting'?
Mariano - 26 Mar 2007 14:25 GMT
> In addition to my earlier advice..
> 2) Refrain from enclosing part of the subject in
[quoted text clipped - 18 lines]
>
> Andrew T.

PS: JComboBox with DefaultComboBoxModel
Gordon Beaton - 26 Mar 2007 14:54 GMT
> 2) Refrain from enclosing part of the subject in
> delimiters like '['/']'.  Google Groups (which I
> am not implying does 'the right thing') will not
> display any such words in the title as shown on
> the group page.

Nonsense.

Subject tags (short keywords in []) are common in Usenet, and Google
groups handles them just fine (not that that should make any
difference either way).

Search for "subject tags" to find many examples and even instructions
on their use in various newsgroups. They may not be common here but
they certainly do no harm.

/gordon

--
Andrew Thompson - 26 Mar 2007 17:31 GMT
> > 2) Refrain from enclosing part of the subject in
> > delimiters like '['/']'.  Google Groups (which I
[quoted text clipped - 3 lines]
>
> Nonsense.

Find the post with subject line..
 [secret] subject line
<http://groups.google.com/group/alt.test/topics>
(Using GG, of course)

Andrew T.
Gordon Beaton - 26 Mar 2007 18:29 GMT
> Find the post with subject line..
>   [secret] subject line
><http://groups.google.com/group/alt.test/topics>
> (Using GG, of course)

Your newsreader is apparently broken - get one that isn't instead of
expecting others to cater to your whims.

/gordon

--
Andrew Thompson - 26 Mar 2007 18:52 GMT
> > Find the post with subject line..
> >   [secret] subject line
> ><http://groups.google.com/group/alt.test/topics>
> > (Using GG, of course)
>
> Your newsreader is apparently broken -

Sure.  Feel free to ignore the fact that
your claims were *wrong*, instead go
directly to 'attack mode'.

>...get one that isn't instead of
> expecting others to cater to your whims.

'Get stuffed Gordon.'*

Not everybody has the luxury of being
able to run a news client.  My comment
was designed to assist the OP to get
better help from *people* *in* *general*,
whether those people are posting through
GG, JavaKB (...), or a news client.

* Normally I do not issue commands, but
'like for like'.

Andrew T.
Mariano - 26 Mar 2007 19:00 GMT
I've not understand what's your problem...
Yes, i'm not using newsreader where is the problem??? :|
Andrew Thompson - 26 Mar 2007 19:43 GMT
> I've not understand what's your problem...

Did you try finding the post I mentioned
to Gordon?

> Yes, i'm not using newsreader where is the problem??? :|

Problem 1 is that sticky '?' key, please
fix that.  Problem 2 is that (as I mentioned
earlier), the post with subject
 '[something] about a problem'
..reads as..
 'about a problem'
..for some readers.

My point is that if you want all words of
a subject line to appear for all readers,
the most reliable way to achieve that is to
put the subject as ..
 'something about a problem'
..and leave out the (mostly redundant) '['/']'.

This problem may be *caused* by Google
Groups (and possibly other web interfaces
to usenet, maybe even some news readers),
but it *becomes* the problem of whoever
wants to communicate something clearly to
the widest audience.

Do you have any problem with *not*
including brackets in the subject
lines of future posts?

Andrew T.
Gordon Beaton - 26 Mar 2007 19:16 GMT
> * Normally I do not issue commands, but
> 'like for like'.

Are you joking? That's pretty much all you do here.

/gordon

--
Michael Rauscher - 27 Mar 2007 06:20 GMT
>> * Normally I do not issue commands, but
>> 'like for like'.
>
> Are you joking? That's pretty much all you do here.

Perhaps you should re-read Andrew's postings. The word /please/
indicates a request rather than a command. Andrew uses this word regularely.

One shouldn't consider that Andrew's intention is to attack someone. If
one pays attention on the content of his postings it should get clear
that he gives hints on how to post to newsgroups. By following this
hints one can get maximum benefit.

On this thread Andrew pointed out that Google will hide the part of the
subject which is enclosed in brackets. So, where's the problem?

On other threads, multi-posting, top-posting, full-quoting is an issue.
If possible, I don't answer to multi-postings (in fact, I don't even
like to answer cross-postings without follow ups). And I hate
full-quotings since often they're unreadable and thus they steal my time.

Of course, no one needs to follow anybody's advices. Anybody should feel
free to multi-post, top-post, full-quote, ... But no one should be
amazed if there'll be no or just a few replies then.

Bye
Michael


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.