JPanel updates.....

T

tiewknvc9

Hi thanks for reading.

I have creating a gui that contains a JPanel containing an ImageIcon as
a background and a scrollpane in front of it.

I am trying to update the gui when the user resizes the screen, however
I find that when the user resizes the screen EITHER

1. the Image background does not show up at all
2. The JScrollPane does not move its position,,,,

When the screen is resized I ...

m_pBackImage.invalidate();
validate();

this makes the JScrollPane move at the right time, but makes the
background JPanel not draw...

but if I do this...

validate();
m_pBackImage.invalidate();

then the JPanel image shows and the JScrollPane doesnt move until the
2nd screen resize...

any ideas?
 
K

Knute Johnson

tiewknvc9 said:
Hi thanks for reading.

I have creating a gui that contains a JPanel containing an ImageIcon as
a background and a scrollpane in front of it.

I am trying to update the gui when the user resizes the screen, however
I find that when the user resizes the screen EITHER

1. the Image background does not show up at all
2. The JScrollPane does not move its position,,,,

When the screen is resized I ...

m_pBackImage.invalidate();
validate();

this makes the JScrollPane move at the right time, but makes the
background JPanel not draw...

but if I do this...

validate();
m_pBackImage.invalidate();

then the JPanel image shows and the JScrollPane doesnt move until the
2nd screen resize...

any ideas?

A lot of ideas. Lose the ImageIcon. Draw your image on the JPanel, add
the JPanel to the ScrollPane, add the ScrollPane to a JFrame, size the
JFrame as desired. As a basic rule you only need to revalidate if you
have added or removed components. I don't think you are doing that.

Please remember if you want help with your code that it is much better
for us to see the actual code!

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;

public class ImagePanel extends JPanel {
BufferedImage bi;

public ImagePanel(String fname) {
try {
bi = ImageIO.read(new File(fname));
setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

public void paintComponent(Graphics g) {
if (bi == null)
g.drawString("No Image",10,40);
else
g.drawImage(bi,0,0,null);
}

public static void createGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImagePanel ip = new ImagePanel("saturn.jpg");
JScrollPane sp = new JScrollPane(ip);
f.add(sp);
f.setSize(300,200);
f.setVisible(true);
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
createGUI();
}
};
EventQueue.invokeLater(r);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top