Solved

Image as a JButton

Posted on 2010-11-09
9
496 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
  • 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 26

Accepted Solution

by:
ksivananth earned 250 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 250 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This is about my first experience with programming Arduino.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now