I'm trying to make an image processing frame similar to one found in something like Photoshop or Paint Shop Pro and I'm running into problems.
Right now I have a JFrame window with a JDesktopPane. When I click a button, a JInternalFrame is made with the following components in it:
imageLabel = new JLabel("picture.png");
scrollPane.setViewPort(imageLabel);
internalFrame.add(scrollPane); // I also tried with a BorderLayout()
desktopPane.add(internalFrame);
My problem is this: I don't want the JLabel or the JScrollPane to stretch to the size of the JInternalFrame if the JLabel is smaller than the JInternalFrame.
I've tried padding the space around the JLabel with "empty" JLabels. I've tried switching layout styles of the JScrollPane. I've tried setting the preferred and maximum sizes of the JLabel and the JScrollPane to that of picture.png. None of it works for what I need. I don't want the blank "space" around the JLabel to be a part of the JScrollPane or the JLabel so that I can use various MouseEvents to trigger on the picture itself rather than the space left by the "stretched" JLabel or JScrollPane whenever I resize the JInternalFrame.
Thanks in advance.
Edit1: Here is a bit of code that highlights the problem.
import java.awt.*;
import java.awt.event.*;
class fooFrame extends JFrame implements MouseListener
{
private static fooFrame frame;
JLabel fooLabel;
public fooFrame()
{
JDesktopPane background = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setSize(500, 500);
internalFrame.setLocation(20, 20);
internalFrame.setVisible(true);
Image image = Toolkit.getDefaultToolkit().getImage("test.gif");
fooLabel = new JLabel(new ImageIcon("test.gif"));
fooLabel.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
JScrollPane fooScrollPane = new JScrollPane(fooLabel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
fooScrollPane.setPreferredSize(new Dimension(fooLabel.getWidth(), fooLabel.getHeight()));
fooScrollPane.setViewportView(fooLabel); // add JLabel to JScrollPane
internalFrame.add(fooScrollPane); // add JScrollPane to JInternalFrame
background.add(internalFrame); // add JInternalFrame to JDesktopPanel
this.setContentPane(background); // add JDesktopPanel to JFrame
fooLabel.addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Clicked the picture.");
}
public void mouseEntered(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Entered the picture.");
}
public void mouseExited(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Exited the picture.");
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public static void createAndShowGUI()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
frame = new fooFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("foo");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You will have to get your own "test.gif" and if you make the internalFrame larger than the picture it fills the remaining space with the label. As all the mouseEvents fire when I cross the internalFrame rather than onto the picture like I want to have happen.
Edit2: Code modified with Kleopatra's suggestions.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class a1
{
public static void main(String[] args)
{
fooFrame.createAndShowGUI();
}
}
class fooFrame extends JFrame implements MouseListener
{
private static fooFrame frame;
JLabel fooLabel;
public fooFrame()
{
JDesktopPane background = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("Internal Frame", true, true, true, true);
internalFrame.setLocation(20, 20);
internalFrame.setVisible(true);
internalFrame.pack();
Image image = Toolkit.getDefaultToolkit().getImage("test.gif");
fooLabel = new JLabel(new ImageIcon(image));
fooLabel.setBorder(new LineBorder(Color.PINK));
JScrollPane fooScrollPane = new JScrollPane((fooLabel), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
internalFrame.setLayout(new BoxLayout(internalFrame.getContentPane(), BoxLayout.LINE_AXIS));
fooScrollPane.setViewportView(fooLabel); // add JLabel to JScrollPane
internalFrame.add(fooScrollPane); // add JScrollPane to JInternalFrame
background.add(internalFrame); // add JInternalFrame to JDesktopPanel
this.setContentPane(background); // add JDesktopPanel to JFrame
fooLabel.addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Clicked the picture.");
}
public void mouseEntered(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Entered the picture.");
}
public void mouseExited(MouseEvent me)
{
if (me.getSource() == fooLabel)
System.out.println("Exited the picture.");
}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
@Override
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public static void createAndShowGUI()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e)
{
}
frame = new fooFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("foo");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Question&Answers:
os