6 Replies - 514 Views - Last Post: 03 August 2013 - 10:32 AM Rate Topic: -----

#1 madjass  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 36
  • Joined: 28-September 12

question: add a pic on a button java swing

Posted 02 August 2013 - 06:03 PM

hi,
i trying to insert a pic on a button but only text that came out. do i need to re-size the pic or the problem is to find the pic location???

the pic location is at the same folder as the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class TButton implements ActionListener {
			JLabel jlabel;
			JButton jbutton1;
			JButton jbutton2;
			
			TButton(){
			ImageIcon licon = new ImageIcon("Lrrw.jpeg");
			ImageIcon ricon = new ImageIcon("Rrrw.jpeg");
			// create a new JFrame container
			JFrame jfrm = new JFrame("A Button Example");
			
			//specify FlowLayout for the layout manager
			jfrm.getContentPane().setLayout(new FlowLayout());
			
			//give the frame an initial size
			jfrm.setSize(500,400);
			
			// terminate the program when the user closes the application
			jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			//Make two buttons
			 jbutton1 = new JButton(" First ", licon);
			 jbutton2 = new JButton(" Second ", ricon);
			
			// add action listeners
			jbutton1.addActionListener(this);
			jbutton2.addActionListener(this);
			
			// add the buttons to the content pane
			jfrm.getContentPane().add(jbutton1);
			jfrm.getContentPane().add(jbutton2);
			
			// create a base text-based label
			jlabel = new JLabel("this is my first webBrowser");
			
			//add the label to the frame
			jfrm.getContentPane().add(jlabel);
			
			// display the frame
			jfrm.setVisible(true);
			}
			
			//handle buttons events
			public void actionPerformed(ActionEvent ae){
			
			if (ae.getActionCommand().equals(" First "))
				jlabel.setText(" First button was pressed ");
				else
				jlabel.setText(" Second button was pressed ");
				}
				
				public static void main (String[] args){
		
		javax.swing.SwingUtilities.invokeLater(new Runnable(){
				public void run() {
						new TButton();
						}
						});
			} 


Is This A Good Question/Topic? 0
  • +

Replies To: question: add a pic on a button java swing

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: question: add a pic on a button java swing

Posted 02 August 2013 - 06:32 PM

EDIT: Sorry. I just noticed that you wrote that the images are in the same location as the .java files. ImageIcon may be loading the image asynchronously.

Do this

 BufferedImage licon;

try {
    licon = ImageIo.read(getClass().getResource("Lrrw.jpeg"));
}
catch(IOException e) {
    e.printStackTrace();
}



Now try creating the buttons.

jButton button1 = new jButton("text", new ImageIcon(licon));



wow. I am so off my game tonight.

This post has been edited by farrell2k: 02 August 2013 - 06:48 PM

Was This Post Helpful? 2
  • +
  • -

#3 madjass  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 36
  • Joined: 28-September 12

Re: question: add a pic on a button java swing

Posted 02 August 2013 - 06:38 PM

View Postfarrell2k, on 02 August 2013 - 06:32 PM, said:

Probably just the location of the image. Put it into the same package as your .java files.


The pics are located in the same package
Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10366
  • View blog
  • Posts: 38,403
  • Joined: 27-December 08

Re: question: add a pic on a button java swing

Posted 02 August 2013 - 06:39 PM

I don't see where you setIcon() on the JButtons. That looks like your problem.
Was This Post Helpful? 3
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: question: add a pic on a button java swing

Posted 02 August 2013 - 06:45 PM

He's doing it in the JButton constructor. I updated my post.
Was This Post Helpful? 3
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10366
  • View blog
  • Posts: 38,403
  • Joined: 27-December 08

Re: question: add a pic on a button java swing

Posted 02 August 2013 - 06:51 PM

I missed that! Good catch!
Was This Post Helpful? 3
  • +
  • -

#7 madjass  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 36
  • Joined: 28-September 12

Re: question: add a pic on a button java swing

Posted 03 August 2013 - 10:32 AM

View Postfarrell2k, on 02 August 2013 - 06:32 PM, said:

EDIT: Sorry. I just noticed that you wrote that the images are in the same location as the .java files. ImageIcon may be loading the image asynchronously.

Do this

 BufferedImage licon;

try {
    licon = ImageIo.read(getClass().getResource("Lrrw.jpeg"));
}
catch(IOException e) {
    e.printStackTrace();
}




did you contruct your bufferedimage class right??? i did a lot google search especialy on buffered class and something might be missing. even on my code i getting an error that it cant find the symbol Buffered class.

Now try creating the buttons.

jButton button1 = new jButton("text", new ImageIcon(licon));



wow. I am so off my game tonight.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1