Article View: borland.public.jbuilder.java.api
Article #1198help, GUI redrawing is obviously confusing me
From: PeterH
Date: Sat, 27 Apr 2002 02:40
Date: Sat, 27 Apr 2002 02:40
239 lines
8376 bytes
8376 bytes
I have a class here that is a piece of a project and I'm just about to
scream! :< The code seems to do absolutely everything but what I thought I
was coding it to do. :< Can you take a look and see what I'm doing wrong to
have the right hand side panel be painted exactly like the left hand side
panel, even though it's totally another class that is supposed to have an
image in it?
I think I'm doing something very wrong with my last method, but I just don't
know what?
Any help would be appreciated. Thank you very much.
----------------------------------------------------------------
package com.pchenry.photomaster;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
/**
* Description of the Class
*
*@author peter
*@created April 26, 2002
*/
public class GroupPhotos extends JPanel {
JLabel lblFrom = new JLabel();
JTextField txtFrom = new JTextField();
JButton cmdBrowseFrom = new JButton();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JList lstPhotos = new JList();
JLabel lblPhotos = new JLabel();
Photo pnlPhotos = new Photo();
/**
* Description of the Method
*/
public void requestFromFocus() {
txtFrom.requestFocus();
}
/**
* Constructor for the GroupPhotos object
*/
public GroupPhotos() {
try {
jbInit();
} catch( Exception ex ) {
ex.printStackTrace();
}
}
/**
* Description of the Method
*
*@exception Exception Description of the Exception
*/
void jbInit() throws Exception {
lblFrom.setText( "From:" );
this.setLayout( gridBagLayout1 );
cmdBrowseFrom.setText( "..." );
cmdBrowseFrom.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed( ActionEvent e ) {
cmdBrowseFrom_actionPerformed( e );
}
} );
lblPhotos.setText( "Photos" );
txtFrom.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
txtFrom_actionPerformed( e );
}
} );
lstPhotos.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
lstPhotos.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged( ListSelectionEvent e ) {
lstPhotos_selectionChange( e );
}
} );
this.add( lblFrom, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 5,
5, 5, 5 ), 0, 0 ) );
this.add( txtFrom, new GridBagConstraints( 1, 0, 2, 1, 1.0, 0.0
, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new
Insets( 5, 5, 5, 5 ), 0, 0 ) );
this.add( cmdBrowseFrom, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets( 5,
5, 5, 5 ), 0, 0 ) );
this.add( lblPhotos, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets( 5,
5, 5, 5 ), 0, 0 ) );
JScrollPane scrollPane = new JScrollPane( lstPhotos );
this.add( scrollPane, new GridBagConstraints( 1, 1, 1, 1, 0.0, 1.0
, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new
Insets( 5, 5, 5, 5 ), 100, 200 ) );
this.add( pnlPhotos, new GridBagConstraints( 2, 1, 2, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
5, 5, 5, 5 ), 200, 200 ) );
}
/**
* Description of the Method
*
*@param e Description of the Parameter
*/
void cmdBrowseFrom_actionPerformed( ActionEvent e ) {
String currentPath = txtFrom.getText();
if( currentPath == null || currentPath.length() == 0 ) {
currentPath = ".";
}
JFileChooser fc = new JFileChooser( currentPath );
fc.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
fc.addChoosableFileFilter( new ImageFilter() );
fc.setDialogTitle( "Please Select the Digital Photography
Directory" );
int retval = fc.showOpenDialog( this );
if( retval == JFileChooser.APPROVE_OPTION ) {
File f = fc.getSelectedFile();
if( f.isFile() ) {
f = f.getParentFile();
}
//if
try {
txtFrom.setText( f.getCanonicalPath() );
} catch( IOException ioe ) {
System.err.println( "An error has occurred with retrieving the
path of the filename '" + f.getAbsolutePath() + "'" );
ioe.printStackTrace();
}
updatePhotos();
}
//if
}
//cmdBrowseFrom_actionPerformed
/**
* Description of the Method
*/
private void updatePhotos() {
File fromPath = new File( txtFrom.getText() );
lstPhotos.setListData( fromPath.listFiles() );
}
/**
* Description of the Class
*
*@author peter
*@created April 26, 2002
*/
private class ImageFilter extends FileFilter {
/**
* Description of the Method
*
*@param f Description of the Parameter
*@return Description of the Return Value
*/
public boolean accept( File f ) {
if( f.isDirectory() ) {
return true;
} else {
String extension = getExtension( f );
if( ( extension.toUpperCase().equals( "JPG" ) ) || (
extension.toUpperCase().equals( "JPEG" ) ) || (
extension.toUpperCase().equals( "GIF" ) ) ) {
return true;
} else {
return false;
}
}
}
//accept
/**
* Gets the description attribute of the ImageFilter object
*
*@return The description value
*/
public String getDescription() {
return "Image Files (*.JPG, *.JPEG, *.GIF)";
}
}
/**
* Gets the extension attribute of the GroupPhotos object
*
*@param f Description of the Parameter
*@return The extension value
*/
private String getExtension( File f ) {
String filename = f.getName();
int extensionLoc = filename.lastIndexOf( '.' );
if( ( extensionLoc > 0 ) && ( extensionLoc < filename.length() - 1 ) )
{
String extension = filename.substring( extensionLoc + 1,
filename.length() );
return extension;
} else {
return "";
}
//if
}
/**
* Description of the Method
*
*@param e Description of the Parameter
*/
void txtFrom_actionPerformed( ActionEvent e ) {
if( !txtFrom.getText().trim().equals( "" ) ) {
updatePhotos();
}
}
/**
* Description of the Method
*
*@param e Description of the Parameter
*/
void lstPhotos_selectionChange( ListSelectionEvent e ) {
File filePhoto = ( File )lstPhotos.getSelectedValue();
pnlPhotos.setImage( filePhoto.toString() );
pnlPhotos.paintComponent( getGraphics() );
}
}
Technical Details
Message-ID:
<3cca1147_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!feed1.news.be.easynet.net!skynet.be!skynet.be!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,da4fdf29b67dc0d2,start X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-04-26 19:58:47 PST From: "PeterH" <!Sorry-TooMuchSpam!nope@nope.com> Newsgroups: borland.public.jbuilder.java.api Subject: help, GUI redrawing is obviously confusing me Date: Fri, 26 Apr 2002 22:40:34 -0400 Lines: 242 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 24.112.10.56 Message-ID: <3cca1147_2@dnews> X-Trace: dnews 1019875655 24.112.10.56 (26 Apr 2002 19:47:35 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!195.40.4.120.MISMATCH!easynet-quince!easynet.net!feed1.news.be.easynet.net!skynet.be!skynet.be!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews