You could write a subclass of JTabbedPane to add these extra methods.
But you'll have to provide a way to change layout managers and add
components with constraints on the tabs.
Anyway, in the long run I can't see this making the code much cleaner
(only the need for one panel per tab is removed, so all other
components, subpanels, ... still need to be declared and placed.
Bart
> You could write a subclass of JTabbedPane to add these extra methods.
> But you'll have to provide a way to change layout managers and add
[quoted text clipped - 4 lines]
>
> Bart
Thanks,
I thinks subclassing JTabbedPane requires more work so ill stick with
just adding everything to a JPanel.
Another problem im having at the moment, is that i have written a
subclass of JPanel that displays and image on itself, then i want to
put this ImagePanel onto a Tab in a JTabbedPane. The problem is that
the JTabbedPane is not resizing to fit the ImagePanel, so you only see
half or some of the image.
Here is the code for ImagePanel.java
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.*;
public class ImagePanel extends JPanel
{
/**
* This member variable holds the currently displayed image.
*/
private BufferedImage mImage;
public ImagePanel()
{
this.mImage = null;
this.setPreferredSize(new Dimension(410, 50));
}
/**
* loadImage() loads an image, using a MediaTracker to ensure
* that the image
* data is fully loaded. Then it converts the image to a
BufferedImage.
* Finally, it adjusts the window size and placement based on the
new
* image
* size.
*
* @param fileName The path of the image to load.
* @return A BufferedImage object containing the loaded image.
*/
public BufferedImage loadImage(String fileName)
{
// Use a MediaTracker to fully load the image.
Image image = Toolkit.getDefaultToolkit().getImage(fileName);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
} catch (InterruptedException ie)
{
return null;
}
if (mt.isErrorID(0))
return null;
// Make a BufferedImage from the Image.
mImage = new BufferedImage(image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mImage.createGraphics();
g2.drawImage(image, null, null);
//this.setPreferredSize(new Dimension(image.getWidth(null)
,
image.getHeight(null)));
validate();
repaint();
return mImage;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (mImage == null)
return;
Insets insets = getInsets();
g.drawImage(mImage, insets.left, insets.top, null);
}
}
BartCr - 21 Dec 2005 06:36 GMT
Change your paintComponent like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (mImage == null) {
return;
}
Insets insets = getInsets();
Dimension size = getSize();
g.drawImage(mImage, insets.left, insets.top, size.width -
insets.left - insets.right,
size.height - insets.top - insets.bottom,
this);
}
}
other drawImage methods on graphics allow to resize the painted image
while painting
Bart
placid - 21 Dec 2005 06:58 GMT
> Change your paintComponent like this:
>
[quoted text clipped - 17 lines]
>
> Bart
Your a life saver man, il work on that now so that it only shows the
original size of the image.
Is it possible to display the image (original size, with no scaling) in
a confined space but allow scollbars to scroll the image horizontally
and vertically ?
Knute Johnson - 21 Dec 2005 19:19 GMT
>>Change your paintComponent like this:
>>
[quoted text clipped - 24 lines]
> a confined space but allow scollbars to scroll the image horizontally
> and vertically ?
Sure. Put your image JPanel in a JScrollPane and add the JScrollPane to
the JTabbePane.

Signature
Knute Johnson
email s/nospam/knute/
placid - 22 Dec 2005 03:25 GMT
> >>Change your paintComponent like this:
> >>
[quoted text clipped - 27 lines]
> Sure. Put your image JPanel in a JScrollPane and add the JScrollPane to
> the JTabbePane.
if implement a MouseListener with my ImagePanel, with the following
code;
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
the (x,y) co-ordinates will not belong to the Image being display ?
How would i get the pixel co-ordinate that the mouse was clicked at ?
Knute Johnson - 22 Dec 2005 05:22 GMT
>>>>Change your paintComponent like this:
>>>>
[quoted text clipped - 39 lines]
>
> How would i get the pixel co-ordinate that the mouse was clicked at ?
Here you go:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class test8 extends JPanel {
BufferedImage image;
public test8(String fname) throws IOException {
image = ImageIO.read(new File(fname));
setPreferredSize(new
Dimension(image.getWidth(),image.getHeight()));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me.getX()+" "+me.getY());
}
});
}
public void paintComponent(Graphics g) {
g.drawImage(image,0,0,null);
}
public static void createGUI() {
try {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
test8 t8 = new test8("saturn.jpg");
JScrollPane sp = new JScrollPane(t8);
sp.setPreferredSize(new Dimension(400,300));
tp.add(sp);
f.add(tp);
f.pack();
f.setVisible(true);
} catch (IOException ioe) {
System.out.println("Can't read image file");
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
createGUI();
}
};
EventQueue.invokeLater(r);
}
}

Signature
Knute Johnson
email s/nospam/knute/
placid - 22 Dec 2005 06:24 GMT
> >>>>Change your paintComponent like this:
> >>>>
[quoted text clipped - 98 lines]
> }
> }
thats simpler than my code (the following code is how i use to load an
image), i use a MediaTracker to wait for the image to load up, but i
dont really need that at the moment so il just use ImageIO.read(..)
which is way better, thanks again/
private BufferedImage mImage;
public BufferedImage loadImage(String fileName)
// Use a MediaTracker to fully load the image.
/*Image image = Toolkit.getDefaultToolkit().getImage(fileName);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
} catch (InterruptedException ie)
{
return null;
}
if (mt.isErrorID(0))
return null;
// Make a BufferedImage from the Image.
mImage = new BufferedImage(image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_RGB);*/
Graphics2D g2 = mImage.createGraphics();
g2.drawImage(mImage, null, null);
this.setPreferredSize(new Dimension(mImage.getWidth(null), mImage
.getHeight(null)));
validate();
repaint();
return mImage;
}