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

M

Mariano

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?
 
A

Andrew Thompson

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.
 
M

Mariano

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.

so there's no an existing method to do that. I've to do thease
operations directly on array of element. Right?
 
M

Mariano

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.

PS: JComboBox with DefaultComboBoxModel
 
A

Andrew Thompson

On 26 Mar, 15:17, "Andrew Thompson" <[email protected]> wrote:

(please trim replies)
....
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.
 
G

Gordon Beaton

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

--
 
B

Bent C Dalager

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
 
M

Mariano

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

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'?
 
A

Andrew Thompson

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.
 
M

Mariano

I've not understand what's your problem...
Yes, i'm not using newsreader where is the problem??? :|
 
A

Andrew Thompson

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.
 
M

Michael Rauscher

Gordon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top