Why can't I see the images in the buttons?
I am writing this flashcard application where there's a toolbar panel at the bottom. The toolbar contains buttons with images and text in them. When the application runs, the text is visible but not the images. Where am I going wrong?
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;
import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
public class Flashcards implements ActionListener {
JFrame window;
JLabel statusLabel;
//Constructor
public Flashcards()
{
createUserInterface();
}
//Public Methods
//Private Methods.
private void createUserInterface()
{
//Basic UI
window = new JFrame("Flashcards");
window.setSize(600,300);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JPanel mainPanel = new JPanel();
window.getContentPane().add(mainPanel, BorderLayout.CENTER);
statusLabel = new JLabel("Label");
mainPanel.add(statusLabel);
JPanel toolbar = new JPanel();
//toolbar layout and buttons.
window.getContentPane().add(toolbar, BorderLayout.SOUTH);
JMenuBar menubar = new JMenuBar();
window.setJMenuBar(menubar);
//The toolbar and the grouplayout
Icon[] arrayOfButtonImages = {new ImageIcon("/Images/Open.png")};
JButton btnOpenList = new JButton("Open", arrayOfButtonImages[0]);
btnOpenList.setVerticalTextPosition(AbstractButton.CENTER);
btnOpenList.setHorizontalTextPosition(AbstractButton.CENTER);
btnOpenList.setActionCommand("Open");
btnOpenList.addActionListener(this);
GroupLayout toolbarLayout = new GroupLayout(toolbar);
toolbar.setLayout(toolbarLayout);
toolbarLayout.setHorizontalGroup(
toolbarLayout.createSequentialGroup()
.addComponent(btnOpenList);
toolbarLayout.setVerticalGroup(
toolbarLayout.createParallelGroup()
.addComponent(btnOpenList);
}
public void actionPerformed(ActionEvent event)
{
System.out.println(String.format("%s", event.getActionCommand()));
statusLabel.setText(String.format("%s", event.getActionCommand()));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Flashcards();
}
}
Re: Why can't I see the images in the buttons?
Please don't double post the same question. Locking.