JPanel with image in it... truncated
Hi, for my app i have used multiple nested jpanels. But in one step of nesting, all the sudden my panels are truncated, i have the picture here. i have noticed that this is due to setLayout(null). When i comment this line in my panel, this works fine, but i need layout to be null because i need to put some more images in it..
http://img527.imageshack.us/img527/2156/picbf1.th.jpg
Please help
when your layout is set to null, then you are not using a layout manager, and you are fully responsible for making sure that all pictures and components are laid out correctly.
Putting multiple images on a panel are not a contraindication for using layout managers. Consider using layout managers here. Either that, or make sure you are setting up all your positions and sizes correctly.
Similar Messages
-
JPanel with Image just doesn't want to show
Hello,
i am trying to create a JPanel with a JLabel to which i assign an ImageIcon, but for some reason the JPanel seems not to appear in my JFrame.
Here's the code: private void jbInit() throws Exception
NumberListener numListener = new NumberListener();
frame = new JFrame();
frame.setTitle("Error Manager");
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
numberList = new JList(numbers);
numberList.addListSelectionListener(numListener);
numberList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane listScroller = new JScrollPane(numberList);
listScroller.setPreferredSize(new Dimension(250, 80));
frame.getContentPane().add(listScroller, gbc);
imgPanel = new JPanel();
imgPanel.setSize(300, 250);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.createImage("Somepic.png");
imgPanel.add(new JLabel(new ImageIcon(img)));
gbc.gridx = 1;
gbc.gridy = 0;
frame.getContentPane().add(imgPanel, gbc);
frame.pack();
frame.setVisible(true);
}Sorry forgot to close the code tags...
private void jbInit() throws Exception
NumberListener numListener = new NumberListener();
frame = new JFrame();
frame.setTitle("Error Manager");
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
numberList = new JList(numbers);
numberList.addListSelectionListener(numListener);
numberList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane listScroller = new JScrollPane(numberList);
listScroller.setPreferredSize(new Dimension(250, 80));
frame.getContentPane().add(listScroller, gbc);
imgPanel = new JPanel();
imgPanel.setSize(300, 250);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.createImage("1_Alpinweiss_III.png");
imgPanel.add(new JLabel(new ImageIcon(img)));
gbc.gridx = 1;
gbc.gridy = 0;
frame.getContentPane().add(imgPanel, gbc);
frame.pack();
frame.setVisible(true);
} -
How to use recursion with images
Ok, for this program I'm trying to recursively repeat an image 3 times with varying widths and heights, and I don't know how to do that with images. With rectangles and other shape objects it's easy because all I had to do was override the draw method, but with images I'm not sure what to override to allow me to repeat the drawing of it with a different height and width. Any help would be greatly appreciated.
Would I be able to work that in with recursion? Currently I have a JPanel with the paintComponent method being overridden, and I've tried setting up the paintComponent method to call itself to repaint the image, but I've realized everytime I resize the application's window paintComponent gets called again making the images go out of site. Is there a way to override the drawImage method to allow me to change the width and height of the image without causing the panel to repaint itself?
-
VJO not defined & image corrupt or truncated
I am getting this error when the computer renders some websites. It happens with ebay & on youtube. It shows the text but no pictures or background. I am running Windows 7 Home Premium 64bit and Firefox 4.0.1 it also happens in the newest version of FF & in safe mode , I have installed the latest Java runtime with no change ,but interstingly if I turn on "in private browsing" FF runs OK.
I have run Firefox in safe mode, I have uninstalled completely using the APPS own uninstaller & then cleaned up all the files left behind with a 3rd party uninstaller I have updated Firefox to the newest release & rolled back to v4.0.1,I have disabled all addons & AV & malware apps (just for a minute)I have even tried a system restore. Also the "VJO not defined" only happens with FireFox & not with IE.interestingly FireFox runs fine if I enable "in private browsing"Clear the cache and the cookies from sites that cause problems.
"Clear the Cache":
* Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
"Remove Cookies" from sites causing problems:
* Tools > Options > Privacy > Cookies: "Show Cookies" -
Problem to create jar with images
Hello
I have a project, it's a frame with a panel that contains a image, I want to create a Jar, but I have to problems, if create a Jar and execute out the project folder, I can't see the images, if I use getClass().getResource, I have other problems, I can't add panels.
How it's difficult to say, I going to put a link from the project make with eclipse [My project|http://www.megaupload.com/?d=HBPHUWE9] , if someone wants have a look I would be grateful.
http://www.megaupload.com/?d=HBPHUWE9
Just look at the package com.constructor.interfaz
To make the jar use the class InterfazFactoriaPaneles
Thanks in advance !
Edited by: Dav1d on Jul 10, 2009 6:11 AM
Edited by: Dav1d on Jul 10, 2009 6:12 AMI explain better:
I have a Class -> InterfazFactoriaPaneles, with this main
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
InterfazFactoriaPaneles application = new InterfazFactoriaPaneles();
application.getJFrame().setVisible(true);
in the Frame I add a JPanel with an image, to load the image I override this method, where com.constructor.imagenes, it's the path where I've got my images :
public void paintComponent(Graphics g){
Dimension tamanio = getSize();
ImageIcon imagenFondo = new ImageIcon("./src/com/constructor/imagenes/patronConstructor.jpg");
g.drawImage(imagenFondo.getImage(),0,0,tamanio.width,tamanio.height,null);
setOpaque(false);
super.paintComponent(g);
the with a menu, I remove the panel with the image I load other panel with components (JTextField, JTable, JLabel,...).
If run the program from eclipse, everything it's ok, if I export into jar file, it works if the jar it's inside the project folder, but if I put the jar in other location, doens't load images, I google from answer and I read to put images path like that: new ImageIcon(getClass().getResource("/com/constructor/imagenes/patronConstructor.jpg")); , but if I do that, and run in eclipse, everything it's ok, but If I export into a Jar, I load the image, but when I try to change to other panel it doesn't work.
So I do not what's the answer, if someone check my code, and try to make a jar, and the move the jar file into other location, will be able to see images doesn't appears. -
Converting JPanels as image objects
hi,
i am having problem in converting a JPanel object which consits of some
JLabels and images into a Image Object. Actually i want to save the JPanel as
a jpg file which i will do with jped encoder.
so please help me in converting this JPanel to image objectassuming the panel is showing.... (if not, you might need to user getPreferredSize() and setVisible before paint()).
Dimension size = panel.getSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
comp.paint(g2d); -
. How does one go about saving a JPanel and its contents to an Image (JPeG, or any other format)? I have tried Double Buffering, but it seems to always capture all the "junk" that is around the JPanel (such as buttons, borders, JFileChoosers, etc.)
. Any ideas would be greatly appreciated.You can create an image and draw the content of any JPanel with its paint method:
int width = panel.getWidth();
int height = panel.getHeight();
Image image = panel.createImage(width, height);
Graphics g = image.getGraphics();
panel.paint(g); -
Screen dump of JPanel to Image
I'm working on an extensive error dialog which will send all relevant information to a support apparatus. This includes collecting the stack-trace received from the exception, java console log and a screen dump of the underlying JPanel.
The dialog is a modal dialog residing on top of the JPanel in question. I can retreive the parent panel's Graphics object and need to print the JPanel with all it's children to an Image object.
This should be possible, but how?
Another thing, is the java console available from the client code? The code runs as an applet on Windows clients.I've never tried this, but the idea I have is to create a BufferedImage, and getGraphics() of the BufferedImage. Now call paint() of the JPanel and pass it the graphics object.
Maybe you also have to pre-obtain the height/width of the JPanel for the size of the Image. -
here is a sample code i need the image to be displayed on button click but the following code is giving some exceptions.
could u please make out where i am going wrong
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JApplet;
import javax.swing.JButton;
public class NewJApplet extends JApplet implements MouseListener{
imagePanel imp;
private JButton draw;
* Initialization method that will be called after the applet is loaded
* into the browser.
public void init() {
imp=new imagePanel(this.getGraphics());
draw=new JButton("draw");
addMouseListener(this);
makegui();
public void makegui()
setLayout(new BorderLayout());
add(BorderLayout.NORTH,draw);
add(BorderLayout.CENTER,imp);
imp.setVisible(false);
public void mouseClicked(MouseEvent e) {
if(e.getSource()==draw)
System.out.println("Clicked");
repaint();
imp.setVisible(true);
public void mousePressed(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
public void mouseReleased(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
public void mouseEntered(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
public void mouseExited(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class imagePanel extends JPanel {
private Image img=null;
Graphics gmain;
public imagePanel(Graphics g)
this.gmain=g;
img=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\sakshi\\My Documents\\NetBeansProjects\\Imagetry1\\1.jpg");
public void paintComponent(Graphics g)
gmain.drawImage(img, 100, 100, null);
System.out.println("printing ");
}>
here is a sample code i need the image to be displayed on button click but the following code is giving some exceptions.
could u please make out where i am going wrong>In addition to the valuable information on the first reply..
1) This applet is attempting to read a File. While it is possible for a signed/trusted applet to create a File and access it, it is usually impractical for an applet, since the File path will not point to anywhere on the server, but to a place on the client file-system. Instead look to use URLs for resources in applets. The Applet class has handy methods for forming URLs to resources relative to the code base or document base. You might need to alter the imagePanel constructor to accept that URL.
2) BTW - imagePanel should be called ImagePanel. The Java nomenclature (naming) conventions indicate EveryWordUpperCaseClass names, camelCaseMethod() names, and camelCaseAttribute names.
3) Sentences should start with a single upper case letter. This helps the reader to quickly scan text, looking for ways they can help. You would not want to make it harder for people who are trying to help, would you?
4) The word 'I' should be always upper case, always.
5) Please take the effort to spell words fully. E.G. 'u' -> 'you'. For the sake of saving two letters, it makes people appear to be lazy dopes. -
<blockquote>Locking duplicate thread.<br>
Please continue here: [[/questions/873950]]</blockquote>
error Image corrupt or truncated: <unknown> what to do ?Jim, thank you for an excellent responce. I truthfully had researched the "Help" section on LightRoom and PhotoShop and found nothing that even remotely resembled your straight forward and easy to follow directions. After upgrading to CS6 and LightRoom 4 I am delighted with all of the new features that Adobe created in PhotoShop. I'm not sure that LightRoom 4 was as great an advancement as PhotoShop itself.
I am certainly going to give your solution a try and see how it works out for me. I concur that I probably do not fully understand and appreciate the relationship between the two programs. Today was my attempt of gaining a better insight to that relationship. Thank you for your response! -
I have refered to this thread to help me create my background image.
http://forum.java.sun.com/thread.jspa?threadID=599393&messageID=3196643
However I am stuck mostly because of not really fully grasping all the objects and how they work yet.
Basically right now I have a JFrame and then I have a container which I put all my objects in.
First question:
What exactly does the following line of code do?
Container contentPane = getContentPane();
Does the "getContentPane()" method get the JFrame, since this is the class this line of code is embedded in?
It sets contentPane which is a container to the JFrame?
Second question...
So using the above link I created a class called BackgroundImage and it works ...sort of. I have two windows - one is my JFrame game and the other is this background frame.
Now I knew this would happen and I realize what I am supposed to do but I have been unsuccessful. Basically I need to make this JPanel my contentPane right?
But when I say something like
JPanel contentPane = panel my JFrame has nothing in it...
How do I take this seperate window and incorporate it into what I already have.
I want to use the contentPane variable since this is the variable where everything gets added throughout the code? I can't seem to get the JPanel to work.
Take a look at some code snippets:
public class DungMast extends JFrame
/** Creates a new instance of EventPress */
public DungMast() {
Animation_Complete=false;
Current_Level = 1;
Level_Complete = false;
CreateUserInterface();
JPanel panel = new JPanel()
protected void paintComponent(Graphics g)
// Dispaly image at at full size
g.drawImage(img_BG.getImage(), 0, 0, null);
// Scale image to size of component
// Dimension d = getSize();
// g.drawImage icon.getImage(), 0, 0, d.width, d.height, null);
// Fix the image position in the scroll pane
// Point p = scrollPane.getViewport().getViewPosition();
// g.drawImage(icon.getImage(), p.x, p.y, null);
super.paintComponent(g);
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(400, 400) );
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );
JButton button = new JButton( "Hello" );
panel.add( button );
} //end constructor for main JFrame
//Then I have something like
JPanel contentPane = panel;
}What exactly does the following line of code do?
Container contentPane = getContentPane();
It gets the content pane for the JFrame and saves a reference to it for later use, viz,
the reference "contentPane" can be used to call methods in the Container class.
Lightweight (Swing) top–level containers have a content pane that holds the child
components for the parent container. See the comments section of the JRootPane class api
for a birds–eye view.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class BackgroundImage {
private JPanel getContent(BufferedImage image) {
// Make up our own content pane with an image
// in the background.
ContentPanel cp = new ContentPanel(image);
// Add some components.
cp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
for(int j = 0; j < 10; j++) {
gbc.gridwidth = (j+1)%3 == 0 ? gbc.REMAINDER : 1;
cp.add(new JButton(String.valueOf(j+1)), gbc);
return cp;
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("images/cougar.jpg"));
BackgroundImage test = new BackgroundImage();
JFrame f = new JFrame();
System.out.println("Default contentPane = " +
f.getContentPane().getClass().getName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(test.getContent(image));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
class ContentPanel extends JPanel {
BufferedImage image;
public ContentPanel(BufferedImage image) {
this.image = image;
// This is required for JComponents that are used
// as content panes since the Ocean LookAndFeel
// (which can have non-opaque panels) was introduced.
// Content panes must be opaque so they can draw
// themseves and their child components.
setOpaque(true);
System.out.println("Default layout manager = " +
getLayout().getClass().getName());
protected void paintComponent(Graphics g) {
// Fill background with opaque color.
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
int x = (w - image.getWidth())/2;
int y = (h - image.getHeight())/2;
g.drawImage(image, x, y, this);
} -
JPanel and image (alreday discussed) why this is not working ?
import javax.swing.*;
import java.awt.*;
public class test{
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JLabel label = new JLabel("Hello World");
//frame.getContentPane().add(label);
Image img = Toolkit.getDefaultToolkit().getImage("fond.jpg");
DrawingPanel drawingPanel = new DrawingPanel(img);
frame.getContentPane().add(drawingPanel);
frame.pack();
frame.setVisible(true);
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
class DrawingPanel extends JPanel {
private Image img = null;
DrawingPanel(Image image)
{ img = image; }
public void paintComponent(Graphics g) {
// First paint background unless you will
// paint whole area yourself.
super.paintComponent(g);
// Use the image width & height to find the starting point
int msgX = getSize().width/2 - img.getWidth(this);
int msgY = getSize().height/2 - img.getHeight(this);
//Draw image at centered in the middle of the panel
g.drawImage(img, msgX, msgY, this);
}well yes if i do this, the label will appear but not
the drawing on the jpannel.Ahh, now you write this. If I were you, I'd stop using Image and start using BufferedImage -- it's
easier to work with. This post gives you the basics: http://forum.java.sun.com/thread.jsp?forum=20&thread=522483 -
How to add a JPanel with label and border line
hi,
I want a Jpanel with label and border line like this.Inside it i need to have components.Is there a resuable component to bring this directly??
Any solution in this regards.???
Label-----------------------------------------------------------
| |
| |
| |
| |
| |
|________________________________________ |[url http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html]How to Use Borders
-
I want to make a slideshow to view on my television with image's duration ranging from seconds to an hour or more and I want to use my iPhone or iPad mini to control the television through my Apple tv.
I've been unable to locate an app that will do this. The Photos app that comes with the phone or iPad has extremely limited duration controls. PhotoStream seems to load everything from my phone or iPad not allowing me to just load up a set group of images.
iPhoto for iPad is getting some terrible recent reviews. I tried a couple other free apps but they don't use Airplay.
I can do something like this with iPhoto and my MB Air, but it's kind of ugly to have the computer open in order to connect by Airplay to my Apple TV. I've thought the iPad or iPhone would be a lower profile controller.
Am I out of luck?Thanks for your help. Since I'm uninterested in loading all my photos (the only option) into photostream, I won't be able to use the settings in ATV. I guess I'm just stuck with using iPhoto on my MB Air. Thanks again.
paul -
WYSIWYG editor upload with images
Hello, friends!
I need help solving following problem:
I need to create WYSIWYG editor, which just to enter text and image. Other options (like font-size, font-weight...) not need. Then when submit the form with richedit, all data in form with images must sent to server.
1) images must upload
2) text must content following: text <img="src=../image1.png"> text <img="src=../image2.png"> text
or maybe all images must save as a blob data
Help me, people!Is this an Oracle question?
Maybe you are looking for
-
Not printing text only on Canon--grahics print ok. Not printer problem. Prints ok on a Brother and another network Apple prints ok on Canon. Believe a computer setting was inadvertently reset. What setting and how do I reset?
-
Unable to view the contents in NQS Config file
Hi Forum, After I successfully install the OBI EE 10.1.3 Application, I am unable to view the contents in NQS Config file. It is opening in Notepad with error showing as "The filename, directory name or volume label syntax is incorrect." So please gu
-
I AM LOOKING FOR A REPLACEMENT CHARGER FOR MY LAPTOP OR A NEW PORT PLUG TO REPAIR MINE
DO YOU HAVE A REPLACEMENT CHARGER AT A RESONABLE PRICE OR A REPLACEMENT TIP "C"
-
Can't get rid of duplicate email accounts itunes imports from outlook
Hey everybody, iTunes imports my email account information from Outlook and for some reason it finds duplicate email accounts. I only have one .pst file which only has one of each email account attached to it but iTunes finds duplicates. So in iTunes
-
Can't connect to wifi on my iPad 2
I'm on a college campus and I can't connect to the wifi with my iPad 2. On both my MacBook Pro and iPhone 4 there are 5 different networks I can use, but on my iPad not one will show up. How can I connect to the wifi? I've tried "choosing the network