John - Guru, please HELP!
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   DM_DM
Posted On:   Tuesday, September 4, 2001 05:20 AM

John! Let me tell You the whole story. I'm trying to write an application which has to show my image files (set of photos) with a text description. The list of image files placed into the JList. So, when I select a file from the JList it shows me an image (everything is OK). But, every image has it's text description in MyText.text file. It's a file with each text line for every image. Actually, I am trying to connect two actions - showing my image and setting it's specified text line description (from MyText.txt) in a JTextArea. I thought that the best way to do this with my text it is like I did it with my images through Vector. But No Success. Can You help me on that? If    More>>

John!

Let me tell You the whole story.

I'm trying to write an application which has to show my image files
(set of photos) with a text description. The list of image files placed into the JList. So, when I select a file from the JList it shows me an image (everything is OK).

But, every image has it's text description in MyText.text file. It's
a file with each text line for every image. Actually, I am trying to connect two actions - showing my image and setting it's specified text line description (from MyText.txt) in a JTextArea. I thought that the best way to do this with my text it is like I did it with my images through Vector. But No Success.

Can You help me on that? If You will show me how to do it on my example I will really appreciate it.

Thank You DM.

Here is my application:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import javax.swing.*;

import javax.swing.event.*;

public class UnitA extends JPanel implements ListSelectionListener {

BasicUnitA Myapplet;

JPanel listPanel, picturePanel;

Vector imageNames1;

JList list1;

JLabel picture1;

JScrollPane listScrollPane1, pictureScrollPane1, TSP1;

JTextArea TA1;

int index;

UnitA (BasicUnitA parent, boolean p1, boolean p2) {

Myapplet=parent;

//Read image names from a properties file

ResourceBundle imageResource1;

try{

imageResource1 = ResourceBundle.getBundle("imagenames1");

String imageNamesString1 = imageResource1.getString("images");

imageNames1 = parseList(imageNamesString1);

} catch (MissingResourceException e) {

System.err.println("Please, add properties file with image names.");

System.exit(1);

}

//Create one list of images and put it in a scroll pane and panel

list1 = new JList(imageNames1);

list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list1.setSelectedIndex(0);

list1.addListSelectionListener(this);

listScrollPane1 = new JScrollPane(list1);

listScrollPane1.setPreferredSize(new Dimension (120,120));

//Set up the picture label and put it in a scroll pane

ImageIcon firstImage1 = new ImageIcon("java/Applications/images/fotos/"
+

(String)imageNames1.firstElement());

picture1 = new JLabel(firstImage1);

picture1.setPreferredSize(new Dimension (firstImage1.getIconWidth(),

firstImage1.getIconHeight()));

pictureScrollPane1 = new JScrollPane(picture1);

pictureScrollPane1.setPreferredSize(new Dimension (500,360));

pictureScrollPane1.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createLoweredBevelBorder(),

BorderFactory.createEmptyBorder(5,5,5,5)),

BorderFactory.createLineBorder(Color.black)));

pictureScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLL

BAR_ALWAYS);

TA1 = new JTextArea();

TA1.setLineWrap(true);

TA1.setWrapStyleWord(true);

TA1.setEditable(false);

TA1.setFont(new Font("Serif", Font.PLAIN, 16));

TSP1 = new JScrollPane(TA1);

TSP1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_

ALWAYS);

TSP1.setPreferredSize(new Dimension(500,40));

TSP1.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createCompoundBorder(

BorderFactory.createLoweredBevelBorder(),

BorderFactory.createEmptyBorder(5,5,5,5)),

BorderFactory.createLineBorder(Color.black)));

}

 

public void valueChanged(ListSelectionEvent e) {

if (e.getValueIsAdjusting()) return;

JList theList1 = (JList)e.getSource();

if (theList1.isSelectionEmpty()) {

picture1.setIcon(null);

TA1.setText(null);

} else {

index = theList1.getSelectedIndex();

ImageIcon newImage1 = new

ImageIcon("java/Applications/images/fotos/" +

(String)imageNames1.elementAt(index));

picture1.setIcon(newImage1);

picture1.setPreferredSize(new Dimension (newImage1.getIconWidth(),

newImage1.getIconHeight()));

picture1.revalidate();

try{

BufferedReader reader = new
BufferedReader(new FileReader("c:/java/Applications/MyText.txt")));

String str;

while((str = reader.readLine()) != null) {

TA1.setText(TA1.getText()+str);

}

reader.close();

}catch(IOException evt){};

}

}

 

protected static Vector parseList(String theStringList) {

Vector v = new Vector(10);

StringTokenizer tokenizer = new StringTokenizer (theStringList, " ");

while (tokenizer.hasMoreTokens()) {

String image = tokenizer.nextToken();

v.addElement(image);

}

return v;

}

}

   <<Less

Re: John - Guru, please HELP!

Posted By:   Stevens_Gestin  
Posted On:   Wednesday, September 5, 2001 04:31 AM

Hi,

for me it seems like you made a object oriented mistake. If you want to associate an image and a text it's seems for me you need a class like JohnPicture with two private members : the picture and the text.



In your JList, you will reference JohnPicture instances not pictures and you will then be able to display simultaneously correct picture and text.



Your problem is then to construct correctly your Vector with your JohnPicture instances. For this, delegate the construction of this vector to another class like JohnPictureLoader. This class will create your vector. You report your problem into this class, but this is a better construction.



I hope this can help you.



Stevens
About | Sitemap | Contact