I asked here how to add background image to JComboBox properly, but didn't see answers, so i thought that i should separate these two questions...
So. How to add background image to JComboBox text field and to JComboBox popup panel properly?
UPD:
Some code for you)
Current code:
// ... Renderer
public class CBoxListCellRenderer implements ListCellRenderer {
ImagePanel panel;
JLabel label = new JLabel();
public CBoxListCellRenderer(Image img) {panel = new ImagePanel(img);}
public void setImage(Image img) {panel.setImage(img);}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
label.setText(value.toString());
panel.add(label);
return panel;
}
}
// ... ImagePanel
public class ImagePanel extends JPanel {
private BufferedImage img;
public ImagePanel(String img) {
setImage(img);
}
public void setImage(String img)
{
try {
this.img = ImageIO.read(this.getClass().getResource(img));
} catch (IOException ex) {
ex.printStackTrace();
}
Dimension size = new Dimension(this.img.getWidth(), this.img.getHeight());
setSize(size);
}
#Override
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), 0, 0, img.getWidth(), img.getHeight(), this);
}
}
// ... Colorizing arrow
class ColorArrowUI extends BasicComboBoxUI {
public static ComboBoxUI createUI(JComponent c) {
return new ColorArrowUI();
}
#Override protected JButton createArrowButton() {
return new BasicArrowButton(
BasicArrowButton.SOUTH,
Color.cyan, Color.magenta,
Color.yellow, Color.blue);
}
}
// ... Creating object
combo_language = new JComboBox(new DefaultComboBoxModel(new String[] { "English", "日本語", "Русский" }));
combo_language.setBorder(null);
combo_language.setRenderer(new CBoxListCellRenderer(new ImageIcon(getClass().getResource("/Images/form.png")).getImage()));
combo_language.setUI(new ColorArrowUI());
// ... Putting JComboBox to JFrame
Try using a ListCellRenderer like described here:
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
Here's an example of a custom ListCellRenderer. I return a JPanel that has painted an image to it then added a JLabel that holds the value of the list object. There's room for improvement on it, but it's just an example of what you can do.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestCBoxListCellRenderer {
public TestCBoxListCellRenderer() {
String[] list = {"Hello World 1", "Hello World 2", "Hello World 3"};
JComboBox box = new JComboBox(list);
box.setRenderer(new CBoxListCellRenderer());
JOptionPane.showMessageDialog(null, box, "Check out this Renderer", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args) {
new TestCBoxListCellRenderer();
}
class BackGroundPanel extends JPanel {
BufferedImage img = null;
public BackGroundPanel() {
try {
img = ImageIO.read(TestCBoxListCellRenderer.class.getResource("/res/leafcell.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(),
0, 0, img.getWidth(), img.getHeight(), this);
}
}
private class CBoxListCellRenderer implements ListCellRenderer {
final BackGroundPanel panel = new BackGroundPanel();
JLabel label = new JLabel();
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
label.setText(value.toString());
panel.add(label);
return panel;
}
}
}
You may want to add a thin line border or do something when its selected, its up to you. Use the variables from the getListCellRendererComponent method to render to your liking accordingly
Related
I am trying to add background image in jTable but when I run code nothing shown. only white color and no jTable shown and my java Application hangs also. is there any thing wrong in this code. here is the preview also.
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
{
public Main() {
JTable table = new JTable(100, 5) {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
((JComponent) c).setOpaque(false);
}
return c;
}
};
ImageJScrollPane isp = new ImageJScrollPane(table);
isp.setBackgroundImage(new ImageIcon("/Images/user/lockscreen.png"));
getContentPane().add(isp);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public static void main(String [] args) {
Main main = new Main();
main.setSize(400, 400);
main.setVisible(true);
}
}
class ImageJScrollPane extends JScrollPane
{
private ImageIcon image = null;
public ImageJScrollPane() {
this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
public ImageJScrollPane(Component view) {
this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
public ImageJScrollPane(Component view, int vsbPolicy, int hsbPolicy) {
super(view, vsbPolicy, hsbPolicy);
if (view instanceof JComponent) {
((JComponent) view).setOpaque(false);
}
}
public ImageJScrollPane(int vsbPolicy, int hsbPolicy) {
this(null, vsbPolicy, hsbPolicy);
}
public void setBackgroundImage(ImageIcon image) {
this.image = image;
}
public void paint(Graphics g) {
// Do not use cached image for scrolling
getViewport().setBackingStoreEnabled(true);
if (image != null) {
Rectangle rect = getViewport().getViewRect();
for (int x=0; x<rect.width; x+=image.getIconWidth()) {
for (int y=0; y<rect.height; y+=image.getIconHeight()) {
g.drawImage(image.getImage(), x, y, null, null);
}
}
super.paint(g);
}
}
}
Here is one way:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
setLayout( new BorderLayout() );
JTable table = new JTable(5, 5);
table.setOpaque( false );
DefaultTableCellRenderer renderer =
(DefaultTableCellRenderer)table.getDefaultRenderer(Object.class);
renderer.setOpaque(false);
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setOpaque( false );
scrollPane.getViewport().setOpaque( false );
final ImageIcon icon = new ImageIcon("mong.jpg");
JPanel background = new JPanel( new BorderLayout() )
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), this);
}
};
background.add( scrollPane );
add(background);
}
private static void createAndShowGUI()
{
JPanel panel = new JPanel();
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Make sure the /Images/user/lockscreen.png is in classpath, and also you are using absolute path '/' instead use relative path like ../ or ./ (or) make sure the absolute path is valid.
I want to print a JTable with background image or water mark. My code is:
public void actionPerformed(ActionEvent ae)
{
boolean status=false;
MessageFormat header = null;
header = new MessageFormat("Header");
MessageFormat footer = null;
footer = new MessageFormat("Page");
boolean fitWidth = true;
boolean showPrintDialog = true;
boolean interactive = true;
/* determine the print mode */
JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH
: JTable.PrintMode.NORMAL;
try
{
status = jt.print(mode, header, footer,showPrintDialog,null,interactive);
if(status ==true)
{
frame.dispose();
}
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
How can I pass or set the background image in this method?
there no easy way to set whatever for BackGround for whole JTable, but JViewPort from JScrollPane can do that easilly, then doesn't matter if is inside JScrollPane a JTable or another JComponents
for example
import java.awt.*;
import javax.swing.*;
class ImageAsTableBackround {
private JScrollPane sp;
private JTable table;
private String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
private String[][] data = new String[25][6];
public void buildGUI() {
sp = new JScrollPane();
// uncomment these codes lines for panting an image from package,
// but then block code table = new TableBackroundPaint0(data, head);
//sp.setViewport(new ImageViewport());
//table = new JTable(data, head);
//table.setOpaque(false);
// code for painting from generated code
table = new TableBackroundPaint0(data, head);
table.setBackground(new Color(0, 0, 0, 0));
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sp.setViewportView(table);
JFrame frame = new JFrame();
frame.getContentPane().add(sp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class ImageViewport extends JViewport {
private static final long serialVersionUID = 1L;
private Image img;
public ImageViewport() {
try {
ImageIcon image = new ImageIcon(getClass().getResource("resources/PICT6090.jpg"));
img = image.getImage();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
} else {
g.drawString("This space for rent", 50, 50);
}
}
}
class TableBackroundPaint0 extends JTable {
private static final long serialVersionUID = 1L;
TableBackroundPaint0(Object[][] data, Object[] head) {
super(data, head);
setOpaque(false);
((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
}
#Override
public void paintComponent(Graphics g) {
Color background = new Color(168, 210, 241);
Color controlColor = new Color(230, 240, 230);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
g2.fillRect(0, 0, width, height);
g2.setPaint(oldPaint);
for (int row : getSelectedRows()) {
Rectangle start = getCellRect(row, 0, true);
Rectangle end = getCellRect(row, getColumnCount() - 1, true);
g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
}
super.paintComponent(g);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ImageAsTableBackround().buildGUI();
}
});
}
}
The problem has two parts
print an image an each page
be sure the image "shines-through" the table
the second is addressed by #mKorbel (though not really solved because there is no nice solution :-)
To solve the first, I would go for a custom Printable and subclass JTable to return it, something like
public class BackgroundPrintable implements Printable {
Printable tablePrintable;
JTable table;
MessageFormat header;
MessageFormat footer;
BufferedImage background;
public BackgroundPrintable(MessageFormat header, MessageFormat footer) {
this.header = header;
this.footer = footer;
}
public void setTablePrintable(JTable table, Printable printable) {
tablePrintable = printable;
this.table = table;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
printImage(graphics, pageFormat, pageIndex);
int exists = tablePrintable.print(graphics, pageFormat, pageIndex);
if (exists != PAGE_EXISTS) {
return exists;
}
return PAGE_EXISTS;
}
private void printImage(Graphics graphics, PageFormat pageFormat,
int pageIndex) {
// grab an untainted graphics
Graphics2D g2d = (Graphics2D)graphics.create();
// do the image painting
....
// cleanup
g2d.dispose();
}
}
// use in JTable subclass
#Override
public Printable getPrintable(PrintMode printMode,
MessageFormat headerFormat, MessageFormat footerFormat) {
Printable printable = super.getPrintable(printMode, null, null);
BackgroundPrintable custom = new BackgroundPrintable(headerFormat, footerFormat);
custom.setTablePrintable(this, printable);
return custom;
}
To achieve the second, both the JTable and its renderers must be transparent. Tricky, because:
probably only if printing - otherwise they should have their usual opacity
all of the renderers must be transparent, and there is no completely safe way to get hold of them all
A custom JTable could try to achieve that by forcing the rendering component's opacity in its prepareRenderer:
#Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
JComponent comp = (JComponent) super.prepareRenderer(renderer, row, column);
if (isPaintingForPrint()) {
comp.setOpaque(false);
} else {
comp.setOpaque(true);
}
return comp;
}
Actually, that's not entirely valid: the code in the else block might be the wrong-thing-to-do for naturally transparent components. No really reliable solution available, I'm afraid.
I want to have transparent panels in my GUI (if like Windows 7 window headers, it is better).
Before I have used com.sun.awt.AWTUtilities as
AWTUtilities.setWindowOpacity(frame, (float)0.90);
but its parameter is a window like JFrame and couldn't be used for JPanel.
Also I want to have effects on JPanel or JLabel for example luminescence, as is on Windows 7 header buttons. Any other interesting effect is also helpful for me.
Please see the tutorials How to Create Translucent and Shaped Windows and* How to Create Translucent and Shaped Windows*. Follow the links to excellent example depots by #camickr.
For example,
import java.awt.event.*;
import java.awt.Color;
import java.awt.AlphaComposite;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
public class ButtonTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ButtonTest().createAndShowGUI();
}
});
}
private JFrame frame;
private JButton opaqueButton1;
private JButton opaqueButton2;
private SoftJButton softButton1;
private SoftJButton softButton2;
public void createAndShowGUI() {
opaqueButton1 = new JButton("Opaque Button");
opaqueButton2 = new JButton("Opaque Button");
softButton1 = new SoftJButton("Transparent Button");
softButton2 = new SoftJButton("Transparent Button");
opaqueButton1.setBackground(Color.GREEN);
softButton1.setBackground(Color.GREEN);
frame = new JFrame();
frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
frame.add(opaqueButton1);
frame.add(softButton1);
frame.add(opaqueButton2);
frame.add(softButton2);
frame.setSize(567, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Timer alphaChanger = new Timer(30, new ActionListener() {
private float incrementer = -.03f;
#Override
public void actionPerformed(ActionEvent e) {
float newAlpha = softButton1.getAlpha() + incrementer;
if (newAlpha < 0) {
newAlpha = 0;
incrementer = -incrementer;
} else if (newAlpha > 1f) {
newAlpha = 1f;
incrementer = -incrementer;
}
softButton1.setAlpha(newAlpha);
softButton2.setAlpha(newAlpha);
}
});
alphaChanger.start();
Timer uiChanger = new Timer(3500, new ActionListener() {
private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
private int index = 1;
#Override
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(laf[index].getClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception exc) {
exc.printStackTrace();
}
index = (index + 1) % laf.length;
}
});
uiChanger.start();
}
public static class SoftJButton extends JButton {
private static final JButton lafDeterminer = new JButton();
private static final long serialVersionUID = 1L;
private boolean rectangularLAF;
private float alpha = 1f;
public SoftJButton() {
this(null, null);
}
public SoftJButton(String text) {
this(text, null);
}
public SoftJButton(String text, Icon icon) {
super(text, icon);
setOpaque(false);
setFocusPainted(false);
}
public float getAlpha() {
return alpha;
}
public void setAlpha(float alpha) {
this.alpha = alpha;
repaint();
}
#Override
public void paintComponent(java.awt.Graphics g) {
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
if (rectangularLAF && isBackgroundSet()) {
Color c = getBackground();
g2.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g2);
}
#Override
public void updateUI() {
super.updateUI();
lafDeterminer.updateUI();
rectangularLAF = lafDeterminer.isOpaque();
}
}
}
If you have time I recommend you go through this Filty Rich Clients. By using this book you can learn to create stunning visual and animated effects with Swing and Java 2D. Learn graphics and animation fundamentals as well as advanced rendering techniques.
EDIT:
To creat transparent panels call
setOpaque(false)
It'll pass off painting the background to its parent, which may draw its own background.
You can do a screen capture and then use that to paint the background of the panel.
My resizable JScrollPane's content has a minimum width. If the JScrollPane is smaller than this width, horizontal scroll bars should appear. If it's greater than this width, the viewport content should expand to fill up the entire viewport.
Seems like a simple concept, and I've got something that's working, but it feels like a hack:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class SSBTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final Component view = new MyView();
final JScrollPane jScrollPane = new JScrollPane(view);
jScrollPane.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(final ComponentEvent e) {
final Dimension minimumSize = view.getMinimumSize();
final int width = Math.max(minimumSize.width, jScrollPane.getViewport().getWidth());
view.setPreferredSize(new Dimension(width, minimumSize.height));
}
});
showInDialog(jScrollPane);
}
});
}
private static void showInDialog(final JScrollPane jScrollPane) {
final JDialog dialog = new JOptionPane(jScrollPane).createDialog("JScrollPane Resize Test");
dialog.setResizable(true);
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
}
private static final class MyView extends JPanel {
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawString("Dimensions are " + getSize(), 10, 20);
g.drawRect(0, 0, getMinimumSize().width-1, getMinimumSize().height-1);
g.setColor(Color.BLUE);
g.drawRect(0, 0, getPreferredSize().width-1, getPreferredSize().height-1);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(200, 200);
}
#Override
public Dimension getPreferredSize() {
return super.getPreferredSize();
}
}
}
Resizing the dialog triggers the ComponentListener, which explicitly sets the preferred size of the viewport view, triggering component validation. However, resizing causes jittery scroll bars. Is there a cleaner way to do this?
EDIT: thanks to camickr for the ScrollablePanel link, I've modified my JPanel class to implement Scrollable, and dynamically change the return value for getScrollableTracksViewportWidth().
When the viewport is big, I return true for getScrollableTracksViewportWidth(), telling the JScrollPane to fill the view with my component. When the viewport is small, I return false, so the scrollbars appear.
import javax.swing.*;
import java.awt.*;
public class SSBTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final Component view = new MyView();
final JScrollPane jScrollPane = new JScrollPane(view);
showInDialog(jScrollPane);
}
});
}
private static void showInDialog(final JScrollPane jScrollPane) {
final JDialog dialog = new JOptionPane(jScrollPane).createDialog("JScrollPane Resize Test");
dialog.setResizable(true);
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
}
private static final class MyView extends JPanel implements Scrollable {
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawString("MyView: " + getWidth() + "x" + getHeight(), 10, 20);
g.drawRect(0, 0, getMinimumSize().width-1, getMinimumSize().height-1);
g.setColor(Color.BLUE);
g.drawRect(0, 0, getPreferredSize().width-1, getPreferredSize().height-1);
g.drawString("Preferred/Minimum Size", 10, getPreferredSize().height/2);
g.setColor(Color.GREEN);
g.drawLine(0, 0, getWidth(), getHeight());
}
#Override
public Dimension getMinimumSize() {
return new Dimension(200, 200);
}
#Override
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableUnitIncrement(final Rectangle visibleRect, final int orientation, final int direction) {
return 10;
}
public int getScrollableBlockIncrement(final Rectangle visibleRect, final int orientation, final int direction) {
return visibleRect.width;
}
public boolean getScrollableTracksViewportWidth() {
final Container viewport = getParent();
return viewport.getWidth() > getMinimumSize().width;
}
public boolean getScrollableTracksViewportHeight() {
return true;
}
}
}
Not sure, but you might be able to use the Scrollable Panel. You can configure the component resizing (try using STRETCH). The code works on the preferred size of the component not the minimum size so it may not be exactly what you want.
I am learning Swing and wrote an app that lets user select an image file and displays it on a JPanel. It works, but I want to handle situations when
user doesn't select any file
user selects a non image file
In these cases I want to clear the JPanel and show an error message on a text area.
I tried to do this as below.But I am not sure if this is the way to do this properly.I would like your suggestions.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
class Display extends JFrame {
private MyImagePanel canvas;
private JButton okbutton;
private JTextArea result;
private JFileChooser filechooser;
private static Insets insets = new Insets(0, 0, 0, 0);
public Display(String name) {
super(name);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
addWidgets();
showGUI();
}
private void showGUI() {
this.pack();
this.setVisible(true);
}
private void addWidgets() {
canvas = new MyImagePanel();
okbutton = new JButton("OK");
filechooser = new JFileChooser("Select imagefile");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
filechooser.setFileFilter(filter);
filechooser.setControlButtonsAreShown(false);
result = new JTextArea(10, 10);
addComponent(filechooser, 0, 0, 2, 4, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComponent(canvas, 2, 0, 2, 2, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComponent(result, 2, 2, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
addComponent(okbutton, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
}
public void addOKButtonListener(ActionListener okl) {
okbutton.addActionListener(okl);
}
public void displayResult(String msg) {
result.setText(msg);
}
public void clearResultField() {
result.setText("");
}
public void displayImage(String imagefilename) {
canvas.setImage(imagefilename);
}
public String getSelectedFile() {
java.io.File selectedFile = filechooser.getSelectedFile();
String filePathName = "";
if (selectedFile == null) {
result.setText("select a file");
} else {
filePathName = selectedFile.getPath();
}
return filePathName;
}
public void addComponent(Component component, int gridx, int gridy,
int gridwidth, int gridheight, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
this.add(component, gbc);
}
}//end class Display
class MyImagePanel extends JPanel {
private BufferedImage bi;
public MyImagePanel() {
super();
bi = null;
}
public void setImage(String imagefilename) {
try {
bi = ImageIO.read(new File(imagefilename));
this.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
} catch (Exception e) {
bi = null;
}
this.revalidate();
this.repaint();
}
#Override
public void paintComponent(Graphics g) {
// clear the background
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
if (bi != null) {
// draw the image
g.drawImage(bi, 0, 0, null);
}
}
}
class GUIController {
private Display display;
public GUIController(Display d) {
display = d;
display.addOKButtonListener(new OKButtonListener());
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
display.clearResultField();//make result field blank
String fileselection = display.getSelectedFile();
if (fileselection.length() > 0) {
display.displayImage(fileselection);
} else {
display.displayResult("actionPerformed:no file selected");
display.displayImage("");
}
}
}
}
public class ImageDisplay {
public static void main(String[] args) {
Display d = new Display("image demo");
GUIController ctrl = new GUIController(d);
}
}
The several drawImage() methods in java.awt.Graphics do "nothing if img is null." As a result, setting the image to null is sufficient. You're clearing the background explicitly, but super.paintComponent(g) is an alternative that clears the panel to the background color.
Addendum: You may also want to study the examples found in the articles How to Use File Choosers and Working with Images.
Addendum: I used a different layout and added the image to a JScrollPane. I also had setImage() return a result to let the Display know what happened.
Addendum: This newer, simpler revision extends JFileChooser to handle approve and cancel directly.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/** #see https://stackoverflow.com/questions/4053090 */
public class ImageDisplay extends JFrame {
private static final String title = "Select a file";
private MyImagePanel imagePanel = new MyImagePanel();
private JLabel result = new JLabel(title, JLabel.CENTER);
private MyChooser fileChooser = new MyChooser();
public ImageDisplay(String name) {
super(name);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWidgets();
this.pack();
this.setVisible(true);
}
private void addWidgets() {
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
fileChooser.setFileFilter(filter);
this.add(fileChooser, BorderLayout.WEST);
this.add(new JScrollPane(imagePanel), BorderLayout.CENTER);
this.add(result, BorderLayout.SOUTH);
}
class MyChooser extends JFileChooser {
#Override
public void approveSelection() {
File f = fileChooser.getSelectedFile();
if (imagePanel.setImage(f)) {
result.setText(f.getName());
} else {
result.setText(title);
}
}
#Override
public void cancelSelection() {
imagePanel.setImage(null);
result.setText(title);
}
}
class MyImagePanel extends JPanel {
private BufferedImage bi;
public MyImagePanel() {
this.setPreferredSize(new Dimension(500, 700));
}
/** Return true if read() succeeded. */
public boolean setImage(File f) {
try {
bi = ImageIO.read(f);
} catch (Exception e) {
bi = null;
}
if (bi != null) {
setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
}
this.revalidate();
this.repaint();
return bi != null;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ImageDisplay("Image Demo").setVisible(true);
}
});
}
}