Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create Jcombobox with fixed width?

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create Jcombobox with fixed width ? (irrespective of its elements width)
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
couple of things to try

comboBox.setPreferredSize(...)
comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXX");
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply...

Its working, but the contents of the JComboBox are partially visible, is there any way to display the whole content...
I mean JComboBox width is fixed but its content should expand w.r.t contents length
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comboBox.setPrototypeDisplayValue(widestItem);
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply...i have done this way
comboBox.setPreferredSize(new Dimension(10,20))
comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

but still the content is not visible completely..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preferred size 10, 20? You are lucky to see the combo box at all when it's that small!
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leave the size.....
let me know, whether it is possible to display the entire content or not
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comboBox.setPreferredSize(new Dimension(500,25));
OR//<---------------
comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

either, in conjunction with using a layoutManager, and both work fine

if they still don't work for you, post your code
 
Maneesh Godbole
Saloon Keeper
Posts: 11088
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhishek Reddy:
leave the size.....
let me know, whether it is possible to display the entire content or not


Abhiskek,

I don't think that was really nice on your part.
I understand, you might be rushed to get this thing done, or frustrated at not getting it right on your own, inspite of numerous attempts; all of us have been through that phase.

However, you need to remember, statements like these, might make people think twice before helping you out in the future. You need to be careful about such things buddy
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam sorry for that ....
thanks for the reply...............

here is the sample code



look for the codes inside //---------------------------//
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your problem is probably GroupLayout (which I don't use, and can't compile your code to confirm)

here's your code, stripped of all unnecessary stuff, leaving the frame as a default Borderlayout.

I've added another combobox (no prototype display), just for comparison,
and added a few extra XXX's to make it stand out


compile/run the code, note the differing widths, then comment out the
setPrototypeDisplayValue line,
recompile/rerun and you'll notice the 'aaaaaa...' item fully displayed,
so it does appear GroupLayout is causing all your problems
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...for the reply
i have understood the usage of setPrototypeDisplayValue(prototypeValue);

But i want to display the JComboBox in the following way

----------
select |
-------------------------
item1|
item2|
item3|
item4|
aaaaaaaaaaaaaaaaaaaaaaaa|
-------------------------
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------
select |
-------------------------
item1 |
item2 |
item3 |
item4 |
aaaaaaaaaaaaaaaaaaaaaaaa|
-------------------------
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I know what you really want now.

search the sun forums and google for SteppedComboBox,
it matches exactly your description.
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.......

SteppedCombo Box!!!

i will search for it......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic