• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add a component to a Jcheckbox

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a requirement like..
i will have a JCheckbox having a component. That means i have some text next to my checkbox, which have some part in normal text and some part in bold.
can i achive this requirement. Please reply me if anyone knows about this problem.

I checked with all constructors, but all of them are taking string, not a component. I think if it takes a component, then we can achieve the requirement.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can set the text of the JCheckBox to show the text. Like many Swing components it has limited HTML support in its text. For example:
As soon as your text starts with <html> it will be treated as HTML.
 
Sri Palem
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Prime,

Thanks for your answer. The solution worked. Can i achieve it without html tags. Because in my requirement, i am displaying all these check boxes with names in a fixed width columns. If the checkbox label exceeds the width limit, It will show the label with ellipses and mouse hovering on the label will show the entire label as a tooltip. If i use html tags for displaying some of the text in bold, its not displaying the ellipses, instead it displaying the whole label in multiple rows. So can i have solution without html tags.

Thanks in Advance,
Sravanthi
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you already have a component that displays "which have some part in normal text and some part in bold"
just create a JCheckBox (no text), then add that checkbox to a JPanel, and your component to the panel,
using an appropriate layoutManager to display them side-by-side
 
Sri Palem
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried doing this , but it is loosing the feature of showing ellipses if the text size is more than the column width in which it is displaying. Please suggest me solution for this.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Please suggest me solution for this.

I can't - I do not know what component you are using to display your bold/normal text.

post the code you have tried
 
Sri Palem
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the above code if we comment adding one of the label, it will show the label in ellipses, if it is exceeding the width of the pane. but i wanted to display these 2 labels side by side and if it exceeds the length of the pane, it should display in ellipses. Please let me know if you understand the problem correctly.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about you UseCodeTags? Use the edit button to add them.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this

 
Sri Palem
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dunn,

Thanks for your answer, but here first label is also showing ellipses. Since we are showing these 2 labels as part of Checkbox, it should appear like a single label. we should not show ellipses in the middle of 2 labels. and if combination of the both the labels size increased, we have to show ellipses only at the end.

Thanks a lot
Sravanthi
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> but here first label is also showing ellipses

only because of the layoutManger I used.

I've shown you the basics, now experiment with the various other layoutManagers to get the effect you're after
 
Sri Palem
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for asking again..
i tried with GridBagLayout by using GridBagLayoutConstaints. But still could not achive the requirement. Please help me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic