File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to add a component to a Jcheckbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add a component to a Jcheckbox" Watch "How to add a component to a Jcheckbox" New topic
Author

How to add a component to a Jcheckbox

Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23
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
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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
Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23
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

Joined: Jun 09, 2003
Posts: 4632
> 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
Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23


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

Joined: Oct 27, 2005
Posts: 19541
    
  16

How about you UseCodeTags? Use the edit button to add them.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this

Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23
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

Joined: Jun 09, 2003
Posts: 4632
> 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
Sravanthi Macharla
Greenhorn

Joined: Sep 12, 2009
Posts: 23
Sorry for asking again..
i tried with GridBagLayout by using GridBagLayoutConstaints. But still could not achive the requirement. Please help me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add a component to a Jcheckbox
 
Similar Threads
Apply 2d array of answer to the right position of JTextField grid with JCheckBox
Clear Screen(reset Component)
How to mark checked CheckBox in JTree only when user click on CheckBox?
JtableHeader with two separate listeners, one for each component : classic label and extra checkbox
How to checked CheckBox in JTree only when user click on CheckBox?