Hello everyone
I encounter a weird problem: I have several JPanels (ImageObject) on a
JPanel (topPanel) inside a JScrollPane (both inside Folder) - the
ImageObjects get displayed fine - but the JCheckBox and JRadioButton
which are positioned on the ImageObject (which is a subclass of JPanel)
are not clickable. To say it right: they get the WinXP-L&F "orange
glow" when I move the mouse pointer over them - but when I click them
the do not change state. They don't fire an ItemState or
ActionPerformed event either.
Here's the code - maybe one of you Java-Gurus can see what I'm doing
wrong...
The Folder class is the container so to speak - it contains the
JScrollPane and the topPanel. ImageObjects (see below) get added to the
topPanel via the addImage() method in Folder.
The ImageObject class below contains the problematic
JCheckBox/JRadiobutton (selectedCheckBox, groupRadioButton).
Well, thanks a lot for having a look at the problem!
al
public class Folder extends JPanel implements ActionListener,
ItemListener {
private DevelopmentFrame parent;
private JScrollPane scrollPane;
//private JTable table;
//private ImageTableModel myTableModel;
private JPopupMenu picPopup;
private JPanel topPanel;
private int colCount;
private int rowCount;
private JPanel bottomPanel;
private JButton dialogOpenButton;
private JCheckBox quickaccessCheckbox;
private JTextField quickaccessInput;
private ButtonGroup radioGroup;
/** Creates a new instance of Folder */
public Folder(JPopupMenu picPopup, DevelopmentFrame parent) {
this.setLayout(new FlowLayout());
this.bottomPanel = new JPanel();
this.bottomPanel.setLayout(new
org.netbeans.lib.awtextra.AbsoluteLayout());
this.radioGroup = new ButtonGroup();
this.parent = parent;
this.dialogOpenButton = new javax.swing.JButton();
this.dialogOpenButton.setText(parent.getResourceBundle().getString("selecteFolderOnHarddrive"));
this.dialogOpenButton.setActionCommand("openDirDialog");
this.dialogOpenButton.addActionListener(this);
this.quickaccessCheckbox = new
JCheckBox(parent.getResourceBundle().getString("quickaccessCodeForFolder"));
this.quickaccessCheckbox.setActionCommand("showQuickaccessInput");
this.quickaccessCheckbox.addItemListener(this);
this.quickaccessInput = new JTextField();
this.quickaccessInput.setPreferredSize(new Dimension(140,20));
this.quickaccessInput.setVisible(false);
this.colCount = 0;
this.rowCount = 0;
this.topPanel = new JPanel();
this.topPanel.setBackground(Color.WHITE);
this.topPanel.setLayout(new GridBagLayout());
this.scrollPane = new JScrollPane(topPanel);
this.scrollPane.setPreferredSize(new Dimension(700,325));
this.bottomPanel.add(this.quickaccessCheckbox, new
org.netbeans.lib.awtextra.AbsoluteConstraints(0,0));
this.bottomPanel.add(this.quickaccessInput, new
org.netbeans.lib.awtextra.AbsoluteConstraints(210,2) );
this.bottomPanel.add(this.dialogOpenButton, new
org.netbeans.lib.awtextra.AbsoluteConstraints(450,0));
this.add(this.scrollPane);
this.add(this.bottomPanel);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("openDirDialog")) {
parent.openDirectoryDialog();
}
}
public void itemStateChanged(ItemEvent evt) {
Object source = evt.getItemSelectable();
if (source == this.quickaccessCheckbox) {
boolean state = false;
if (evt.getStateChange() == ItemEvent.SELECTED) {
state = true;
}
this.quickaccessInput.setVisible(state);
}
}
public void addImage(ImageObject img) {
img.setButtonGroup(this.radioGroup);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 0.5;
c.anchor = c.CENTER;
c.gridx = this.colCount;
c.gridy = this.rowCount;
c.insets = new Insets(5,5,5,5);
this.topPanel.add(img, c);
this.topPanel.repaint();
if (this.colCount == 3){
this.colCount = 0;
this.rowCount++;
} else {
this.colCount++;
}
}
}
public class ImageObject extends JPanel implements ItemListener,
ActionListener {
private String filename = new String("");
private java.awt.Image curImage;
private java.awt.Image curThumbnail;
private File file;
private MediaTracker tracker;
private JCheckBox selectedCheckBox;
private JRadioButton groupRadioButton;
private JPanel optionsPanel;
private DevelopmentFrame parent;
private boolean selectedPic = false;
private boolean groupPic = false;
/** Creates a new instance of ImageObject */
public ImageObject(String filename, DevelopmentFrame parent) {
this.setParent(parent);
this.setFilename(filename);
this.loadImage(this.file);
this.init();
}
public void itemStateChanged(ItemEvent evt) {
Object source = evt.getItemSelectable();
if (source == this.selectedCheckBox) {
boolean state = false;
if (evt.getStateChange() == ItemEvent.SELECTED) {
state = true;
}
this.selectedPic = state;
}
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == this.selectedCheckBox) {
this.selectedCheckBox.setSelected(true);
}
}
public ImageObject(File imageFile, DevelopmentFrame parent) {
this.setParent(parent);
this.file = imageFile;
this.loadImage(imageFile);
this.init();
}
public ImageObject() {
}
public void setButtonGroup(ButtonGroup radioGroup) {
radioGroup.add(this.groupRadioButton);
}
public void init() {
Color bgColor = new Color(152,186,255);
this.setPreferredSize(new Dimension(150, 190));
this.setSize(150,190);
this.setMinimumSize(new Dimension(150, 190));
this.selectedCheckBox = new
JCheckBox(parent.getResourceBundle().getString("selectedImage"));
this.groupRadioButton = new
JRadioButton(parent.getResourceBundle().getString("groupImage"));
this.selectedCheckBox.setBackground(bgColor);
this.selectedCheckBox.addItemListener(this);
this.groupRadioButton.setBackground(bgColor);
this.groupRadioButton.setVisible(false);
this.setBackground(Color.WHITE);
this.optionsPanel = new JPanel();
this.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
this.optionsPanel.setSize(this.getWidth(), 40);
this.optionsPanel.setBackground(bgColor);
this.optionsPanel.add(selectedCheckBox);
this.optionsPanel.add(groupRadioButton);
this.add(this.optionsPanel, new
org.netbeans.lib.awtextra.AbsoluteConstraints(0, 155, 155, 30));
}
public void addPopupMenu(JPopupMenu popup) {
this.addMouseListener(new PopupListener(popup));
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
try {
this.filename = filename;
this.file = new File(filename);
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadImage(File file) {
try {
curImage = Toolkit.getDefaultToolkit().getImage(
file.getAbsolutePath() );
} catch (Exception e) {
e.printStackTrace();
}
}
public java.awt.Image getImage() {
return this.curImage;
}
public java.awt.Image getThumbnail() {
if (this.curThumbnail == null) {
this.createThumbnail();
}
return this.curThumbnail;
}
public void createThumbnail() {
float newWidth;
float newHeight;
if (this.curImage.getWidth(null) >
this.curImage.getHeight(null)) {
// längsformat
newWidth = 150;
newHeight (newWidth/this.curImage.getWidth(null))*this.curImage.getHeight(null);
// bildli für vorne nur landscape
this.groupRadioButton.setVisible(true);
} else {
newHeight = 150;
newWidth (newHeight/this.curImage.getHeight(null))*this.curImage.getWidth(null);
}
this.curThumbnail = this.curImage.getScaledInstance(new
Float(newWidth).intValue(),new Float(newHeight).intValue(),
this.curImage.SCALE_SMOOTH);
}
protected void paintComponent( Graphics g ) {
this.setBackground(Color.WHITE);
tracker = new MediaTracker(this);
getThumbnail();
tracker.addImage(this.curThumbnail, 2);
try {
tracker.waitForID(2);
} catch(Exception e) {
e.printStackTrace();
}
super.paintComponent(g);
if ( this.curImage != null ) {
g.drawImage(this.curThumbnail, 0,0, this);
}
}
public DevelopmentFrame getParent() {
return parent;
}
public void setParent(DevelopmentFrame parent) {
this.parent = parent;
}
}
John McGrath - 17 Mar 2005 05:03 GMT
> Here's the code - maybe one of you Java-Gurus can see what I'm doing
> wrong...
There is way too much code here - you should be able to pare this down to
something smaller that exhibits the problem. And at the same time, it is
not complete; it references some classes (e.g. DevelopmentFrame) that are
not defined and some third-party classes (e.g. AbsoluteLayout). It would
be far better to post a runnable example of the problem. That way, others
can try it out and see what is going wrong. Andrew Thompson has a very
good article describing how to post such examples at:
http://www.physci.org/codes/sscce.jsp
> public DevelopmentFrame getParent() {
> return parent;
[quoted text clipped - 3 lines]
> this.parent = parent;
> }
That said, this sticks out like a sore thumb. I presume you are using
Java 1.5, since this would not compile with earlier versions of Java.
Covariant return types make this legal in Java 1.5, but I suspect you did
not intend to override the Container getParent() method. As a result,
some code is going to ask an ImageObject instance for its parent (which
should be the topPanel) and be told it is the DevelopmentFrame. This
could cause untold havoc in the Swing/AWT code. I cannot say for certain
that this is causing your problem, but it certainly could.

Signature
Regards,
John McGrath
al schmid - 17 Mar 2005 11:17 GMT
John,
thanks for the heads up in respect to posting code... I'll try to do
better next time.
thanks for the idea about the getParent() method - that was something I
was not thinking of. And yes, I am using Java 1.5.
Regards,
al schmid
al schmid - 17 Mar 2005 11:47 GMT
John,
update: you were right. I changed the getParent() method in all my
classes to getParentFrame() and now it works.
thanks a lot!
al