views:

72

answers:

1

I'm using Java Swing graphical editor with netbeans to make my project...but using it brings some limitations like I can't add to a jpanel an image,using java swing options. So i'll need to code it, implementing a new jPanel.

My problem is that the code generated by the java swing graphical editor can't be edited so instead of adding the new JPanel code in the initComponents() section I'm doing it after this function is called in the constructor of my main JPanel.

But any code I add is not recognized by the "Designer" which means that after making my coded objects I can't use them in the "Designer" and everything must be coded, which is a pain considering how much easier is previewing and moving elements in the "Designer" tool.

How can I code what I want but steel appear in the "DEsigner"?

Thx in advance

+1  A: 

Here are two ways to add an image to a JPanel using the NetBeans GUI editor. The class ImagePanel below is created using the New JPanel Form command.

Non-designer: The first approach modifies the constructor to set a background image, and the class overrides paintComponent() to draw the image. No code inside the editor fold changes.

Designer: Using the GUI designer, the second approach adds a JLabel named imageLabel. The code to create the JLabel with a centered Icon goes in the property named Custom Creation Code, while the following two lines go in the Post-Creation Code.

package overflow;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class ImagePanel extends javax.swing.JPanel {

    private Image image;

    /** Creates new form ImagePanel */
    public ImagePanel(Image image) {
        this.image = image;
        this.setPreferredSize(new Dimension(
            image.getWidth(null), image.getHeight(null)));
        initComponents();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        imageLabel = new JLabel(new ImageIcon("image2.jpg"), JLabel.CENTER);
        imageLabel.setHorizontalTextPosition(JLabel.CENTER);
        imageLabel.setVerticalTextPosition(JLabel.TOP);

        setLayout(new java.awt.GridLayout());

        imageLabel.setText("imageLabel");
        add(imageLabel);
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JLabel imageLabel;
    // End of variables declaration

}

Here's a suitable Main class and main() method to display the panel:

package overflow;

import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                try {
                    f.add(new ImagePanel(ImageIO.read(new File("image1.jpg"))));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

http://i41.tinypic.com/dmw4nl.png

trashgod