?
Solved

Image as a JButton

Posted on 2010-11-09
9
Medium Priority
?
506 Views
Last Modified: 2012-05-10
I am still not able to  make a image a JButton after all the help in previous post.

Thanks to my poor Java Knowledge.

Can some one please help more on this ? May be some little code snippet.

I am trying to make a image act as a JButton and not to insert a image in a button.

Thanks !
0
Comment
Question by:hipal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 34099582
0
 
LVL 92

Expert Comment

by:objects
ID: 34099900
thats going to be a fair bit of work to do properly.
far easier to use existing JButton class
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34100379
Can you attach an image you'd like to use?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hipal
ID: 34102012
Thanks everyone.

@CEHJ, Please find the pic attached. Or you can just use any pic.

I have to make a lot of buttons using different pics.
pic.png
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1000 total points
ID: 34102034
you can simply use JButton/JLabel and set this as icon with empty border and it will just seem like an image in the UI!
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 34104484
Try the following
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    private void setGui() {
	try {
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container cp = getContentPane();
	    cp.setLayout(new FlowLayout());
	    JButton b = new JButton();
	    b.setIcon(new ImageIcon("pic.png"));
	    b.setBorder(null);
	    b.setContentAreaFilled(false);
	    cp.add(b);
	    b.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    System.out.println("Clicked");
		}
	    });
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public static void main(String[] args) {
	try {
	    SwingUtilities.invokeAndWait(new Runnable() {
		public void run() {
		    F f = new F();
		    f.setGui();
		    f.pack();
		    f.setVisible(true);
		}
	    });
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34104809
>>you can simply use JButton/JLabel and set this as icon with empty border

An empty border isn't sufficient actually
0
 

Author Comment

by:hipal
ID: 34104889
yes, we need to set the contentfillarea too .. :(

I just realized that ksiv.suggested just to make border zero ..

Anyways ....
0
 
LVL 92

Expert Comment

by:objects
ID: 34106946
that what I had already suggested ;)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Make the most of your online learning experience.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question