0

I am trying to add an image to a JButton and I'm not sure what I'm missing. When I run the following code the button looks exactly the same as if I had created it without any image attribute. Background.jpeg is in the root of my project folder.

this is the code:

package eg.edu.guc.dvonn.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import eg.edu.guc.dvonn.engine.Board;


public class FirstWindow extends JFrame implements ActionListener {

    JButton Startbutton;
    JPanel welcomePanel;
    JPanel SecondPanel;
    JPanel StandardPanel;
    JPanel custPanel;
    JPanel  panelFill;
    JButton Standard;
    JButton fill;
    JButton put;
    JButton cust;
    JLabel label;
    TextField rows;
    TextField col;
    Board b;
    JButton[][] button;
    int r1;
    int cc;


         public FirstWindow(){

             setSize(800,600);
             setVisible(true);
             setLayout(new BorderLayout());

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              welcomePanel = new JPanel();

            label  = new JLabel("Welcome to Dvonn");
            welcomePanel.add(label);
            welcomePanel.setVisible(true);

             label.setForeground(Color.RED);
             label.setFont(new Font("Serif", Font.PLAIN, 40));


             Startbutton = new JButton ("start");



             Startbutton.setBackground(Color.BLACK);
             Startbutton.setForeground(Color.RED);
             Startbutton.setPreferredSize(new Dimension(120,40));

             welcomePanel.add(Startbutton);
             add(welcomePanel);

             Startbutton.addActionListener(this);


             SecondPanel = new JPanel();
             SecondPanel.setVisible(false);
             SecondPanel.setSize(400,400);
              rows = new TextField();
              col= new TextField();
              SecondPanel.add(rows);
              SecondPanel.add(col);

              fill = new JButton("fill random");
              fill.addActionListener(this);
              fill.setBackground(Color.LIGHT_GRAY);
              fill.setForeground(Color.BLUE);


              put = new JButton("put");
              put.setBackground(Color.LIGHT_GRAY);
              put.setForeground(Color.BLUE);


              Standard = new JButton("Standardised board");
              Standard.setBackground(Color.LIGHT_GRAY);
              Standard.setForeground(Color.BLUE);
              Standard.addActionListener(this);


              cust = new JButton("customized");
              cust.setBackground(Color.LIGHT_GRAY);
              cust.setForeground(Color.BLUE);
              cust.addActionListener(this);

              panelFill = new JPanel();

              panelFill.add(fill);
              panelFill.add(put);
              SecondPanel.add(Standard);
              SecondPanel.add(cust);


              b = new Board(r1,cc);  
                this.r1 =5;
                this.cc = 11;
                button = new JButton [r1][cc];
                StandardPanel = new JPanel();
                StandardPanel.setBackground(Color.WHITE);
                StandardPanel.setLayout(new GridLayout(r1, cc));

                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {
                        button[i][j] = new JButton(); // i want to     add an image to it

                        button[i][j].addActionListener(this);
                    }
                }
                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {

                        button[i][j].setLayout(new FlowLayout());
                        button[i][j].setBackground(Color.WHITE);

                        StandardPanel.add(button[i][j], i, j);

                    }
                }



         }  

         public FirstWindow(int row,int cols){

             setSize(800,600);
             setVisible(true);
             setLayout(new BorderLayout());

             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


              fill = new JButton("fill random");
              fill.addActionListener(this);
              fill.setBackground(Color.LIGHT_GRAY);
              fill.setForeground(Color.BLUE);


              put = new JButton("put");
              put.setBackground(Color.LIGHT_GRAY);
              put.setForeground(Color.BLUE);


              Standard = new JButton("Standardised board");
              Standard.setBackground(Color.LIGHT_GRAY);
              Standard.setForeground(Color.BLUE);
              Standard.addActionListener(this);

              cust = new JButton("customized");
              cust.setBackground(Color.LIGHT_GRAY);
              cust.setForeground(Color.BLUE);
              cust.addActionListener(this);

              panelFill = new JPanel();
              panelFill.add(fill);
              panelFill.add(put);





            this.r1 = row;
            this.cc = cols;



                 b = new Board(r1,cc);  


                button = new JButton [r1][cc];

                custPanel = new JPanel();
                custPanel.setBackground(Color.WHITE);
                custPanel.setLayout(new GridLayout(r1, cc));

                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {
                        button[i][j] = new JButton(); // i want to     add an image to it
                        button[i][j].addActionListener(this);
                    }
                }
                for (int i = 0; i <= r1 - 1; i++) {
                    for (int j = 0; j <= cc - 1; j++) {

                        button[i][j].setLayout(new FlowLayout());
                        button[i][j].setBackground(Color.WHITE);
                        custPanel.add(button[i][j], i, j);

                    }
                }
                add(custPanel);
                add(panelFill,BorderLayout.SOUTH);


         }
         public static void main  (String [] args){
             FirstWindow window = new FirstWindow();



         }
        @Override
        public void actionPerformed(ActionEvent e) {

            if (e.getSource()==Startbutton){

                this.remove(welcomePanel);
                this.add(SecondPanel);
                SecondPanel.setVisible(true);
                //this.add(SecondPanel);

                JLabel label1 = new JLabel("dvonn");
                label1.setForeground(Color.RED);
                label1.setFont(new Font("Serif", Font.PLAIN, 25));
                SecondPanel.add(label1);

                }

                if(e.getSource()== cust){
                   String rowText = rows.getText();
                   String colText = col.getText();

                   r1 = Integer.parseInt(rowText);
                   cc = Integer.parseInt(colText);
                   this.setVisible(false);
                FirstWindow custom =new FirstWindow(r1, cc);




                 } 
                      if(e.getSource()==Standard){
                         SecondPanel.setVisible(false); // set the second panel invisible
                         this.add(StandardPanel); //to add the panel with the buttons
                         StandardPanel.setVisible(true);

                         add(panelFill,BorderLayout.SOUTH);
                         add(StandardPanel,BorderLayout.CENTER);

                     }



              /* Board b  = new Board(r1,c1);
                    b.fillRandom();*/

                //  }








            }
}           
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Tharwat7
  • 1
  • 1
  • 5

3 Answers3

1

You can generate an ImageIcon and set it as the button's icon using

JButton button; // precondition: not null
ImageIcon icon = new ImageIcon(FirstWindow.class.getResource("/Background.jpeg"));
button.setIcon(icon);
button.setDisabledIcon(icon); // or a grayed-out version

If that doesn't work, try creating a eg.edu.guc.dvonn.resources package and changing the second line to this:

ImageIcon icon = new ImageIcon(
    FirstWindow.class.getResource(
    "/eg/edu/guc/dvonn/resources/Background.jpeg"));

Make sure to refresh your Eclipse project before you run the code (click in the project manager, in the white space or on the project name, and press the F5 key, or right click → Refresh).

wchargin
  • 15,589
  • 12
  • 71
  • 110
  • @trashgod: but i've noticed that (when i tried in netbeans). the image is embedded with the size of the image and not with that of the button....How can we embedd the image in the size of the button..... – Arjun K P Jun 16 '12 at 18:32
  • @akp: It sounds like you need to scale the image to a convenient size, as suggested [here](http://stackoverflow.com/a/6916719/230513). – trashgod Jun 16 '12 at 21:39
0

see here http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

pbaris
  • 4,525
  • 5
  • 37
  • 61
0

I would recommend simplifying what you are doing and try this basic tutorial. If you get it working with the gif, then change it to your jpg. If that works, then you can replace the code with your simpler example.

Jbutton tutorial: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

James Oravec
  • 19,579
  • 27
  • 94
  • 160