How to set multiple images on a button?
5 posters
Page 1 of 1
How to set multiple images on a button?
i want to set multiple image on a button in swing. How can i do it??
Like: [image] text label [image] text label on a button.
Like: [image] text label [image] text label on a button.
BIT0216-Habib- Administrator-RC
- Course(s) :
- BIT
Blood Group : O+
Posts : 217
Points : 458
Re: How to set multiple images on a button?
Nice, you can customize JButton, I mean you can make your own Button. You can use HTML code in your button.
Have a look--
Now you can use this MyButton as your own Button. All functionality will be same as JButton.
Cheers!
Have a look--
- Code:
public class MyButton extends JButton {
private String firstName, firstImageSource, lastName, lastImageSource;
public MyButton(String firstName, String firstImageSource, String lastName,
String lastImageSource) {
this.firstName = firstName;
this.firstImageSource = firstImageSource;
this.lastName = lastName;
this.lastImageSource = lastImageSource;
String str = makeMyButton();
setText(str);
}
private String makeMyButton() {
StringBuilder string = new StringBuilder();
string.append("<html>").append("<body>").append("<p>")
.append(firstName).append(" <img src=\"").append(
firstImageSource).append("\"").append("/>").append(" ")
.append(lastName).append(" <img src=\"")
.append(lastImageSource).append("\"").append("/>").append(
"</p>").append("</body>").append("</html>");
return string.toString();
}
}
Now you can use this MyButton as your own Button. All functionality will be same as JButton.
Cheers!
Re: How to set multiple images on a button?
What does "StringBuilder" class do?? I mean why we are not using only "String"?
BIT0107-Toma- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 280
Points : 453
Re: How to set multiple images on a button?
Hmm you can use String here, but using StringBuilder is a better idea.
Reason:
String is an immutable object, once constructed they cannot be changed anymore.That means, whenever you "change" the value of a String, you create a new object basically and make that variable reference this new object. Appending a String to another existing one is the same kind of deal and here old object is always dropped.
So for making bigger string by concatenation means there is more to copy and more garbage is produced that is less performing.
So Here StringBuilder is good choice. Thats why I use it.
StringBuilder is mutable object. So it is better to use in this type of situation as here we have no security issues.
I think, now you are clear.
Reason:
String is an immutable object, once constructed they cannot be changed anymore.That means, whenever you "change" the value of a String, you create a new object basically and make that variable reference this new object. Appending a String to another existing one is the same kind of deal and here old object is always dropped.
So for making bigger string by concatenation means there is more to copy and more garbage is produced that is less performing.
So Here StringBuilder is good choice. Thats why I use it.
StringBuilder is mutable object. So it is better to use in this type of situation as here we have no security issues.
I think, now you are clear.
Re: How to set multiple images on a button?
Nice idea. I gave two bad ideas when Habib asked me this personally. This idea is far better (possibly the best) than mine.
I have one point of observation:
Dont you think the appends should be in separate lines? The code does not look CLEAN!
I have one point of observation:
- Code:
string.append("<html>").append("<body>").append("<p>")
.append(firstName).append(" <img src=\"").append(
firstImageSource).append("\"").append("/>").append(" ")
.append(lastName).append(" <img src=\"")
.append(lastImageSource).append("\"").append("/>").append(
"</p>").append("</body>").append("</html>");
Dont you think the appends should be in separate lines? The code does not look CLEAN!
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: How to set multiple images on a button?
Another thing. I just checked the source of String builder. I think we should use it when we know the possible size of the string we will need. Because it starts with a size of 16 by default, and internally reinitializes the array when it overflows. Just check the source, you will get it.
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: How to set multiple images on a button?
yup, just noticed..
thanks.
and about separated line : eclipse code formatter does this thing, I'm not responsible
thanks.
and about separated line : eclipse code formatter does this thing, I'm not responsible
Last edited by BIT0112-Rokon on Fri Mar 04, 2011 2:33 am; edited 1 time in total
Re: How to set multiple images on a button?
I think he meant that the appending should be done separately instead of using a single statement.
Like this:
Edit:
BTW, Habib told me some days ago that he was not able use multiple images even though he used the code correctly.
I checked that out, and it appeared as though the html code was not able to find/use the image in button. IF I remember correctly, I told him to use absolute path, but that didn't work as well.
Can you please provide a working example with instructions about placement of images in directory? Also, is there any restriction on image type?
Like this:
- Code:
string.append("<html>");
string.append("<body>");
string.append("<p>");
Edit:
BTW, Habib told me some days ago that he was not able use multiple images even though he used the code correctly.
I checked that out, and it appeared as though the html code was not able to find/use the image in button. IF I remember correctly, I told him to use absolute path, but that didn't work as well.
Can you please provide a working example with instructions about placement of images in directory? Also, is there any restriction on image type?
Re: How to set multiple images on a button?
I'm not sure, why its not working,it should work.
Btw here is another version of code.
you can download my eclipse project with a demo : CLICK HERE
Note : You will notice that there is no variable named model in this code, but still its working.
But I should write something like
ButtonModel model = getModel();
actually getModel() return a model that is declared in AbstractButton class.
as it is in there, so I didn't declare it again here. Im not sure, its a good idea or not. probably not. Its kind of my experiment.
Btw here is another version of code.
you can download my eclipse project with a demo : CLICK HERE
Note : You will notice that there is no variable named model in this code, but still its working.
But I should write something like
ButtonModel model = getModel();
actually getModel() return a model that is declared in AbstractButton class.
as it is in there, so I didn't declare it again here. Im not sure, its a good idea or not. probably not. Its kind of my experiment.
Re: How to set multiple images on a button?
It works. I'am very happy to have that i wanted.
but i have one question to ask: What is the use of GradientPaint??
and it would be very much helpful for me if there have some comments in that code(for MyCustomButton class) so that i understand how it works.
anyway thanks to Rokon vai for his kind help.
but i have one question to ask: What is the use of GradientPaint??
and it would be very much helpful for me if there have some comments in that code(for MyCustomButton class) so that i understand how it works.
anyway thanks to Rokon vai for his kind help.
BIT0216-Habib- Administrator-RC
- Course(s) :
- BIT
Blood Group : O+
Posts : 217
Points : 458
Re: How to set multiple images on a button?
Hmm... Did you ask that before you read the java doc or after reading javadoc?
BTW, when you will mature, you will learn that commenting is bad
Confused? Comments are supposed to explain what the code does, right?
Well, better programmers code in such a way that the code itself becomes self explanatory
BTW, when you will mature, you will learn that commenting is bad
Confused? Comments are supposed to explain what the code does, right?
Well, better programmers code in such a way that the code itself becomes self explanatory
Similar topics
» Report Button : A button that should be used by All members
» Multiple Terminal in One Window
» Run a single programm using multiple PCs(mpi linux)
» Multiple IM Chatting softwares for mobiles
» Projects in IITDU forum: Multiple Sections or One?
» Multiple Terminal in One Window
» Run a single programm using multiple PCs(mpi linux)
» Multiple IM Chatting softwares for mobiles
» Projects in IITDU forum: Multiple Sections or One?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford