Problems in Using JLabel to Display a Chosen Image File - java

I encountered a problem while I am trying to display an image after I clicked a button and chose image file within the "Choose File Dialog".
Initially, I was managed to display the chosen image in JLabel, but later I created a separate ActionListener, I think it started to go wrong since then. Whatever image I choose, the JLabel won't display it.
I debugged it, and sure that the file chooser does pass the image to ImageIcon, JLabel does get the value from ImageIcon, but it doesn't display the image even after revalidate() and repaint().
Here I attached my code for your kind reference!
(I trimmed the code for a clean look, so there might be some brackets left not useful)
package com.xxx.LoyalCardManager;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
public class LoyalCardManagerMain implements ActionListener{
private JFrame frame;
private DatabaseHandler db = new DatabaseHandler();
private JLabel labelPic;
private JButton buttonPic;
private File picFile = new File("");
private BufferedImage image;
/**
* Launch the application.
* #throws SQLException
* #throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoyalCardManagerMain window = new LoyalCardManagerMain();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
/**
* Create the application.
*/
public LoyalCardManagerMain() {
// Database initialisation
initDatabase();
// Draw GUI
frame = new JFrame();
frame.setBounds(100, 100, 619, 487);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
buttonPic = new JButton("Click to Choose Pic");
buttonPic.setBounds(415, 252, 166, 29);
frame.getContentPane().add(buttonPic);
buttonPic.setEnabled(false);
buttonPic.setActionCommand("ChoosePic");
buttonPic.addActionListener(this);
labelPic = new JLabel();
labelPic.setBounds(415, 30, 167, 210);
frame.getContentPane().add(labelPic);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("ChoosePic")) {
//TODO Label now cannot display images.
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new FileFilter() {
public boolean accept (File f) {
String extension = Utils.getExtension(f);
if(extension != null) {
if (extension.equals(Utils.gif) ||
extension.equals(Utils.jpeg) ||
extension.equals(Utils.jpg) ||
extension.equals(Utils.png) ||
extension.equals(Utils.tif) ||
extension.equals(Utils.tiff)) {
return true;
}else{
return false;
}
}
return false;
}
public String getDescription() {
return "Image File (*.gif, *.jpeg, *.jpg, *.png, *.tif, *.tiff)";
}
});
int retVal = chooser.showOpenDialog(frame);
if (retVal == JFileChooser.APPROVE_OPTION) {
picFile = chooser.getSelectedFile();
try {
image = ImageIO.read(picFile);
} catch (IOException e) {
e.printStackTrace();
}
// Calculate the pic's ratio and do re-scale
double ratio = (double) labelPic.getWidth() / (double) labelPic.getHeight();
// Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height.
int scaledW = (int) (image.getHeight() * ratio);
image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST);
ImageIcon icon = new ImageIcon(image);
labelPic.setVisible(true);
labelPic.setIcon(icon);
labelPic.revalidate();
labelPic.repaint();
}
}
}
}
I also referenced other similar questions:
image loading using a JFileChooser into a JFrame
Image won't display in JLabel
Updating an image contained in a JLabel - problems
External Site: JFIleChooser opening image to JLabel
As well as Java Tutorial Docs
How to Use Buttons, Check Boxes, and Radio Buttons
But I still can't figure it out why the JLabel not display the chosen image.
Thanks for your kind help mates!

Ok, I finally figured out what's wrong with the code:
If I intend to use BufferedImage to resize (sorry, in my question I mis-understanding the method scale with resize), I need to use drawImage method to "redraw" the image. Otherwise the image will not be shown.
I made modification here:
double ratio = (double) labelPic.getWidth() / (double) labelPic.getHeight();
// Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height.
int scaledW = (int) (image.getHeight() * ratio);
image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST);// Edit here
ImageIcon icon = new ImageIcon(image);
labelPic.setVisible(true);
labelPic.setIcon(icon);
labelPic.revalidate();
labelPic.repaint();
From the "Edit Here" mark, I use the following code:
BufferedImage imageTemp = new BufferedImage(resizedW, resizedH, BufferedImage.TYPE_INT_RGB);
imageTemp.getGraphics().drawImage(image,0,0, scaledW, scaledH, null);
image = imageTemp;
And there's difference between first pass the value to imageTemp then pass to image and directly pass the value to image. If I pass the new BufferedImage directly to image, it will display a pure black colour instead of the image you choose.

Try using this to display the image:
JfileChooser getImage = new JFileChooser();
..........
ImageIcon imagePath= new ImageIcon(getImage.getPath());
JLabel imageLabel= new JLabel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imagePath.getImage(), 0, 0, width, height, null);
}
};
imageLabel.setLocation(10, 40);
imageLabel.setBorder(viewAnimalPanelBorder);
imageLabel.setSize(200, newHeight);
panel.add(imageLabel);
Let me know if you require more assistance.
Also, try displaying the picture without using the JFileChooser, maybe hard code the path for a test.

Related

How to refresh/reload an image in Swing GUI as the real image file changes?

I am trying to implement Remote FrameBuffer Protocol using Java socket programming.
I have a server side program that takes screenshot of the entire screen using robot and store it in BufferedImage .Then I converted it into a byte array and sending it to the client .
Objective :
To display the entire screen of the server side machine in a Swing GUI of the client side.
Problem i am facing :
i am able to send the image in bytes from server and receive it from the server by the client (client.java) and convert it into a jpg image (output.jpg) using ImageIO and put that image in a Swing frame.
But i am able to see the first image in the Swing and whenever the image gets updated ,the image in the Swing is not updating or refreshing .
What I want :
I want the image to refresh and show updated image every time the server sends the image data .
client.java
package remoteclient;
import java.lang.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class client {
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",5900);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int width=0,height=0;
try {
width = din.readInt(); //getting width and height from server thru socket.
height = din.readInt();
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame("Client");
JLabel label = new JLabel();
f.setSize(width, height);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
boolean continueLoop = true;
while(continueLoop)
{
try {
int len = din.readInt();
byte[] imageInByte = new byte[len];
System.out.println(len);
din.readFully(imageInByte);
System.out.println(imageInByte);
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
BufferedImage bImage2 = ImageIO.read(bis);
// Image im1 = bImage2.getScaledInstance(width,height, Image.SCALE_SMOOTH);
ImageIO.write(bImage2, "jpg", new File("output.jpg") );
bImage2 = ImageIO.read(new File("output.jpg"));
label.setIcon(new ImageIcon(im1));
ImageIcon icon = new ImageIcon(bImage2);
icon.getImage().flush();
label.setIcon( icon );
f.getContentPane().add(label, BorderLayout.CENTER);
f.pack();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
What I want :
I want the image to refresh and show updated image every time the server sends the image data .
Updated code with comments about demo code that should be removed from your working code:
Here's an example, using default UIManager icons, and SwingWorker, as noted in the comments to the original posting. You would instead use images from your server connection.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
public class SwingLabelWithUpdatedImage {
public static void main(String args[]) throws Exception {
final JLabel label = new JLabel("", SwingConstants.CENTER);
final JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.CENTER);
final Dimension preferredSize = new Dimension(200, 100);
frame.setPreferredSize(preferredSize);
frame.setVisible(true);
frame.pack();
final ImageUpdateWorker task = new ImageUpdateWorker(label);
task.execute();
}
public static class ImageUpdateWorker extends SwingWorker<Void, IconInfo> {
// iconInfoList is not need in your code. It's here so I can
// supply a dummy set of icons to demonstrate UI updates.
final List<IconInfo> iconInfoList;
private JLabel label;
ImageUpdateWorker(JLabel label) {
this.label = label;
// Delete this in your code
this.iconInfoList = initIconInfoList();
}
#Override
public Void doInBackground() {
boolean isTrue = true;
while (isTrue) {
// Put your socket code to read the next icon from a server.
// You don't need to do the ImageIO.write(), ImageIO.read() dance,
// unless you must save the icon to disk. In that case, you don't need
// to read it back in.
// Here, I just rotate the iconInfoList to make it
// appear as though a new icon was received.
// Your code will not have any need to do this.
Collections.rotate(iconInfoList, -1);
// Just publish the icon you create from the image
// you receive from your remote server.
publish(iconInfoList.get(0));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void process(List<IconInfo> icons) {
// You might check for an empty list.
// #kleopatra's suggestion to get the last icon is correct.
// See https://docs.oracle.com/javase/tutorial/uiswing/concurrency/interim.html
IconInfo iconInfo = icons.get(icons.size() - 1);
label.setIcon(iconInfo.icon);
// Your code will not do this
label.setText(iconInfo.name);
// You can get the icon dimensions just from the icon,
// so you don't really need the IconInfo class.
label.setSize(iconInfo.dimension);
}
/** Demo code only. It doesn't belong in your working code.
*/
protected List<IconInfo> initIconInfoList() {
// Just a quick way to get some icons; don't need to
// fetch from a server just to demonstrate how to
// refresh the UI.
List<IconInfo> iconInfoList = UIManager.getDefaults().keySet().stream()
.filter(this::isIconKey)
.map(IconInfo::new)
.filter(iconInfo -> iconInfo.icon != null)
.collect(Collectors.toList());
return iconInfoList;
}
/** Demo code only. It doesn't belong in your working code.
*/
protected boolean isIconKey(Object key) {
return String.class.isAssignableFrom(key.getClass())
&& ((String) key).toLowerCase().contains("icon");
}
}
/** This is just a convenience to convey
* the icon and its UIManager key (i.e., name).
* Your remote server doesn't supply a name,
* so you don't really need this class.
* It's just to make the demo more expressive.
*/
public static class IconInfo {
final private String name;
final private Icon icon;
final private Dimension dimension;
IconInfo(Object name) {
this.name = name.toString();
icon = UIManager.getIcon(name);
dimension = icon == null
? new Dimension(32, 32)
: new Dimension(icon.getIconWidth(), icon.getIconHeight());
}
}
}

Unhandled Exception Error in Java even with try-catch

I am quite new to Java and started learning on YouTube. To practice GUI Programs, I decided to make my own and am now trying to resize an image to add to a button in my aplication. I searched how to resize images and found some source code online which I decided to test and put in my own program, but when I call the method I get an unreported exception java.io.IOException; must be caught or declared to be thrown error and the IntelliJ IDE says Unhandled exception: java.io.IOException. I have the try-catch blocks in my code, but this still comes up. How can I fix this? Here is some of my code:
Images.java (class with the resizer method I found online) the try-catch I put in myself.
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Images {
//Image resizer method for scaling images
void resizeImage(String inputImagePath, String outputImagePath, int scaledWidth, int scaledHeight) throws IOException {
//reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
//creates output image
BufferedImage outputImage = new BufferedImagee(scaledWidth, scaledHeight, inputImage.getType());
//scales the inputImage to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1);
//writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
try {
inputFile = null;
inputImage = ImageIO.read(inputFile);
}
catch(IOException e){
e.printStackTrace();
System.out.println("image file path is null");
}
}
}
GUI.java (where the error appears when I try to call the method). The error appears at plus.resizeImage.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.company.Images;
public class GUI extends JFrame {
//This will be used for dimensions of the window
private int height, width;
GUI(int w, int h) {
super("OS Control");
setWidth(w);
setHeight(h);
setSize(getWidth(), getHeight());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setContent();
setVisible(true);
}
//Gets and sets for height and width of window
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
////////////////////////////////////////////////
/*Method with the actual contents of the aplication
i.e buttons, text fields, etc.
*/
void setContent() {
//variables used in the methods for ease of changing if needed
int buttonWidth, buttonHeight;
int searchBarWidth, searchBarHeight;
buttonWidth = 200;
buttonHeight = 100;
searchBarWidth = 350;
searchBarHeight = 25;
//Panel for the two center buttons
JPanel buttons = new JPanel();
//flow layout to center horizontally
buttons.setLayout(new FlowLayout());
buttons.setBackground(Color.decode("#9E9E9E"));
JButton mechanicButton = new JButton("Mecanicos");
mechanicButton.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
mechanicButton.setFocusable(false);
mechanicButton.addActionListener(new MechanicButtonEventHandling());
buttons.add(mechanicButton);
JButton osButton = new JButton("Ordens de Servico");
osButton.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
osButton.setFocusable(false);
osButton.addActionListener(new OSButtonEventHandling());
buttons.add(osButton);
JPanel center = new JPanel();
//gridbag layout to center vertically
center.setLayout(new GridBagLayout());
center.setBackground(Color.decode("#9E9E9E"));
//combine the two to center horizontally and vertically
center.add(buttons);
JPanel search = new JPanel();
search.setLayout(new FlowLayout());
search.setBackground(Color.decode("#9E9E9E"));
search.setSize(new Dimension(getWidth(), searchBarHeight));
JTextField searchBar = new JTextField("Pesquisar: ");
searchBar.setPreferredSize(new Dimension(searchBarWidth, searchBarHeight));
searchBar.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
search.add(searchBar);
JPanel plusPanel = new JPanel();
plusPanel.setLayout(new BorderLayout());
plusPanel.setSize(new Dimension(10, 10));
Images plus = new Images();
plus.resizeImage("plus.png","plusButton.png", 10, 10);
ImageIcon plusButtonImage = new ImageIcon("plusButton.png");
JButton plusButton = new JButton(plusButtonImage);
plusButton.setSize(new Dimension(10, 10));
plusPanel.add(plusButton, BorderLayout.SOUTH);
//add to jframe
add(search);
add(center);
add(plusPanel);
}
private class MechanicButtonEventHandling implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame mechanicList = new JFrame("Lista de Mecanicos");
mechanicList.setSize(getWidth(), getHeight());
mechanicList.setLocation(100, 100);
mechanicList.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
mechanicList.setVisible(true);
}
}
private class OSButtonEventHandling implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame osList = new JFrame("Lista de ordens de servico");
osList.setSize(getWidth(), getHeight());
osList.setLocation(700, 100);
osList.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
osList.setVisible(true);
}
}
}
You need to have a better look at how Java handles exceptions.
Your Images#resizeImage method contains a few operations that throw an IOException this being, the ImageIO#read and ImageIO#write calls.
At the bottom of the method's body you do this:
try {
inputFile = null;
inputImage = ImageIO.read(inputFile);
} catch(IOException e){
e.printStackTrace();
System.out.println("image file path is null");
}
In this code bit you're handling the thrown IOException with a try-catch clause. The
same cannot be said though for these calls:
BufferedImage inputImage = ImageIO.read(inputFile);
ImageIO.write(outputImage, formatName, new File(outputImagePath));
Since both of these thrown an exception which is not handled within the method's scope (i.e with a try-catch clause), you're forcing the method's signature to have a throws declaration for the IOException thrown by these two. Since, IOException is a checked exception, the compiler expects to find a try-catch clause wrapping the call to Images#resizeImage method.
To fix your code you have two possible solutions:
Remove the try-catch clause from the method's body, leaving only the throws declaration forcing callers to handle the exception at the calling point.
Wrap almost all of the method's body or the places where an IOException is thrown in try-catch clauses, thus handling the exception within the method's body (not a good idea since this way you'll not know whether the act of resizing failed or not).
It seems that your understanding of how exceptions work and how you need to handle them is a bit limited, so before doing anything else I would suggest to take a small detour and check on them.
https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

Displaying an image on a JLabel [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am having trouble displaying an image on a JLabel. I have another class called ControlPanel which saves the image in the project folder. I had the methods which save the image in this class but for some reason, I was getting a NullPointerException. When I moved them in the other class everything started working properly. The actual image is a bufferedImage on which the user draws.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GalleryPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JLabel[] images;
private static final int MAX_IMAGES = 12;
private int currentImage;
public void init()
{
images = new JLabel[MAX_IMAGES];
setLayout(new GridLayout(3,4));
setBackground(Color.GRAY);
}
public void addImageToGallery(String fileName, String fileExtension)
{
if ( currentImage < images.length - 1)
{
ImageIcon icon = new ImageIcon(fileName + fileExtension);
images[currentImage] = new JLabel(icon, JLabel.CENTER);
add(images[currentImage]);
displayImage(currentImage);
currentImage++;
}
else
{
throw new ArrayIndexOutOfBoundsException("The gallery is full");
}
}
// display the doily image in the gallery
public void displayImage(int index)
{
images[index].setSize(300, 300);
add(images[index]);
}
public final int getMaxImages()
{
return MAX_IMAGES;
}
public Dimension getPreferredSize()
{
return new Dimension(380, 700);
}
}
those are the two methods in my other class which are responsible for saving the actual image
// Shows a dialog box which asks the user to choose a name for the file that he wants to save
public void saveImage(BufferedImage bufImage)
{
String fileName = JOptionPane.showInputDialog("Choose image name");
if (fileName != null)
{
if(fileName.equals(""))
{
fileName = "Untitled";
}
chooseImageFormat(bufImage, fileName);
}
}
//shows a dialog box which asks the user to select the file format of the image he would like to save
public void chooseImageFormat(BufferedImage bufImage, String fileName)
{
Object[] imageFormats = {"PNG", "JPEG"};
String userInput = (String) JOptionPane.showInputDialog(null, "Choose file format", "File Format Settings", JOptionPane.PLAIN_MESSAGE, null, imageFormats, "PNG");
String imageFormat = (userInput.equals("PNG")) ? "PNG" : "JPEG";
String fileExtension = (imageFormat.equals("PNG")) ? ".png" : ".jpg";
File file = new File(fileName + fileExtension );
try
{
ImageIO.write(bufImage, imageFormat, file);
}
catch (IOException e)
{
e.printStackTrace();
}
gallery.addImageToGallery(fileName, fileExtension);
}
I think you declare your gallery like this :
GalleryPanel gallery;
For that you get NullPointerException, so instead use this :
GalleryPanel gallery = new GalleryPanel();
EDIT
It worked but is there a way to not instantiate gallery like this?
You should to declare it and initialize it before you use it, there are another solution but you should to make many changes in your code, you have to make :
public static void addImageToGallery(String fileName, String fileExtension) {
You can call a static method like this
GalleryPanel.addImageToGallery(fileName, fileExtension);
But like i said, you should to make many changes.
Good luck.

Showing JDialog as sheet not working

I am currently using this code to create a JDialog;
package com.kamuara.reposync.window;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
public class SheetDialog {
private JFrame _windowFrame;
public static void main(String[] args) {
System.setProperty("apple.awt.documentModalSheet", "true");
System.setProperty("apple.awt.brushMetalLook", "true");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new SheetDialog();
} catch (Exception e) {
e.printStackTrace();
}
}
public SheetDialog() {
_windowFrame = new JFrame();
_windowFrame.setResizable(false);
_windowFrame.setBounds(100, 100, 451, 320);
_windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_windowFrame.getContentPane().setLayout(null);
_windowFrame.setVisible(true);
JButton showDialogButton = new JButton("Show Dialog");
showDialogButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
showSheetDialog(_windowFrame, "Test", "This should be a sheet dialog", "Oke");
}
});
showDialogButton.setBounds(328, 263, 117, 29);
_windowFrame.getContentPane().add(showDialogButton);
}
public void showSheetDialog(JFrame owner, String title, String message, String button) {
final JDialog messageDialog = new JDialog(owner, title, Dialog.ModalityType.DOCUMENT_MODAL);
messageDialog.setBounds(30, 0, owner.getWidth() - 60, 130);
// TODO: only when os is osx
messageDialog.getRootPane().putClientProperty("apple.awt.documentModalSheet", "true");
messageDialog.setLayout(null);
int offsetX = 25;
JLabel titleLabel = new JLabel(title);
titleLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
titleLabel.setBounds(offsetX, 10, 100, 25);
messageDialog.getContentPane().add(titleLabel);
JLabel messageLabel = new JLabel(message);
messageLabel.setVerticalTextPosition(JLabel.TOP);
messageLabel.setHorizontalTextPosition(JLabel.LEFT);
messageLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
messageLabel.setBounds(offsetX, 10, messageDialog.getWidth() - 10, messageDialog.getHeight() - 60);
messageDialog.getContentPane().add(messageLabel);
JButton okButton = new JButton(button);
okButton.setBounds(messageDialog.getWidth() - 105, messageDialog.getHeight() - 35, 100, 25);
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
messageDialog.dispose();
}
});
messageDialog.getContentPane().add(okButton);
messageDialog.setVisible(true);
}
}
I was previously using Java 6 to compile the application and setting the clientProperty apple.awt.documentModalSheet was working perfectly to display the dialog as a "Sheet" on OSX but now I started using Java 7 (update 25) and the dialog is no longer displayed as a Sheet. I can't seem to find any update documentation on this. Have they changed anything about this? How can I solve this? The current interface design looks tons better with a Sheet than a dialog.
Update
I found the following Bug report which seems to be the same issue as I am experiencing;
http://bugs.sun.com/view_bug.do?bug_id=8010197
Does anyone know how to resolve this? I have looked into libraries like QuaQua but I would prefer not using any library because I just want the Sheet functionality.
Update 2
I tried QuaQua, but the library currently has the exact same problem when compiling with Java 7. Any workarounds?
Update 3
Replaced code with working sample (http://pastebin.com/PJ8VGdPb)
Update 4
Found out SWT has a style for their Shell class named SWT.SHEET which still works in Java7, I don't prefer using a library like SWT, but it seems to be the only solution.
As far as I know, Apple didn't officially released their version of JDK 7. The latest version of the JDK Apple has optimized for their OS X is still JDK 6. That is also why updates for Java come thru the AppStore update tab. These updates do not come straight from Oracle.
If you downloaded JDK 7 directly from Oracle, this is a more generic, non-tweaked version.
So, I think you will just have to wait for Apple to release their OS X optimized JDK 7.
I experienced a lot of OS X features not to be working when I download from Oracle:
Trackpad gestures
Native Aqua Look'n'Feel doesn't work, even when trying to set it manually through UIManager.
Application icon not working when using JOptionPane.
JMenu's will stick into the JFrame itself instead of moving to the top of the screen.
It seems before JDK fix the bug, you have to implement the Sheet yourself.
The key points are:
Use the glass pane of the window frame to hold the Sheet dialog
Use the GridBagLayout (with NORTH anchor) to put the dialog at top|center of the pane
Animate the Sheet when show/disappeare by painting the dialog repeatedly, every time paint more/less part of the dialog
The following is the example code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.LineBorder;
public class SheetableJFrame extends JFrame implements ActionListener {
public static final int INCOMING = 1;
public static final int OUTGOING = -1;
public static final float ANIMATION_DURATION = 1000f;
public static final int ANIMATION_SLEEP = 50;
JComponent sheet;
JPanel glass;
Sheet animatingSheet;
boolean animating;
int animationDirection;
Timer animationTimer;
long animationStart;
BufferedImage offscreenImage;
public SheetableJFrame() {
super();
glass = (JPanel) getGlassPane();
glass.setLayout(new GridBagLayout());
animatingSheet = new Sheet();
animatingSheet.setBorder(new LineBorder(Color.black, 1));
}
public JComponent showJDialogAsSheet(JDialog dialog) {
sheet = (JComponent) dialog.getContentPane();
sheet.setBorder(new LineBorder(Color.black, 1));
glass.removeAll();
animationDirection = INCOMING;
startAnimation();
return sheet;
}
public void hideSheet() {
animationDirection = OUTGOING;
startAnimation();
}
private void startAnimation() {
glass.repaint();
// clear glasspane and set up animatingSheet
animatingSheet.setSource(sheet);
glass.removeAll();
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
glass.add(animatingSheet, gbc);
gbc.gridy = 1;
gbc.weighty = Integer.MAX_VALUE;
glass.add(Box.createGlue(), gbc);
glass.setVisible(true);
// start animation timer
animationStart = System.currentTimeMillis();
if (animationTimer == null) animationTimer = new Timer(ANIMATION_SLEEP, this);
animating = true;
animationTimer.start();
}
private void stopAnimation() {
animationTimer.stop();
animating = false;
}
// used by the Timer
public void actionPerformed(ActionEvent e) {
if (animating) {
// calculate height to show
float animationPercent = (System.currentTimeMillis() - animationStart) / ANIMATION_DURATION;
animationPercent = Math.min(1.0f, animationPercent);
int animatingHeight = 0;
if (animationDirection == INCOMING) {
animatingHeight = (int) (animationPercent * sheet.getHeight());
} else {
animatingHeight = (int) ((1.0f - animationPercent) * sheet.getHeight());
}
// clip off that much from sheet and put it into animatingSheet
animatingSheet.setAnimatingHeight(animatingHeight);
animatingSheet.repaint();
if (animationPercent >= 1.0f) {
stopAnimation();
if (animationDirection == INCOMING) {
finishShowingSheet();
} else {
glass.removeAll();
glass.setVisible(false);
glass.setLayout(new GridBagLayout());
animatingSheet = new Sheet();
}
}
}
}
private void finishShowingSheet() {
glass.removeAll();
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
glass.add(sheet, gbc);
gbc.gridy = 1;
gbc.weighty = Integer.MAX_VALUE;
glass.add(Box.createGlue(), gbc);
glass.revalidate();
glass.repaint();
}
class Sheet extends JPanel {
Dimension animatingSize = new Dimension(0, 1);
JComponent source;
BufferedImage offscreenImage;
public Sheet() {
super();
setOpaque(true);
}
public void setSource(JComponent source) {
this.source = source;
animatingSize.width = source.getWidth();
makeOffscreenImage(source);
}
public void setAnimatingHeight(int height) {
animatingSize.height = height;
setSize(animatingSize);
}
private void makeOffscreenImage(JComponent source) {
GraphicsConfiguration gfxConfig = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
offscreenImage = gfxConfig.createCompatibleImage(source.getWidth(), source.getHeight());
Graphics2D offscreenGraphics = (Graphics2D) offscreenImage.getGraphics();
source.paint(offscreenGraphics);
}
public Dimension getPreferredSize() {
return animatingSize;
}
public Dimension getMinimumSize() {
return animatingSize;
}
public Dimension getMaximumSize() {
return animatingSize;
}
public void paint(Graphics g) {
// get the bottom-most n pixels of source and paint them into g, where n is height
BufferedImage fragment = offscreenImage.getSubimage(0, offscreenImage.getHeight() - animatingSize.height,
source.getWidth(), animatingSize.height);
g.drawImage(fragment, 0, 0, this);
}
}
}
The test code
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class SheetTest extends Object implements PropertyChangeListener {
JOptionPane optionPane;
SheetableJFrame frame;
public static void main(String[] args) {
new SheetTest();
}
public SheetTest() {
frame = new SheetableJFrame();
// build JOptionPane dialog and hold onto it
optionPane = new JOptionPane("Do you want to close?", JOptionPane.QUESTION_MESSAGE, JOptionPane.CANCEL_OPTION);
frame.setSize(640, 480);
frame.setVisible(true);
optionPane.addPropertyChangeListener(this);
JDialog dialog = optionPane.createDialog(frame, "irrelevant");
frame.showJDialogAsSheet(dialog);
}
public void propertyChange(PropertyChangeEvent pce) {
if (pce.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
System.out.println("Selected option " + pce.getNewValue());
frame.hideSheet();
}
}
}
reference
http://oreilly.com/pub/h/4852
http://book.javanb.com/swing-hacks/swinghacks-chp-6-sect-6.html
Here's a super dodgy hack I came up with which sets the flag which the JDK now appears to be forgetting to set and manually positions the window in the right place. There's still a missing shadow though, so I wonder if anyone can improve on it. ;)
This messes with internal classes and private fields, so it might break in any given new release of the JDK, but it still works on 8u5. Maybe it will give some insight into how these internal AWT classes are structured.
public static void makeSheet(Dialog dialog) {
dialog.addNotify();
ComponentPeer peer = dialog.getPeer();
// File dialogs are CFileDialog instead. Unfortunately this means this hack
// can't work for those. :(
if (peer instanceof LWWindowPeer) {
LWWindowPeer windowPeer = (LWWindowPeer) dialog.getPeer();
//XXX: Should check this before casting too.
CPlatformWindow platformWindow = (CPlatformWindow) windowPeer.getPlatformWindow();
try {
Method method = CPlatformWindow.class.getDeclaredMethod(
"setStyleBits", int.class, boolean.class);
method.setAccessible(true);
method.invoke(platformWindow, 64 /* CPlatformWindow.SHEET */, true);
Window parent = dialog.getOwner();
dialog.setLocation(dialog.getLocation().x,
parent.getLocation().y + parent.getInsets().top);
} catch (Exception e) {
Logger.getLogger(SheetHack.class.getName())
.log(Level.WARNING, "Couldn't call setStyleBits", e);
}
}
}

why showing .GIF image increase memory continuously?

i am showing simple code sample. I showed an gif image in a Jlabel. When run the programme, TASK manager shows that memory is increasing continuously. Why it happens?
Edited:
try this code please... on show glass button, glass panel is shown with gif image and a hide glass button in it and with that memory will be started increasing. On clicking hide glass button, glass panel will be hidden and memory increasing will b stopped.
#mKorbel : I had debugged it, the constructor will be called once, so no re-initializing of JFrame and also included : setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public class circle extends JFrame {
public ImageIcon pic;
final JPanel glass;
public JButton glass_show;
public JButton hide_glass;
public circle() {
super("Hi shamansdsdsd");
setSize(500, 300);
// Image icon initialize once :
pic = new ImageIcon("images/loadinag.gif");
glass_show = new JButton("Show Glass panel");
this.add(glass_show);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
glass = (JPanel) this.getGlassPane();
hide_glass = new JButton("Hide Glass panel");
glass.add(hide_glass);
glass.add(new JLabel(pic));
glass.setOpaque(false);
}
public void initialize_listeners(){
glass_show.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent A) {
glass.setVisible(true);
}
});
hide_glass.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent A) {
glass.setVisible(false);
}
});
}
public static void main(String[] args) {
circle mFrame = new circle();
mFrame.initialize_listeners();
mFrame.setVisible(true);
}
}
There is a bug in Java with animated GIF images. There is no memory increase with other images.
Edit;
Below example runs without memory leak; but you need Eclipse SWT library from Eclipse's site
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class GIFExample {
static Display display;
static Shell shell;
static GC shellGC;
static Color shellBackground;
static ImageLoader loader;
static ImageData[] imageDataArray;
static Thread animateThread;
static Image image;
static final boolean useGIFBackground = false;
public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
shell.setSize(300, 300);
shell.open();
shellGC = new GC(shell);
shellBackground = shell.getBackground();
FileDialog dialog = new FileDialog(shell);
dialog.setFilterExtensions(new String[] {"*.gif"});
String fileName = dialog.open();
if (fileName != null) {
loader = new ImageLoader();
try {
imageDataArray = loader.load(fileName);
if (imageDataArray.length > 1) {
animateThread = new Thread("Animation") {
#Override
public void run() {
/* Create an off-screen image to draw on, and fill it with the shell background. */
Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight);
GC offScreenImageGC = new GC(offScreenImage);
offScreenImageGC.setBackground(shellBackground);
offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight);
try {
/* Create the first image and draw it on the off-screen image. */
int imageDataIndex = 0;
ImageData imageData = imageDataArray[imageDataIndex];
if (image != null && !image.isDisposed()) image.dispose();
image = new Image(display, imageData);
offScreenImageGC.drawImage(
image,
0,
0,
imageData.width,
imageData.height,
imageData.x,
imageData.y,
imageData.width,
imageData.height);
/* Now loop through the images, creating and drawing each one
* on the off-screen image before drawing it on the shell. */
int repeatCount = loader.repeatCount;
while (loader.repeatCount == 0 || repeatCount > 0) {
switch (imageData.disposalMethod) {
case SWT.DM_FILL_BACKGROUND:
/* Fill with the background color before drawing. */
Color bgColor = null;
if (useGIFBackground && loader.backgroundPixel != -1) {
bgColor = new Color(display, imageData.palette.getRGB(loader.backgroundPixel));
}
offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height);
if (bgColor != null) bgColor.dispose();
break;
case SWT.DM_FILL_PREVIOUS:
/* Restore the previous image before drawing. */
offScreenImageGC.drawImage(
image,
0,
0,
imageData.width,
imageData.height,
imageData.x,
imageData.y,
imageData.width,
imageData.height);
break;
}
imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
imageData = imageDataArray[imageDataIndex];
image.dispose();
image = new Image(display, imageData);
offScreenImageGC.drawImage(
image,
0,
0,
imageData.width,
imageData.height,
imageData.x,
imageData.y,
imageData.width,
imageData.height);
/* Draw the off-screen image to the shell. */
shellGC.drawImage(offScreenImage, 0, 0);
/* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
try {
int ms = imageData.delayTime * 10;
if (ms < 20) ms += 30;
if (ms < 30) ms += 10;
Thread.sleep(ms);
} catch (InterruptedException e) {
}
/* If we have just drawn the last image, decrement the repeat count and start again. */
if (imageDataIndex == imageDataArray.length - 1) repeatCount--;
}
} catch (SWTException ex) {
System.out.println("There was an error animating the GIF");
} finally {
if (offScreenImage != null && !offScreenImage.isDisposed()) offScreenImage.dispose();
if (offScreenImageGC != null && !offScreenImageGC.isDisposed()) offScreenImageGC.dispose();
if (image != null && !image.isDisposed()) image.dispose();
}
}
};
animateThread.setDaemon(true);
animateThread.start();
}
} catch (SWTException ex) {
System.out.println("There was an error loading the GIF");
}
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
Code Source
there are two areas
1) you forgot to declare setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); then current JVM instance is still in (the Native OS) RAM, until PC restart or power-off
2) maybe you are create a new JFrame for every Images on the fly

Categories