Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JLabel updating StatusBar

Status
Not open for further replies.

kellan4459

IS-IT--Management
Jul 3, 2003
84
US
I have three classes (StatusBar, ImageGUI, ImageLoader)

the status bar class is nothing more than a JLabel that I instantiate in the ImageLoader class. In this class I update the text of the JLabel throughout the class. I then have my ImageGUI which has an instance of ImageLoader. I add the ImageLoader StatusBar to my GUI through a getStatusBar getter method in ImageLoader. It shows up with the instantiated text but the text doesn't change. It has been a while since I have worked with the Java GUI so I'm a little rusty. Does anyone see what I may be missing?

###STATUS BAR CLASS
public class StatusBar extends JLabel {

/** Creates a new instance of StatusBar */
public StatusBar() {
super();

this.setMessage("Ready");
}

public void setMessage(String message) {
this.setText(message);
}
}


###snippet where I add to my Frame in ImageGUI class
iu = new ImageLoader();
myPanel.add(iu.getStatusBar());

###snippet where I create then method where I set the text ###in ImageLoader class
public class ImageUploader {
private StatusBar sbPoint;

public ImageUploader(){
this.sbPoint = new StatusBar();
this.sbPoint.setMessage("Please select a directory to process");
}

public StatusBar getStatusBar(){

return this.sbPoint;
}

public void AddWatermark(String directoryPath,String imagePath, String watermarkPath) throws IOException {
this.getStatusBar().setMessage("creating WaterMark for "+imagePath);
}


}


 
I have changed it to get rid of the extra StatusBar class and I'm just using a JLabel in the Gui class with a getter and setter. Now I have the 2 classes and I see the setStatusBar sets the right value and the getStatusBar returns that value. Though I still do not get the update on my JLabel. I have tried repaint and other things but nothing seems to work. Here is my code for both classes. Any help is greatly appreciated.

###############ImageUploader.java#########################
/*
* Created on Mar 30, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package src;

import gui.ImageLoaderGui;

import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;
import com.enterprisedt.net.ftp.*;

/**
* @author
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ImageUploader {
private ImageLoaderGui ilg;

public ImageUploader(){
ilg = new ImageLoaderGui(this);
}

public static boolean createWatermarkDirectory(String mainPath){
String path = mainPath.concat("watermarked\\");
boolean exists = (new File(path)).exists();
if (exists) {
return exists;
} else {
boolean success = new File(path).mkdir();
return success;
}
}

public static boolean createThumbnailDirectory(String mainPath){
String path = mainPath.concat("thumbnail\\");
boolean exists = (new File(path)).exists();
if (exists) {
return exists;
} else {
boolean success = new File(path).mkdir();
return success;
}
}

public void AddWatermark(String directoryPath,String imagePath, String watermarkPath) throws IOException{
ilg.setStatusBar("creating WaterMark for "+imagePath);

System.out.println("status = " + ilg.getStatusBar());
BufferedImage im = ImageIO.read(new File(imagePath));
BufferedImage im2 = ImageIO.read(new File(watermarkPath));

Graphics2D g = im.createGraphics();

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
g.drawImage(im2, (im.getWidth()-im2.getWidth())/2, (im.getHeight()-im2.getHeight())/2, null);

g.dispose();

//display(im);
createWatermarkDirectory(directoryPath);
String watermarkFile = directoryPath.concat("watermarked\\");
String[] splitPath = imagePath.split("\\\\");
watermarkFile = watermarkFile.concat(splitPath[splitPath.length-1]);
System.out.println("watermark file = "+ watermarkFile);
ImageIO.write(im, "jpg", new File(watermarkFile));
}

public static void display(BufferedImage image) {
JFrame f = new JFrame("WaterMark");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JLabel(new ImageIcon(image)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public String[] ProcessDirectory(String myDir ) {
File dir = new File(myDir);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
System.out.println("dir is = "+dir);
System.out.println("name is = "+name);
boolean retVal = ((name.endsWith(".jpg"))||(name.endsWith(".jpeg"))||(name.endsWith(".JPG"))||(name.endsWith(".JPEG")));
System.out.println("returning "+retVal);
return retVal;
}
};
String[] children = dir.list(filter);
return children;
}

public void CreateThumbnail(String directoryPath, String imagePath) throws IOException{
ilg.setStatusBar("creating Thumbnail for "+imagePath);
createThumbnailDirectory(directoryPath);
String thumbnailFile = directoryPath.concat("thumbnail\\");
String[] splitPath = imagePath.split("\\\\");
thumbnailFile = thumbnailFile.concat(splitPath[splitPath.length-1]);
System.out.println("thumbnail file = "+ thumbnailFile);

BufferedImage biOrig = ImageIO.read(new File(imagePath));
int divider = getThumbnailDivider(biOrig);
int height = biOrig.getHeight()/divider;
int width = biOrig.getWidth()/divider;

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

bi.getGraphics().drawImage(biOrig, 0, 0, width, height, null);
ImageIO.write(bi, "jpg", new File(thumbnailFile));
ImageIcon icon = new ImageIcon(bi);
}


/**
* @param imagePath
* @return
*/
private static int getThumbnailDivider(BufferedImage bi) {

int maxDimension=160;

for(int i=1;i<bi.getHeight();i++){
if(((bi.getHeight()/i) < maxDimension) && ((bi.getWidth()/i) < maxDimension)){
return i;
}
}
return bi.getHeight();
}

public void FTPImages(String path){
FileTransferClient ftp = null;
File pathDir = new File(path);
String[] pathFiles = pathDir.list();
String[] subDirs = path.split("\\\\");
String ftpDirectory = "/public_html/test/test/images/images_03_2008/"+subDirs[subDirs.length-1];

try {
// create client

ftp = new FileTransferClient();

// set remote host
ftp.setRemoteHost(" ftp.setUserName("user");
ftp.setPassword("password");

// connect to the server

ftp.connect();

//ftp.uploadFile(filename, filename);

try {
ftp.createDirectory(ftpDirectory);
} catch (FTPException e){

}
ftp.changeDirectory(ftpDirectory);

System.out.println("directory is = "+ftp.getRemoteDirectory());

for(int i=0;i<pathFiles.length;i++){
if(pathFiles.endsWith(".jpg") || pathFiles.endsWith(".jpeg") || pathFiles.endsWith(".JPG") || pathFiles.endsWith(".JPEG")){
ilg.setStatusBar("FTPing file "+path+pathFiles);
ftp.uploadFile(path+pathFiles,pathFiles);
}

}

// Shut down client

ftp.disconnect();

} catch (Exception e) {
e.printStackTrace();
}
}


public static void main(String[] args) throws IOException {
ImageUploader iu = new ImageUploader();

}
}

##################ImageLoaderGui.java#################
/*
* Created on Apr 4, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import src.ImageUploader;


/**
* @author
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ImageLoaderGui {
private JFileChooser jFCDirectory;
private JLabel jDirectoryPath;
private JButton jSelectDirectory;
private JButton jSubmitDirectory;
private ImageUploader iu;
private JLabel jStatusBar;
private JFrame myFrame;
private JPanel myPanel;


public ImageLoaderGui(ImageUploader iu){

myFrame = new JFrame();

myPanel = new JPanel();
myFrame.getContentPane().add(myPanel);
myFrame.setTitle("ImageLoader");
myPanel.setLayout(null);
myPanel.setBounds(25, 25, 450, 450);
this.iu = iu;
myFrame.setBounds(200,200,500,500);
jDirectoryPath = new JLabel();
myPanel.add(jDirectoryPath);
jDirectoryPath.setText("The path you choose will show here");
jDirectoryPath.setFont(new Font("Arial", Font.PLAIN, 10));
jDirectoryPath.setBounds(25, 50, 450, 25);
jDirectoryPath.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

jStatusBar = new JLabel();
jStatusBar.setText("Select directory to process");
jStatusBar.setFont(new Font("Arial", Font.PLAIN, 10));
jStatusBar.setForeground(Color.ORANGE);
jStatusBar.setBounds(25, 200, 450, 25);
jStatusBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
myPanel.add(jStatusBar);

jSelectDirectory = new JButton();
myPanel.add(jSelectDirectory);
jSelectDirectory.setText("Select Directory");
jSelectDirectory.setBounds(325, 85, 150, 25);
jSelectDirectory.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
jSelectDirectoryMousePressed(evt);
}
});

jSubmitDirectory = new JButton();
myPanel.add(jSubmitDirectory);
jSubmitDirectory.setText("Submit");
jSubmitDirectory.setBounds(325, 135, 150, 25);
jSubmitDirectory.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt2) {
jSubmitDirectoryMousePressed(evt2);
}
});

myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void setStatusBar(String status){
jStatusBar.setText(status);
}

public String getStatusBar(){
return jStatusBar.getText();
}

private void jSelectDirectoryMousePressed(MouseEvent evt) {
System.out.println("jSelectDirectory.mousePressed, event="+evt);
JFileChooser chooser = new JFileChooser("\\\\Office\\Public\\File Server\\Landon\\Web Design\\clsphotography\\images\\");
chooser.setDialogTitle("Select Path to Process");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
jDirectoryPath.setText(chooser.getSelectedFile().getAbsolutePath()+"\\");
}
}

private void jSubmitDirectoryMousePressed(MouseEvent evt){
System.out.println("jSubmitDirectory.mousePressed, event="+evt);
String directoryPath = getDirectoryPath();
System.out.println("path submitted is "+directoryPath);
String[] retFiles = iu.ProcessDirectory(directoryPath);
System.out.println("retfiles length = "+retFiles.length);
for (int i=0;i<retFiles.length;i++){
System.out.println("i = "+i);
String filePath = directoryPath.concat(retFiles.toString());
System.out.println("file ="+ filePath);
try {
iu.CreateThumbnail(directoryPath,filePath);
iu.AddWatermark(directoryPath,filePath,"\\\\Office\\Public\\File Server\\Landon\\Web Design\\clsphotography\\images\\watermark.jpg");

} catch (IOException e) {
e.printStackTrace();
}
}
iu.FTPImages(directoryPath+"thumbnail\\");
iu.FTPImages(directoryPath+"watermarked\\");

}

private String getDirectoryPath(){
return this.jDirectoryPath.getText();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top