IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How to set multiple images on a button?

5 posters

Go down

Java How to set multiple images on a button?

Post by BIT0216-Habib Sun Feb 27, 2011 5:16 pm

i want to set multiple image on a button in swing. How can i do it?? Question

Like: [image] text label [image] text label on a button.
BIT0216-Habib
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0112-Rokon Sun Feb 27, 2011 7:25 pm

Nice, you can customize JButton, I mean you can make your own Button. You can use HTML code in your button.
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!
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0107-Toma Sun Feb 27, 2011 9:40 pm

What does "StringBuilder" class do?? I mean why we are not using only "String"?
BIT0107-Toma
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0112-Rokon Mon Feb 28, 2011 2:36 am

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.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0102-Mohaimin Fri Mar 04, 2011 12:51 am

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:
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
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0102-Mohaimin Fri Mar 04, 2011 12:58 am

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
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0112-Rokon Fri Mar 04, 2011 1:49 am

yup, just noticed..

thanks.

and about separated line : eclipse code formatter does this thing, I'm not responsible Razz


Last edited by BIT0112-Rokon on Fri Mar 04, 2011 2:33 am; edited 1 time in total
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0122-Amit Fri Mar 04, 2011 2:29 am

I think he meant that the appending should be done separately instead of using a single statement.

Like this:
Code:

string.append("<html>");
string.append("<body>");
string.append("<p>");
even better, if functions were introduced where the value of attributes were passed as parameters Smile

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?
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0112-Rokon Fri Mar 04, 2011 7:52 am

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.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0216-Habib Fri Mar 04, 2011 10:38 pm

It works. I'am very happy to have that i wanted. Banana Dancing

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
BIT0216-Habib
Administrator-RC

Course(s) :
  • BIT

Blood Group : O+
Posts : 217
Points : 458

Back to top Go down

Java Re: How to set multiple images on a button?

Post by BIT0122-Amit Fri Mar 04, 2011 10:59 pm

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 Razz

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 Smile
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: How to set multiple images on a button?

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum