5

I am a begining programmer and am building a simple window with buttons and a scrollbar. When I compile my code, the text on my buttons is cut off with an elipsis and the image icon does not show. I have tried compiling it in both eclipse and NetBeans. To solve the problem I have tried

.setMargin(new Insets(0, 0, 0, 0));

.setPreferedSize

adding padding (I forgot the code for this)

.setBounds

and pretty much everything else that I have stumbled upon on the internet. None of these solved my problem and I cannot view the text and images in the buttons.

My code:

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

public class FeedBar2 extends JFrame {

    public FeedBar2() {
        super("FeedBar 2");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // create icons
        ImageIcon loadIcon = new ImageIcon("load.gif");
        ImageIcon saveIcon = new ImageIcon("save.gif");
        ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
        ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
        // create buttons
        JButton load = new JButton("Load", loadIcon);
        JButton save = new JButton("Save", saveIcon);
        JButton subscribe = new JButton("Subscribe", subscribeIcon);
        JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
        // add buttons to toolbar
        JToolBar bar = new JToolBar();
        bar.add(load);
        bar.add(save);
        bar.add(subscribe);
        bar.add(unsubscribe);
        // create menu
        JMenuItem j1 = new JMenuItem("Load");
        JMenuItem j2 = new JMenuItem("Save");
        JMenuItem j3 = new JMenuItem("Subscribe");
        JMenuItem j4 = new JMenuItem("Unsubscribe");
        JMenuBar menubar = new JMenuBar();   
        JMenu menu = new JMenu("Feeds");
        menu.add(j1);
        menu.add(j2);
        menu.addSeparator();
        menu.add(j3);
        menu.add(j4);
        menubar.add(menu);
        // prepare user interface
        JTextArea edit = new JTextArea(8, 40);
        JScrollPane scroll = new JScrollPane(edit);
        BorderLayout bord = new BorderLayout();
        setLayout(bord);
        add("North", bar);
        add("Center", scroll);
        setJMenuBar(menubar);
        pack();
        setVisible(true);
    }

    public static void main(String[] arguments) {
        FeedBar2 frame = new FeedBar2();
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user1590738
  • 51
  • 1
  • 2
  • +1 nice question, welcome on SO forum – mKorbel Aug 10 '12 at 16:25
  • Hopefully this [answer](http://stackoverflow.com/a/9866659/1057230) of mine will able to sort out the image issue for you. And here is one more [example](http://stackoverflow.com/a/11428289/1057230) for your quick reference :-) If you won't use IDE's, then this [answer](http://stackoverflow.com/a/11372350/1057230) might can tell you as to where to place your images with respect to your class files. – nIcE cOw Aug 10 '12 at 16:28

2 Answers2

4

There must be a problem with the locations of you images as this works just fine (URL image used):

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class FeedBar2 extends JFrame {

    public FeedBar2() {
        super("FeedBar 2");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // create icons
        ImageIcon loadIcon = null;
        try {
            loadIcon = new ImageIcon(new URL("http://t0.gstatic.com/images?q=tbn:ANd9GcRQgmCgdCMtXO6db7pX4UwzdvJY9-r8kI2zwE5A6c3VqB9eOR2Pe8gpqQBdeg"));
        } catch (MalformedURLException ex) {
            Logger.getLogger(FeedBar.class.getName()).log(Level.SEVERE, null, ex);
        }
        // create buttons
        JButton load = new JButton("load", loadIcon);
        // add buttons to toolbar
        JToolBar bar = new JToolBar();
        bar.add(load);
        BorderLayout bord = new BorderLayout();
        setLayout(bord);
        add("North", bar);
        pack();
        setVisible(true);
    }

    public static void main(String[] arguments) {
        FeedBar2 frame = new FeedBar2();
    }
}

So are the pictures located in the same directory as the jar (in netbeans they can be located in the main project directory)? If the are located in the jar you will need to exatract them using: getResourceAsStream with the correct path to package which contains the images relative to the current package.

I would recommend always packing your images with your jars, less problems (to do this on netbeans just drag and drop the images in your current package of the class which will access them then simple use the exact name (case sensitive) to retrieve it using the getResourceAsStream

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    +1 for the JAR part :-), never used `getResourceAsStream()`, but got some tips today regarding the same from you :-) – nIcE cOw Aug 10 '12 at 17:02
4
  • do you mean

enter image description here

from code

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

public class FeedBar2 extends JFrame {

    private static final long serialVersionUID = 1L;
    private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon subscribeIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon unsubscribeIcon = UIManager.getIcon("OptionPane.questionIcon");

    public FeedBar2() {
        super("FeedBar 2");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // create icons
        /*ImageIcon loadIcon = new ImageIcon("load.gif");
        ImageIcon saveIcon = new ImageIcon("save.gif");
        ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
        ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");*/
        // create buttons
        JButton load = new JButton("Load", loadIcon);
        JButton save = new JButton("Save", saveIcon);
        JButton subscribe = new JButton("Subscribe", subscribeIcon);
        JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
        // add buttons to toolbar
        JToolBar bar = new JToolBar();
        bar.add(load);
        bar.add(save);
        bar.add(subscribe);
        bar.add(unsubscribe);
        // create menu
        JMenuItem j1 = new JMenuItem("Load");
        JMenuItem j2 = new JMenuItem("Save");
        JMenuItem j3 = new JMenuItem("Subscribe");
        JMenuItem j4 = new JMenuItem("Unsubscribe");
        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("Feeds");
        menu.add(j1);
        menu.add(j2);
        menu.addSeparator();
        menu.add(j3);
        menu.add(j4);
        menubar.add(menu);
        // prepare user interface
        JTextArea edit = new JTextArea(8, 40);
        JScrollPane scroll = new JScrollPane(edit);
        BorderLayout bord = new BorderLayout();
        setLayout(bord);
        add("North", bar);
        add("Center", scroll);
        setJMenuBar(menubar);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] arguments) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                FeedBar2 frame = new FeedBar2();
            }
        });
    }
}
Dimitar
  • 4,402
  • 4
  • 31
  • 47
mKorbel
  • 109,525
  • 20
  • 134
  • 319