2

why in below code the buttons in the JFrame doesn't appear? everything is ok since it is the same as the tutorial I am studying. even I have copied and pasted the source code from the tutorial but it doesn't work

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;


public class BorderLayoutExampleII extends JFrame {


    public BorderLayoutExampleII() {

        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");

        menubar.add(file);
        setJMenuBar(menubar);

        JToolBar toolbar = new JToolBar();
        toolbar.setFloatable(false);

        ImageIcon exit = new ImageIcon("exit.png");
        JButton bexit = new JButton(exit);
        bexit.setBorder(new EmptyBorder(0 ,0, 0, 0));
        toolbar.add(bexit);

        add(toolbar, BorderLayout.NORTH);

        JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
        vertical.setFloatable(false);
        vertical.setMargin(new Insets(10, 5, 5, 5));

        ImageIcon select = new ImageIcon("drive.png");
        ImageIcon freehand = new ImageIcon("computer.png");
        ImageIcon shapeed = new ImageIcon("printer.png");

        JButton selectb = new JButton(select);
        selectb.setBorder(new EmptyBorder(3, 0, 3, 0));

        JButton freehandb = new JButton(freehand);
        freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
        JButton shapeedb = new JButton(shapeed);
        shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));

        vertical.add(selectb);
        vertical.add(freehandb);
        vertical.add(shapeedb);

        add(vertical, BorderLayout.WEST);

        add(new JTextArea(), BorderLayout.CENTER);

        JLabel statusbar = new JLabel(" Statusbar");
        statusbar.setPreferredSize(new Dimension(-1, 22));
        statusbar.setBorder(LineBorder.createGrayLineBorder());
        add(statusbar, BorderLayout.SOUTH);

        setSize(350, 300);
        setTitle("BorderLayout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                BorderLayoutExampleII ex = new BorderLayoutExampleII();
                ex.setVisible(true);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mustafa Shujaie
  • 1,447
  • 1
  • 16
  • 30
  • 2
    Hopefully, this [answer](http://stackoverflow.com/a/9866659/1057230) of mine, might be able to help you in this direction :-) Do watch the last link for better clarification, on the topic. – nIcE cOw May 12 '13 at 15:54
  • 2
    @nIcEcOw thanks man the answer is to use `new ImageIcon(getClass().getResource("drive.png"))`. – Mustafa Shujaie May 12 '13 at 16:02
  • I guess you must visit this [info-page](http://stackoverflow.com/tags/embedded-resource/info) of [embedded-resources](http://stackoverflow.com/questions/tagged/embedded-resource), for more clarification on `first forward slash` before the path :-) – nIcE cOw May 12 '13 at 18:29
  • 1
    And one more [link](http://stackoverflow.com/a/16487108/1057230) – nIcE cOw May 12 '13 at 18:35

2 Answers2

0

check the the file exit.png is in the same folder as your program is or not. You can make sure there is problem in loading image by removing imageicon argument from button, if button is showing then check exit.png files existance

Sudhir Dhumal
  • 902
  • 11
  • 22
  • the images are in the same directory as the class source code file. also I removed the icon from the button and this time I ran program without any Icon but still buttons are not displayed – Mustafa Shujaie May 12 '13 at 15:36
0

Have a look following code where I commented all the imageIcon's and replaced them by simple strings and I got the out put screen

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class BorderLayoutExampleII extends JFrame {

    public BorderLayoutExampleII() {

        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");

        menubar.add(file);
        setJMenuBar(menubar);

        JToolBar toolbar = new JToolBar();
        toolbar.setFloatable(false);

        // ImageIcon exit = new ImageIcon("exit.png");
        JButton bexit = new JButton("Exit");
        bexit.setBorder(new EmptyBorder(0, 0, 0, 0));
        toolbar.add(bexit);

        add(toolbar, BorderLayout.NORTH);

        JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
        vertical.setFloatable(false);
        vertical.setMargin(new Insets(10, 5, 5, 5));

        // ImageIcon select = new ImageIcon("drive.png");
        // ImageIcon freehand = new ImageIcon("computer.png");
        // ImageIcon shapeed = new ImageIcon("printer.png");

        JButton selectb = new JButton("Select");
        selectb.setBorder(new EmptyBorder(3, 0, 3, 0));

        JButton freehandb = new JButton("Freehand");
        freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
        JButton shapeedb = new JButton("shapeed");
        shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));

        vertical.add(selectb);
        vertical.add(freehandb);
        vertical.add(shapeedb);

        add(vertical, BorderLayout.WEST);

        add(new JTextArea(), BorderLayout.CENTER);

        JLabel statusbar = new JLabel(" Statusbar");
        statusbar.setPreferredSize(new Dimension(-1, 22));
        statusbar.setBorder(LineBorder.createGrayLineBorder());
        add(statusbar, BorderLayout.SOUTH);

        setSize(350, 300);
        setTitle("BorderLayout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                BorderLayoutExampleII ex = new BorderLayoutExampleII();
                ex.setVisible(true);
            }
        });
    }
}
Sudhir Dhumal
  • 902
  • 11
  • 22