Results 1 to 5 of 5
- 10-17-2011, 08:51 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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?
Java 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(); } }
- 10-17-2011, 09:14 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Why can't I see the images in the buttons?
Is "/Images/Open.png" the path to the image? The first slash says this is an absolute path - not relative to the application, which would mean it is looking in a location independent of your application (not recommend). See the following for how to specify where an image is within a package
How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 10-17-2011, 09:15 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: Why can't I see the images in the buttons?
Quit multiposting the same question: Why can't I see the images in the buttons?
- 10-17-2011, 09:39 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
- 10-17-2011, 09:40 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Similar Threads
-
Why can't I see the images in the buttons?
By eLancaster in forum New To JavaReplies: 1Last Post: 10-18-2011, 01:15 AM -
how to scroll 2 images at a time(synchronisation),both images are on different panels
By smitharavi in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 04:32 PM -
Help: Displaying Images AND Adding Buttons
By Rhez in forum New To JavaReplies: 2Last Post: 08-05-2010, 06:19 AM -
Help Needed - Creating an Array of Buttons with Images
By 8492nd in forum AWT / SwingReplies: 2Last Post: 04-30-2010, 03:06 AM -
Changing images by clicking arrow buttons. help?
By ashton in forum New To JavaReplies: 3Last Post: 02-08-2009, 11:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks