[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Image as a JButton

Posted on 2010-11-09
9
Medium Priority
?
509 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 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