XMLEncoder and ImageIcon
I have an ImageIcon that I would like to convert to XML using XMLEncoder. I've successfully converted other types of objects, but when I try this one, I don't get the image data - just the description field. I really only care about the image data - I don't want to have to separately handle the file the image is created from if possible.
My code is below. I'm assuming that this just isn't going to work, so any alternatives are greatly appreciated. I'm not married to the ImageIcon, I can use an Image or whatever other class. My only requirements are that it be displayable in a JLabel, storable in a database, and can be sent over a network connection. Big, big plusses for being able to XMLEncode another object that contains the image. Super big plusses.
Any ideas?
Many thanks,
Scott
XMLEncoder xenc = new XMLEncoder(System.out);
ImageIcon img = new ImageIcon("/Users/scott/Desktop/icons/contentIcon.gif");
while (img.getImageLoadStatus() != MediaTracker.COMPLETE) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
xenc.writeObject(img);
I wouldn't recommend reading the image file with ImageIO and then writing it back out. And I certainly wouldn't use ByteArrayOutputStream.toString(), since it's not really character data.
You have the file object "file", so read that in first.
File file = new File(...);
byte[] imageData = new byte[(int)file.length()];
DataInputStream in = new DataInputStream(new FileInputStream(file));
in.readFully(imageData);
in.close();Then you can show that on screen simply enough...
JLabel label = new JLabel(new ImageIcon(imageData));Then to save that byte array as a string, you need to encode the bytes. Base-64 encoding is probably the best way. But it should be said that it's not recommened to use the Sun Base-64 encoder, cuz it might not be on all JVM implementations. There are 3rd-party ones you can find and substitute for it, but...
// to string...
String imageDataString = new sun.misc.BASE64Encoder().encode(imageData);
// save to the XML node
// back to bytes...
byte[] imageData = new sun.misc.BASE64Decoder().decodeBuffer(imageDataString);
// show in a label, like above
Similar Messages
-
XMLencode and XMLdecode methods with jdk1.3??
Hi folks,
I have to work with jdk1.3 but I wanna use the XMLencode and the XMLdecode methods of the new jdk1.4 !!
Does anyone know if and how I could use these methods in my "old" jdk??
Thanks a lot
StevenGo to apache.org and download the xerces package. It's that same as JDK 1.4 except that the classes are in different packages.
-
Problem with JFileChooser and ImageIcon
I'm trying to create an ImageIcon object from file selected in JFileChooser. The problem is that jfc.getSelectedFile().getAbsolutePath() returns a string with backslashes and the constructor of ImageIcon requires the string to have slashes. I`ve tried to create it via URL but it doesn't work as well...
String lastUsedPath;
URL imageURL;
JFileChooser fc = new JFileChooser();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int returnVal = fc.showDialog(frame,title);
if(returnVal==JFileChooser.APPROVE_OPTION){
lastUsedPath = fc.getSelectedFile().getAbsolutePath();
imageURL = getClass().getResource(lastUsedPath); //don't really get this but I copied it from Java Swing Tutorial
}else{
return null;
int curH, curW;
ImageIcon srcIcon = new ImageIcon(imageURL);
curW = srcIcon.getIconWidth();
curH = srcIcon.getIconHeight();If I use it this way imageURL is set to null.
If I set the imageURL as 'fc.getSelectedFile().toURL()' the string is eg.: "file:/E:/file.jpg" instead of "E:/file.jpg". After that the ImageIcon is created but width and height return -1.
If I try to create ImageIcon by 'new ImageIcon(lastUsedPath)' I get a not null object but the width & height of the ImageIcon is -1 as well.
What do I have to do to be able to create an ImageIcon from file selected in JFileChooser? Why is this so hard and mind blowing?It still returns the ImageIcon object with width & height set to -1.
EDIT:
Got it finally:
lastUsedPathForStupidImageIcon = fc.getSelectedFile().getPath();
img = ImageIO.read(new File(lastUsedPathForStupidImageIcon));
curH = img.getHeight(this);
curW = img.getWidth(this);The key was to use another String variable and hold the path instead of the absolute path
Edited by: Beholder on Jan 17, 2010 1:35 PM -
According to the XMLEncoder documentation, Sun has blessed us with "redundancy elimination" so that a bean's default values are not written out into the stream when it is encoded into XML. However, I actually WANT the default values to be written out into the stream and not suppressed. Any ideas on how to do this?
Sander Smith>
This implies that it is the encoder that is deciding
whether something is default and hence should not be
serialized. Will a persistence delegate be able to
override this decision? The examples of persistence
delegates that I've seen have all been for complex
situations, I just want to do something very simple -
serialze all of my data whether or not it is default.
Anyone have an example for how to do this?
Sander SmithTo make XMLEncoder do its job properly I find that it is necessary to write gui code in a special way. Say you have MyPanel which contains nested panels etc. MyPanel and other such panels (the nested ones) would have to have a constructor that does nothing, and a special init() method that sets all the default properties. (Inside the init(), the nested init()s would be being called by the way).
Thus if you constructed a program that read MyPanel into memory, and then called its init(), and then XMLEncoded it, you should then be able to look at the output file, and see your default properties.
- Chris Murphy -
JButton and ImageIcon question ?
hi,
i want to create a simple button with an icon image, i have used the following code :
toolBar = new JToolBar();
toolBar.setPreferredSize(new Dimension(100,50));
toolBar.setRollover(false);
JButton button = new JButton(new ImageIcon("Stop16.gif"));
button.setPreferredSize(new Dimension(40,40));
toolBar.add(button);this code belong to my JMainFrame (extends JFrame), and the "Stop16.gif" file is in the same location as the JMainFrame class, but unfortunately i have a button without the icone associated.
what's wrong with my code ?
thanks for your helpAre you sure that the stop.gif image is in the same directory as you .java file. I had problems with the same code and found that when i moved the image file into the same directory as the main java file it worked fine. But i also dont totally understand the problem you are having.
Andrew -
import java.awt.*;
import javax.swing.*;
<applet code="JLabelDemo" width=250 height=150>
</applet>
public class JLabelDemo extends JApplet {
public void init() {
// Get content pane
Container contentPane = getContentPane();
// Create an icon
ImageIcon ii = new ImageIcon("france.gif");
// Create a label
JLabel jl = new JLabel("France", ii, JLabel.CENTER);
// Add label to the content pane
contentPane.add(jl);
I try to run the code above, but it's appear the JApplet hasn't been Initialized
why??
and no other thing shown after I add the ImageIcon
thanks for the helpImageIcon ii = new ImageIcon("france.gif");
//change the above code to
ImageIcon ii = new ImageIcon(getImage("france.gif")); -
How to recieve Debug-Information from XMLEncoder and XMLDecoder.
Hi, ist there any way to get Informations about the current class/object (methods that are called) the XML-Dencoder is currently working on?
Cause my stack-trace gives no hint and I'm loading (XML-file looks fine) a very complex structure with only one handle to the whole, so debbuging is quite impossible.
Thanks a lot in advance.
Greetings MichaelHi,
Use the Views:
VIAUFKS and VIAUF_AFVC
for getting the required Info.
Get the material from above and the text from MAKT.
reward if useful
regards,
ANJI -
Hey,
I have an ImageIcon arrary and JLabel called ImageLabel on which the ImageIcon is. Bascically I want it to do this: if the ImageIcon on the ImageLabel is a certain icon, say [0] and the click was < 200, then set the ImageIcon to [1] (in effect changing what image is shown on the ImageLabel). And vice versa, as demonstrated by the following code:
if (x < 200) and (ImageIcon theIcon = [0]) // if the click is below 200 on the x axis and theIcon is [0]
setImageLabel.setIcon(ImageIcon[1]);if (x > 200) and (ImageIcon theIcon = [1]) // if the click is above 200 on the x axis and theIcon is [1]
imageLabel.setIcon(ImageIcon[2);
However that code doesn't work. What code would work?Thank you for your reply. I tried using the code, however, I am unsure as to how to use a 2D array.
I think it may help if I elaborate on what I need to do:
I have 80 images, and would like each image to be assigned to a coordinate, such as 1,1 on a coordinate
plane that is 16 wide by 23 high. My first idea would be that you will need (16 * 23) = 368 images, not 80.
By having each image as an integer, I could then set it to load another image when clicked.
For instance I want the program to load with the image set to 1,1 (this image
would be "images/1-1.gif".)
Now, since you are on 1,1 and click on a certain area of the image, it loads 2,1 (that co ordinate having the image "images/2-1.gif". If you click on a different part of the 1,1 image, it would load 3,1 ("images/3-1.gif). etc.
Sorry to say it, but you're still unclear on your goal: will you be only displaying one image at a time ? Do you expect 1 image
to represent each of the (16 * 23) coordinates ? Do you mean that the label will change location depending on the mouse clicks ?
Whatever your objective is, I don't see what is incompatible with the code I suggested in my previous reply. -
Imageicon created from byte array gives -1 height and -1 width...
Hi,
when I am trying to create an imageicon object from byte arrays the length and width of the object are coming to be -1.So I am unable to resize the image..
This is happening for only few images, most of them are working fine and I can resize it...
What possibly could be wrong??
ImageIcon imageIcon = new ImageIcon(pImageData) where pImageData is bytearray..
but I am getting imageIcon.getIconWidth()=-1
and
imageIcon.getIconHeight()=-1
Can anyone help???es5f2000 wrote:
I'm not sure if this is related, but I believe that images which are not currently being rendered return -1, -1 as their size.It is not even correct, so I'm confident it is not related.
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.net.URL;
class TestIconSize {
public static void main(String[] args) throws Exception {
Image image = ImageIO.read( new URL(
"http://forums.sun.com/im/silver-star.gif") );
ImageIcon imageIcon = new ImageIcon( image );
System.out.println( "Width: " + imageIcon.getIconWidth() );
System.out.println( "Height: " + imageIcon.getIconHeight() );
}Output.
andrew@pc1:/media/disk/projects/numbered/all$ java TestIconSize
Width: 16
Height: 16
andrew@pc1:/media/disk/projects/numbered/all$ The post after yours seems of even more dubious quality. For those (and other) reasons, I second Darryl's call for an SSCCE. -
Complete beginners help.using ImageIcon and cant assign images
how do assign the ImageIcon for the front and back to this class.i have two gif files in the same directory. to test the card class i want to press the flip button to display the front and then the back. i am making simple card game but am very new to this and am stumbling at the basics.any help much appreciated. here is my attempt at the code.
( havent put in the paint() )
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JCard extends JComponent
private int value;
private ImageIcon FrontImg;
private ImageIcon BackImg;
private int size;
private boolean faceup;
public JCard()
this.value= value;
public int getValue()
return value;
//returns the width of the card
public int getWidth()
return getSize().width;
//returns the height of the card
public int getHeight()
return getSize().height;
//returns true if the card is face up
public boolean faceup()
return faceup;
//flips the card
public void flip()
if(faceup)
faceup = false;
else
faceup = true;
repaint();
public void setBackImg(ImageIcon img)
BackImg = img;
public void setFrontImg(ImageIcon img)
FrontImg = img;
public void paint(Graphics g)
//test JCard
import javax.swing.*;
import java.awt.event.*;
public class TestJCard extends JFrame implements ActionListener
private JCard card = new JCard();
private JButton b1 = new JButton("flip");
public TestJCard()
super();
JPanel p = (JPanel)getContentPane();
p.add("Center",card);
p.add("South",b1);
b1.addActionListener(this);
public void actionPerformed(ActionEvent ae)
card.flip();
//** To close window**//
class WindowHandler extends WindowAdapter
public void windowClosing(WindowEvent we)
we.getWindow().hide();
System.exit(0);
public static void main(String args[])
TestJCard fm = new TestJCard();
fm.setSize(300,300);
fm.setVisible(true);I'm not sure if I understand exactly what you're asking, but if you're wondering how to create/initialize and ImageIcon from an image file on a local disk, I believe it is as simple as:
ImageIcon i = new ImageIcon("path/filename.ext");
note: the path can be relative (i.e. "images/pict1.gif")
or explicit ("c:/project1/images/pict1.gif").
Hope this is what you're looking for. -
XMLEncoder articles in June JDJ and JavaPro magazines
Just an FYI. There are two good articles on using the XMLEncoder and XMLDecoder in the java.beans package in the June issues of JavaPro magazine and the June issue of the Java Development Journal.
http://www.fawcette.com/javapro/2003_06/magazine/features/chavener/
For JDJ its a bit tedious, http://www.sys-con.com/java/
you have to sign up for a free archive account.For the JDJ this URL has the full article:
http://www.sys-con.com/java/articlenews.cfm?id=2046 -
Hello all,
Could someone teach me to save and ImageIcon class to an file. I've tried ImageIO.write, but that doesn't accept an ImageIcon class. Tried to use BufferedImage, but that doesn't accept ImageIcon either.
ImageIcon icon = new ImageIcon(file.getAbsolutePath(), "Test");
int h = icon.getIconHeight();
int w = icon.getIconWidth();
int hnew, wnew;
if (h > w) {
hnew = jLabelPict.getHeight();
wnew = w / (h / jLabelPict.getHeight());
} else {
wnew = jLabelPict.getHeight();
hnew = h / (w / jLabelPict.getHeight());
Image img = icon.getImage().getScaledInstance(
wnew, hnew, Image.SCALE_FAST);
icon.setImage(img);
jLabelPict.setIcon(icon);
jLabelPict.setText("");
jTextField.setText(file.getAbsolutePath().toString());
At this point the file should be saved.
Many thanks & regardshope this can works (not tested):
ImageIcon icon = ...;
BufferedImage bimg = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
bimg.getGraphics().drawImage(icon.getImage(), 0, 0, null);
ImageIO.write(bimg, "jpeg", file); -
StackOverflowError with XMLEncoder on my objects.
Works fine when I run the example:
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("Test.xml")));
e.writeObject(new JButton("Hello, world"));
e.close()
but when i run my little class I get java.lang.StackOverflowError below.
Could anyone help? (I'm new to XMLEncoder and relatively new to JavaBeans).
Thanks in advance.
Morten
java.lang.StackOverflowError
at java.lang.Class.getMethod0(Class.java:1756)
at java.lang.Class.getMethod(Class.java:963)
at java.beans.Statement.findPublicMethod(Statement.java:230)
at java.beans.Statement.findMethod(Statement.java:270)
at java.beans.Statement.getMethod(Statement.java:366)
at java.beans.Statement.invoke(Statement.java:439)
at java.beans.Expression.getValue(Expression.java:101)
at java.beans.Encoder.getValue(Encoder.java:84)
at java.beans.Encoder.get(Encoder.java:186)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
at java.beans.Encoder.writeObject(Encoder.java:55)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:250)
at java.beans.Encoder.writeExpression(Encoder.java:260)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:363)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:100)
...I think the problem here may be that the BeanBox does not
remove itself as a listener from its components before it
tries to serialise them. The XMLEncoder works out that the
JellyBean and co have listeners (because they are derivatives
of Component - which has a getListeners() method) and will attempt
to serialise those listeners. I suspect that one of these is
the BeanBox itself - or a component inside it. If that component
also knows about the beans it contains there is a possibility
the archiving process will loop - though somthing odd is happening
as the XMLEncoder should detect identical instances in the object
graph and mark them with an "idref" attribute - rather than looping.
It may be that the BeanBox clones part of itself when certain
getters are called. (I don't have your code so this is all a
bit of a guess.)
Anyway, if you want to look at this sort of thing a better
place to start is the BeanBuilder which is availible on the
SwingConnection website. The BeanBox predates all of the
archiving work - whereas the BeanBuilder was purpose built to
demonstrate it and the other new features in SDK 1.4.
Hope that helps.
Philip -
Hello,
I've just about finished a program I've been working on for the last few weeks. But I need some help with one more thing. What I want to do is somehow build into my If and Else statements some code that will change the ImageIcon to a certain picture depending on where the click was. For instance, in my code right now I have code in place:
if (x < 200) // if the click is below 200 on the x axis
setTitle("LEFT (" + x + ", " + y + ")");
else if (x > 440) // if the click is above 440 on the x axis
setTitle("RIGHT (" + x + ", " + y + ")");
.....And such. This is just in place until I get the code in that will change the ImageIcon. I am thinking that I will need to set up an ImageIcon arrary, then have the code be something like
"if (x > 200) and ImageIcon index = 1
set ImageIcon index (4)"
and so on for the rest of the if statements. How would I go about doing this? Is there an easier way than an arrary? Is it possible to have "and" in the "if" statement? My full code is below for more info.
// tourWelby.java
// tourWelby
// A Program that involves a virtual interactive tour of Welby.
// Documentation for this program is located in the 'tourWelby'
// folder.
// Copyright (c) 2005. All rights reserved.
// this imports the necessary Java Packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// this is the main class for the program. It implements the MouseListener
// which is very important to the operation of the program
public class tourWelby extends JFrame implements MouseListener {
// this sets up the label used in the co-ordinate JPanel on the program
private JLabel label;
// this sets up the label used in the image JPanel on the program
private JLabel imageLabel;
// this sets up the label used in the title JPanel on the program
private JLabel titleLabel;
// this sets up the co-ordinate integers
int x,y;
// these methods involve the MouseListener and are used for the program to listen
// to and respond to the mouse
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
// the mouseClicked method makes the program respond to mouse clicks
public void mouseClicked(MouseEvent e) {
// this finds the x and y co-ordinates where the user has clicked
x = e.getX();
y = e.getY();
// this sets the title of the frame to display the cursor co-ordinates; and the
// position the user is facing (Forward, Back, Left, or Right) depending on where
// the co-ordinate is that was clicked on (through use of the IF and ELSE statements)
if (x < 200) // if the click is below 200 on the x axis
setTitle("LEFT (" + x + ", " + y + ")");
else if (x > 440) // if the click is above 440 on the x axis
setTitle("RIGHT (" + x + ", " + y + ")");
else if (y < 200) // if the click is below 200 on the y axis
setTitle("FORWARD (" + x + ", " + y + ")");
else if (y > 280) // if the click is above 280 on the y axis
setTitle("BACK (" + x + ", " + y + ")");
// insert comments...
public tourWelby() { // insert comments
// this creates the program's frame, sets its colour, and makes it not resizable
super("Tour Welby");
setResizable(false);
setBackground(Color.BLACK);
// sets up a new JButton for quitting the program.
// also sets up the size and location of the quit button and adds a Mnemonic for
// keyboard control
JButton quitButton = new JButton("Quit");
quitButton.setSize(80,30);
quitButton.setLocation(176,580);
quitButton.setMnemonic('Q');
// setups up a new JButton called aboutBoxButton, which brings up the About Box.
// also sets up the size and location of the about box button and adds a Mnemonic for
// keyboard control
JButton aboutBoxButton = new JButton("About");
aboutBoxButton.setSize(80,30);
aboutBoxButton.setLocation(86,580);
aboutBoxButton.setMnemonic('A');
// the container is what holds all of the components of the program (all of the JPanels,
// JMenuBar, etc). The quit button and about box button are added at this stage
Container content = getContentPane();
getContentPane().add(quitButton);
getContentPane().add(aboutBoxButton);
// makes a new JButton for use in the About Box and sets up its colour, size and location
JButton aboutCloseButton = new JButton("OK");
aboutCloseButton.setBackground(Color.BLACK);
aboutCloseButton.setForeground(Color.BLACK);
aboutCloseButton.setSize(60,30);
// makes a new JTextPane for use in the About box, and sets the Text, colours, border, and
// makes it uneditable. Also adds the aboutCloseButton and its position in the TextPane
JTextPane aboutpane = new JTextPane();
aboutpane.setText("text goes in here");
aboutpane.setForeground(Color.RED);
aboutpane.setBackground(Color.BLACK);
aboutpane.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.RED));
aboutpane.setEditable(false);
aboutpane.add(aboutCloseButton);
aboutCloseButton.setLocation(4,400);
// sets up a JFrame for the About Box, adds the JTextPane aboutpane to the About Box.
// also sets the colours, size, location, visibility of the frame and makes it non resizable
final JFrame aboutFrame = new JFrame("About Tour Welby");
aboutFrame.setSize(400, 460);
aboutFrame.setLocation(210,160);
aboutFrame.getContentPane().add(aboutpane);
aboutFrame.setBackground(Color.BLACK);
aboutFrame.setVisible(false);
aboutFrame.setResizable(false);
// creates the labels for use in the JPanels
titleLabel = new JLabel();
imageLabel = new JLabel();
// sets the text, font, and colour for the label used in theTitleArea
titleLabel.setText("Tour Welby");
titleLabel.setFont(new Font("Charcoal", Font.BOLD, 48));
titleLabel.setForeground(Color.RED);
// sets up the ImageIcon which is used on the JLabel "imageLabel"
ImageIcon theImage = new ImageIcon("images/1f.jpg");
// sets the size of the imageLabel used in theArea, sets the imageIcon to the imageLabel
// and makes it visible.
imageLabel.setSize(new java.awt.Dimension(640, 480));
imageLabel.setIcon(theImage);
imageLabel.setVisible(true);
// IMPORTANT: sets the MouseListener to run on the imageLabel specified.
// This allows the user to click on various parts of the imageLabel and have the
// program respond accordingly by loading other images and setting title
// to the co-ordinates of the mouse click
imageLabel.addMouseListener(this);
// sets a border (and defines its width and colour) to the imageLabel
imageLabel.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.RED));
// makes a new JMenuBar for use in the frame and sets the menu bar colour
JMenuBar menu = new JMenuBar();
menu.setBackground(Color.BLACK);
// makes a new menu, a File menu for use in the menu bar, sets the colours of the menu
// and adds a Mnemonic for keyboard control
JMenu fileMenu = new JMenu("File");
fileMenu.setBackground(Color.BLACK);
fileMenu.setForeground(Color.WHITE);
fileMenu.setMnemonic('F');
// makes a menu item called About and adds a Mnemonic for keyboard control
JMenuItem aboutItem = new JMenuItem("About");
aboutItem.setMnemonic('A');
// makes a menu item called Quit and adds a Mnemonic for keyboard control
JMenuItem quitItem = new JMenuItem("Quit");
quitItem.setMnemonic('Q');
// this adds the various menu items to the menus
fileMenu.add(aboutItem);
fileMenu.add(quitItem);
// installs the menu bar in the frame and puts the menus into the menu bar
super.setJMenuBar(menu);
menu.add(fileMenu);
// sets up the JPanel for the title area of the frame, and its size, colour, border and label
JPanel theTitleArea = new JPanel();
theTitleArea.setPreferredSize(new Dimension(644, 70));
theTitleArea.setBackground(Color.BLACK);
theTitleArea.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.RED));
theTitleArea.add(titleLabel);
// sets up the main JPanel for the the frame, and its size, colour, and border
JPanel theArea = new JPanel();
theArea.setPreferredSize(new Dimension(820, 680));
theArea.setBorder(BorderFactory.createMatteBorder(2,2,2,2, Color.RED));
// adds the the JPanel theTitleArea, and the JLabel imageLabel in the
// order in which they need to appear
theArea.add(theTitleArea);
theArea.add(imageLabel);
// adds theArea (and subsequently all other JPanels etc) to the container, puts all of
// the JPanels in order in the container, and makes the container visible
getContentPane().add(theArea);
pack();
setVisible(true);
// These are the ActionListners for the program (the quit button, about box button, about
// button, about menu item, quit menu item). Also the WindowListeners are here for the
// main frame (super) and the About Box
quitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { System.exit(0); }
}); // action listener for the Quit button
aboutBoxButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { aboutFrame.setVisible(true); }
}); // action listener for the About Box button (the button that displays the about box)
aboutCloseButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) { aboutFrame.setVisible(false); }
}); // action listener for the About Box OK button (the button that closes the about box)
aboutItem.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) { aboutFrame.setVisible(true); }
}); // action listener for the About menu item
quitItem.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) { System.exit(0); }
}); // action listener for the Quit menu item
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
}); // window listener for the main frame (super)
aboutFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { }
}); // window listener for the About Box
// insert comments
public static void main (String args[]) {
new tourWelby();
}Thanks.A JFrame extends a Frame. The latter class has the setIconImage
method, so you can use that ...
kind regards,
Jos -
Using XMLEncoder in Applet causes securityexception
I've written a simple Applet which should be able to XMLEncode a bean and write the XML-code to the standard output. (later it should be expanded to place various beans on a panel and write the XML to the server the applet is placed on, to be able to retrieve it later for further editing, but which is not the point right now)
The problem is when I run it in Internet Explorer or Netscape with Java plug-in 1.4.01, I get 9 times the following security exception before I get the output.
My question is, is there some way to prevent this security exception from occuring?
java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks)
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0" class="java.beans.XMLDecoder">
<object class="javax.swing.JButton">
<void property="actionCommand">
<string>Hello, world</string>
</void>
<void property="label">
<string>Hello, world</string>
</void>
<void property="model">
<void property="actionCommand">
<null/>
</void>
</void>
</object>
</java>
I've got this simple code:
public class XMLTest extends javax.swing.JApplet {
public XMLTest() {
public void init() {
java.beans.XMLEncoder xmle = new java.beans.XMLEncoder(System.out);
xmle.writeObject(new javax.swing.JButton("Hello, world"));
xmle.close();
<HTML>
<HEAD>
<TITLE>Applet HTML Page</title>
</head>
<BODY>
<APPLET code="XMLTest.class" width=200 height=100>
</applet>
</body>
</html>Thanks for the info; if it's really the last opportunity, I'll sign it, but it's not the intention at all to step out of the sandbox. The Applet doesn't need to write to the local file-system, all what it needs to do is read out the parameters of the beans it contains with XMLEncoder and put it in a string so I can send to the server it came from (this is no security issue).
The output XMLEncoder produces after the security exceptions (see the first message) is sufficient, contains enough parameters and can be used well the reverse way with XMLDecoder and without any security exceptions.
My question is this: Is XMLEncoder itself using code which bypasses the sandbox (probably yes) and is there some way to influence the process of encoding so the security exceptions don't occur?
Maybe you are looking for
-
So basically I have my main menu which takes you onto the loading screen and finally the game. How do I stop my movie from skipping to the loading screen? I know I can use the 'hold on current frame' from the behaviours but I want to use the lingo.
-
Export integration interface error
I am getting error "'Program internal error:null" when trying to run export integration interface in Demantra where i have specified view as well as filename(comma delimiter). Does anyone know what could be possible reason? It works when i remove fil
-
Is there a manual for Content Browser?
I have NO idea what I'm doing. Been trying to get NEX FS700 video files to my Mac then into Premiere. Can't believe there isn't a STEP by STEP manual. Solved! Go to Solution.
-
Shop floor with w/o no in PM module
Hi All, Can you please explain me in understanding the **standard functionality** of Shop floor paper with W/O# in PM module.If possible please share document. Regards, Meghana
-
Implementing export/import java utility
Hi all, I am not sure I hit the correct forum but I'll give it a try. I need to write java utility that access table in one db, select some rows and copy them to a similar table in secon db - as either new rows or update to existing rows (if such row