Image component « Button « Java Swing Q&A





1. Java: Image as toggle button    stackoverflow.com

How can I create in a Swing interface a toggle image button? I have two images, imageon.jpg and imageoff.jpg,and I basically want a clickable element that toggles the images and fires an ...

2. How to change the image of a JButton?    stackoverflow.com

I'm working on a memory game program. I have 30 JButtons on a JPanel. When the user is clicking and finds a match (meaning two buttons with the same image) I ...

3. Why I see only part of the image in the button?    stackoverflow.com

I add an image to the button in the following way:

ImageIcon icon = new ImageIcon("images/col.jpg");
tmp = new JButton(icon);
My image is a red sphere but in my button I see only read ...

4. Is it possible to put text on top of a image in a button?    stackoverflow.com

I have .jpg images in my buttons. I also would like to put some text on top of the images. I use the following syntax for that:

JButton btn = new JButton(label,icon);
But ...

5. toggle button vs image changing button    stackoverflow.com

When to use this functionality and when to use the other? I personally thing that to switch between two different modes one should use a toggle button but i need to ...

6. Java: using an image as a button    stackoverflow.com

I would like to use an image as a button in Java, and I tried to do this:

BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath"));
button = new JButton(new ImageIcon(buttonIcon));
But this still shows the actual ...

7. moving an image via buttons    stackoverflow.com

I want to move my pictures via 2 buttons, my problem is with listener of the other button,, how can I make it work properly ??? this is the code and the pictures ...

8. Show Image when click a button    stackoverflow.com

I have a really short and simple cuestion. What must Y type on *.java file to show a png file on screen when I click on a button. I've been searching and trying ...

9. How to create a custom JButton in java with an image base?    stackoverflow.com

I recently read this thread (Creating a custom button in Java) on creating custom buttons in java by extending the JButton class, however all the solutions on this thread use ...





10. How to code a button with image    coderanch.com

Rose, Basically, all you have to do is extend Component, Button, Panel, Canvas... any of the AWT types you could use to draw an image on... you would send the image into your new class via the constructor, or a set method, and have the new component paint the image in it's paint() method... you would need to make it create ...

11. Image in place of buttons    coderanch.com

Hi, I wanna use image as buttons in place of simple buttons provided by java.awt package, in addition to this, I also wanna change mouse cursor to 'hand' whenever the mouse has come over that image. But dont know how to go about all this. Can anyone help me out !!! Regards and thanx in advance,

12. Help with an Image Button    coderanch.com

13. Image refreshing on JButton problem    coderanch.com

14. Image on the right side of a JButton    coderanch.com

15. Image and button    coderanch.com

Hi everyone, As I am studying a course related with Swing, one skill I need to know is how the click the small image on the left layout then the actual size of image would be shown on the right's. I think the key skill is to set the small image with Event Listener. That means the small image like a ...

16. Image on a JButton    coderanch.com

Hi Michael, I think I may have missed something pretty basic. I moved my images folder to the same directory as my classes, but it still didnt work. I created everything in JBuilder, which sometimes moves things about when you compile. I have a folder called "Malcolm/myprojecs/ficha", in which I have the .jpr file and MainFrame.java. Jbuilder created "Malcolm/myclasses/ficha", so I ...





17. Image instead of a button    coderanch.com

Hello everybody, I've a simple GUI and instead of the standard text appearing on the buttons, I would like to put an image.Could somebody please tell me how can i do that in the follwing piece of code. Many many thanks import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; public class MatlabRC extends JFrame implements ActionListener { private String StartEng = ...

18. how do you load images from a jar file for image buttons?    coderanch.com

I have an application with a pretty simple swing GUI that uses a few image JButtons [which use the ImageIcon as the JButton constructor argument, etc.]. When I had the classes for this GUI loose on the file system and the images loose as well in the same directory from which I ran the code [i.e. the directory from which I ...

19. how to streach an image all over the button    coderanch.com

import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import javax.imageio.ImageIO; import javax.swing.*; public class ScalingExample implements ActionListener { JButton left, right; public ScalingExample() { BufferedImage image = getImage(); left = new JButton(new ImageIcon(image)); left.addActionListener(this); right = new JButton(new ImageIcon(image)); right.addActionListener(this); } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if(button == left) scaleLeft(button); if(button == right) ...

21. add image into buttons etc    coderanch.com

22. Text on the JButton Image    coderanch.com

Yep. Hope this helps import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class StringOnIcon { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new JPanel()); ImageIcon icon = new ImageIcon("c:\\button.png"); JButton button = new JButton(icon) { @Override public void paint(Graphics graphic) { super.paint(graphic); Graphics2D g = (Graphics2D)graphic; FontMetrics ...

23. show image on button click    coderanch.com

Have you checked to see if your prog is reading in the image file ok?? You might want to slip in a call to see if the image file is being recognized as existing like so: public void actionPerformed(ActionEvent ae) { ImageIcon I1 = new ImageIcon(); String s1 = ((JButton)ae.getSource()).getText(); if (s1.equals("Classic")) { String pathName = "G:\\java cd1\\prgs\\Classic.jpg"; I1 = new ...

24. Jbutton Image    coderanch.com

Hey! 1.I'm pretty sure you can store the pictures anywhere as long as you put the full directory in the code. So, ImageIcon anImage = new ImageIcon("/home/ImageFiles/imageForCard.jpg"); But, if you want, you can store the image files in the same folder as your code is located and then you will only have to put the file's name. So, ImageIcon anImage = ...

25. Custom (image) button    coderanch.com

26. JButton with a round image    coderanch.com

27. Adding Image to a button    coderanch.com

I want to add an Image to a button once a button is pressed. To do that, i am using the following code: ImageIcon imageX = new ImageIcon("X.jpg"); button.setPressedIcon(imageX); The Problem is, whenever i am using setPressedIcon it does not display the image, but when i replace it by setIcon, it works.But i have to diable the button once it is ...

28. JButton Image Sprite    coderanch.com

G'day everyone - I've got an image sprite with four sections. I've also got four JButtons. The other day, I irretrievably deleted my piece of code which positioned the sprite as I wanted it in each button. Now, I've been looking for a while to find how I did it but can't for the life of me dig it up. Any ...

29. reduce gap between image and button    coderanch.com

30. Image lost behind buttons    coderanch.com

Class has moved onto adding buttons onto the window. Just prior to this we learnt to add an image via JComponent, then we added buttons onto a JPanel; the problem is that when I try to combine the two into a single window (my own desire, not part of the class) I completely lose whatever is added first. How can I ...

31. Swing : Blinking Button/images    coderanch.com

32. Linking 2 images with a button in a GUI    coderanch.com

Hi everyone, For my class my project is very simple and straight forward. I have to create a volleyball player in a GUI window. I have already done this but I wanted to take it a step further. I currently have the picture of the volleyball player getting ready to spike the volleyball over the net. Now what I want to ...

33. How to place image on JButton    coderanch.com

34. How to add images to a button    coderanch.com

It doesn't make sense to say "the" button because you have 64 buttons, not just one. But let's suppose you aren't seeing an icon on any of those 64 buttons. (Your description isn't very clear so it could mean other things, but let's start with that as a first guess.) Then that would mean your createImage() method is returning something which ...

35. Image as a button    coderanch.com

36. Null pointer exception when creating image button!    java-forums.org

The exception trace tells you the exact line numbers in your code of where the exception occurred. If you are having trouble using Netbeans then refer to the manual that came with your Netbeans. If you are new to Java then consider staying away from IDEs (especially Netbeans) until you understand at least the basics of Java.

37. AWT button image    java-forums.org

38. Help Needed - Creating an Array of Buttons with Images    java-forums.org

Hi all, Im new here and to Java, so please bear with me if I seem a little uneducated as my understanding of Java is very limited. I am undergoing a University project in Java SE and am at the end of my tether now - I simply cannot figure how to get this array working. My tutor is as useful ...

39. changing JButton image upon clicking    java-forums.org

Hello, I want to have a JButton's image change when it it clicked. I don't want it to just change for the click, but to remain the changed picture after the click. So I'm not looking for a method like "setPressedIcon." I would think that I could change the image by using setIcon in the action listener when the button is ...

40. image on a JButton...how??    java-forums.org

41. Fit image to JButton    forums.oracle.com

42. JButtons & Images    forums.oracle.com

Hi all, First time poster & novice java programmer. As part of a project I'm working on I have started to construct a java application. I am able to create basic JButtons and get them to function now with little difficulty however I was just wondering if anyone knew how to resize my JButtons to a preffered size. Also, I am ...

43. Replace a JButton with a Image    forums.oracle.com

I want to create a JFrame which holds a JButton. Theres no problem with put an Image on that JButton, but there is stil the border around the JButton, so it doesnt look very professional. I want to see only the image which is on the JButton. No border! Is there a way? How can I remove the border completely or ...

44. Dinamic image on JButton    forums.oracle.com

I declared the variables iconoa and iconob like ImageIcon with the path of one dinamic image and one static image. Thus, I thought that with the button listener, with the event mouseEntered, I could introduce the dinamic image, but when the user pass the mouse over the button, the dinamic image not appears, only disappears or on the screen appears like ...

45. How to make a Jbutton with an image?    forums.oracle.com

Hi, I would like to make a JButton with a non-rectangular image, it is actually in the shape of a person (like those on bathroom signs). But when I create a JButton with the transparent image icon, there's always the original button's grey background behind it. Is there any way to make a button that shows just the image itself?

46. Make JButton image using external class    forums.oracle.com

47. adding an image to a JButton    forums.oracle.com