Re: Programming question



Hi Mike,

I would most likely write a custom class that extends a JFrame or
JDialog and implements a PropertyChangeListener, then register that
class as a propertychangelistener with the 'host' dialog or frame using
the addPropertyChangeListener(PropertyChangeListener pcl) method. Then
the 'host' just needs to fire a propertychange when selected object
changes.

Here is a JPanel that I use in a situation where a user selects clipart
in a table and that table sends an event to this JPanel. Below the
custom panel is the table that fires the propertychange. The table is
dependant on some other classes that access database connections and a
custom table model class so you will not be able to compile it but it
might give you some good clues.

Good Luck,
Adam

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;

/**
*
* @author a
*/
public class beanJPanel_clipart_PCL extends javax.swing.JPanel
implements PropertyChangeListener {

private int width, height;
private ImageIcon icon;
private Image image;
private static final int ACCSIZE = 400;
private Color bg;
private String Path = "EMPTY";


public beanJPanel_clipart_PCL(){
initComponents();
this.setSize(300, 300);
}

public void setPath(String path)
{
this.Path = path;
if ((Path != null) &&
Path.toLowerCase().endsWith(".jpg") ||
Path.toLowerCase().endsWith(".jpeg") ||
Path.toLowerCase().endsWith(".gif") ||
Path.toLowerCase().endsWith(".png"))
{
icon = new ImageIcon(Path);
image = icon.getImage();

// determine thumbnail size from WIDTH and HEIGHT

int w = this.getWidth();
int h = this.getHeight();

double thumbRatio = (double)w / (double)h;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth /
(double)imageHeight;
if (thumbRatio < imageRatio) {
h = (int)(w / imageRatio);
} else {
w = (int)(h * imageRatio);
}

image = image.getScaledInstance(w, h,
Image.SCALE_SMOOTH);
repaint();
}
}


public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
System.out.println(propertyName);
// Make sure we are responding to the right event.
if
(propertyName.equals(beanSortTableClipart.SELECTED_CLIPART_CHANGED))
{
String selection = (String)e.getNewValue();
String path;

path = "c:/CLIPART/"+selection;
System.out.println(path);
setPath(path);


}

}

public void paintComponent(Graphics g) {
g.setColor(bg);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0,0,this);

}

private void initComponents() {

setLayout(null);

}

}


/*
* beanFormSortTable.java
*
* Created on February 13, 2006, 10:31 PM
*/

package MainUI;

import db_connection.db_worker;
import java.beans.PropertyChangeListener;
import java.sql.ResultSet;
import sort_table.JSortTable;

/**
*
* @author Adam
*/
public class beanSortTableClipart extends javax.swing.JScrollPane
{
private String SQLCall;
private JSortTable result_table;
private db_worker DB_Worker;
private String Selected_clipart;
private PropertyChangeListener PCL;
public static final String SELECTED_CLIPART_CHANGED =
"SelectedClipartChanged";
/** Creates new form BeanForm */
public beanSortTableClipart() {
initComponents();

}

public void setPCL(PropertyChangeListener preview)
{
this.PCL = preview;
this.addPropertyChangeListener(PCL);
}

public void setSelected_clipart(String clipart_id)
{
firePropertyChange(SELECTED_CLIPART_CHANGED,Selected_clipart,
clipart_id);
this.Selected_clipart = clipart_id;
}

public void setDBConnection(db_worker DB_Worker)
{
this.DB_Worker = DB_Worker;
}

public void setSQLCall(String SQL)
{
this.SQLCall = SQL;
}

public void refreshTable()
{
setSQLCall("SELECT * FROM spottedl_clipart.clipart");

ResultSet rs =
DB_Worker.execute_statement_with_return(SQLCall);
result_table = new JSortTable(JSortTable.makeModel(rs));
result_table.set_alternating_row_colors();

this.setViewportView(result_table);

result_table.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt) {
String folder =
String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
2));
String clipart_id =
String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
0));
String inner_folder =
clipart_id+"-"+String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
1));


setSelected_clipart(folder+"/"+inner_folder+"/"+clipart_id+"-thumb.gif");
}
});

result_table.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
String folder =
String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
2));
String clipart_id =
String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
0));
String inner_folder =
clipart_id+"-"+String.valueOf(result_table.getValueAt(result_table.getSelectedRow(),
1));


setSelected_clipart(folder+"/"+inner_folder+"/"+clipart_id+"-thumb.gif");
}
});

}


private void initComponents() {

}


}

.



Relevant Pages

  • Re: Problem with FTP
    ... > private static Encoding ASCII = Encoding.ASCII; ... > private string message = null; ... > private int port = 21; ... > public void Login() ...
    (microsoft.public.pocketpc.developer)
  • Getting Error in Login() method in FtpConnection Class
    ... private static int BUFFER_SIZE = 512; ... private static Encoding ASCII = Encoding.ASCII; ... private string server = "localhost"; ... public void Login() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Alternative to FTP
    ... private NetworkStream ctrlstm; ... private string ReceiveStatus() ... public void MkDir ... public class XFtpWebRequest: WebRequest ...
    (comp.os.vms)
  • Re: System.Net.FTP and VMS
    ... StreamReader objReader = new StreamReader; ... private NetworkStream ctrlstm; ... private string ReceiveStatus() ... public void MkDir ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: JFrame Resize Issues
    ... private Rectangle positRectangle; ... private JMenuItem FilePlayback; ... static public void main{ ... public void componentHidden(ComponentEvent evt) { ...
    (comp.lang.java.programmer)