• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Any idea why my jpeg isn't showing?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
});
}

}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic