Solved

Image as a JButton

Posted on 2010-11-09
9
498 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 41
tomcat administrtor 12 47
spring jars download 1 27
Using VBscript & RegEx, how can I match lines starting with Oracle word 17 59
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

778 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