aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Any idea why my jpeg isn't showing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Any idea why my jpeg isn Watch "Any idea why my jpeg isn New topic
Author

Any idea why my jpeg isn't showing?

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Using Netbeans IDE 5 and cobbled together a program from web samples, but can't seem to get it to show a Jpeg in a frame.

The actual code that I thought would show the pic is as follows:

ImageIcon icon = new ImageIcon("c:/beast.jpg");
JFrame PicFrame = new JFrame("PictureFrame");
PicFrame.setIconImage(icon.getImage());

Program runs with no errors but still no picture - any ideas why it's not happening (program code below)?

Many thanks in advance

Paul.



/*
* Tower.java
*
* Created on 28 July 2006, 11:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package darktower;

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

/**
*
* @author paul
*/
public class Tower extends JPanel {
JFrame TowerFrame;

/** Creates a new instance of Tower */
public Tower(JFrame frame) {
super(new BorderLayout());

this.TowerFrame = TowerFrame;

JLabel TowerTitle;
setLayout(new GridLayout(3, 1));

TowerTitle = new JLabel("LCD Goes Here");
TowerTitle.setVerticalTextPosition(JLabel.CENTER);
TowerTitle.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(TowerTitle, BorderLayout.NORTH);

ImageIcon icon = new ImageIcon("c:/beast.jpg");
JFrame PicFrame = new JFrame("PictureFrame");
PicFrame.setIconImage(icon.getImage());

JLabel PicLabel;
PicLabel = new JLabel("Picture goes here");

PicLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(PicLabel, BorderLayout.CENTER);

JPanel TowerButtonsPanel = CreateTowerButtons();
TowerButtonsPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(TowerButtonsPanel, BorderLayout.SOUTH);
}

private JPanel CreateTowerButtons(){
final int NumButtons = 12;
JButton[] TowerButtons = new JButton[NumButtons];

TowerButtons[ 0] = new JButton("Test1");
TowerButtons[ 1] = new JButton("Test2");
TowerButtons[ 2] = new JButton("Test3");
TowerButtons[ 3] = new JButton("Test4");
TowerButtons[ 4] = new JButton("Test5");
TowerButtons[ 5] = new JButton("Test6");
TowerButtons[ 6] = new JButton("Test7");
TowerButtons[ 7] = new JButton("Test8");
TowerButtons[ 8] = new JButton("Test9");
TowerButtons[ 9] = new JButton("Test10");
TowerButtons[10] = new JButton("Test11");
TowerButtons[11] = new JButton("Test12");

TowerButtons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PressedButton("Test1");
}
});

return createPane("Whatever", TowerButtons);
}

private static void PressedButton(String BtnName){
System.out.println(BtnName);

//To change JPeg here
}

private JPanel createPane(String description, JButton[] TowerButtons) {
int NumButtons = TowerButtons.length;

JPanel NewBox = new JPanel();
JLabel NewLabel = new JLabel(description);
NewBox.setLayout(new GridLayout(0, 3));

for (int i = 0; i < NumButtons; i++) {
NewBox.add(TowerButtons[i]);
}
return NewBox;
}

private static void CreateAndShowTowerGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window
JFrame MainTowerFrame = new JFrame("Dark Tower");
MainTowerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the 1st Controls container
Container MainTowerContainer = MainTowerFrame.getContentPane();
MainTowerContainer.setLayout(new GridLayout(1,1));

//Populate the MainContainer control
MainTowerContainer.add(new Tower(MainTowerFrame));

//Diplay The Window
MainTowerFrame.pack();
MainTowerFrame.setVisible(true);
}

private static ImageIcon TowerIcon(String ImageName)
{
ImageIcon icon = null;
icon = new ImageIcon(
Tower.class.getResource("c:/" + ImageName + ".jpg"));
return icon;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Schedule thread to show GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
CreateAndShowTowerGUI();
}
});
}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

From the Javadoc for javax.swing.JFrame:



So this method doesn't do what you think it does!

Instead of that, change your JLabel to use "icon" as its constructor argument:

PicLabel = new JLabel(icon);

That ought to do it for you.

I'm moving this to our Swing/AWT forum for any further discussion.


[Jess in Action][AskingGoodQuestions]
Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
PicLabel = new JLabel(icon); - This does the job

What's the best way of changing the image - is this OK?
PicLabel.setIcon(new ImageIcon("c:/bazaar.jpg"));

Also, do I have to worry about detroying the previous image icon?

Thanks again

Paul.
 
jQuery in Action, 2nd edition
 
subject: Any idea why my jpeg isn't showing?