I have received a lot of advice, but no concrete help.

10-11  Source: Network gathering  Views:0 

Advertisement
For the past two weeks I have received a lot of advice, but no one told me what to do in a concrete way or demonstrates on my example. Is there any one who can do this and help me ??????
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.
Here is my application. I have indicated with ** in my code how I am trying to do it.
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;
String str;
UnitA (BasicUnitA parent, boolean p1, boolean p2) {
Myapplet=parent;
//Read image names from a properties file
ResourceBundle imageResource1;
BufferedReader reader; //**
try{
reader=new BufferedReader(new FileReader("c:/java/MyText.txt")); //**
str = reader.readLine(); //**
Vector vr = new Vector(); //**
vr.addElement(str); //**
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);
} catch(IOException evt) {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();
TA1.setText(TA1.getText()+vr); //**
TA1.revalidate(); //**
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;
Hi
Look at this program and i am sure it will give you some ideas, (actualy all you have to do is to add the images names in addPicList(), and to add the text in setText(s).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;
public class PicText extends JFrame
     NwList picList;
     NwPict picImag;
     NwText txtImag;
public PicText()
     super ("My Pics");
     getContentPane().setLayout(null);
     setBounds(10,10,710,500);
     addPicList();
     picImag = new NwPict();
     getContentPane().add(picImag);
     txtImag = new NwText("");
     getContentPane().add(txtImag);
     addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
          {     dispose();
               System.exit(0);}});
     setVisible(true);
private void addPicList()
     Vector v = new Vector(10,10);
     v.add("usa.gif");
     v.add("p.gif");
     v.add("img 3");
     picList  = new NwList(v);
     picList.setBounds(10,10,200,310);
     getContentPane().add(picList);
public class NwList extends JPanel
     JList       jl;
     JScrollPane sp;
     JLabel      jt;
public NwList(Vector v)
     setLayout(null);
     jl = new JList(v);
     sp = new JScrollPane(jl);
     jt = new JLabel();
     jt.setBackground(Color.white);
     jt.setForeground(Color.black);
     jt.setOpaque(true);
     jt.setBorder(new JTextField().getBorder());
     jt.setFont(getFont());
     jl.setBounds(0,0,100,1000);
     jl.setBackground(Color.white);
     jl.addListSelectionListener(new ListSelectionListener()
     {     public void valueChanged(ListSelectionEvent e)
                jt.setText((String)jl.getSelectedValue());
                String si = (String)jl.getSelectedValue();
               picImag.load(si);
               txtImag.setText(si);
     add(sp);
     add(jt);
public void setBounds(int x, int y, int w ,int h)
     super.setBounds(x,y,w,h);
     sp.setBounds(0,y+12,w,h-23);
     sp.revalidate();
     jt.setBounds(0,0,w,20);
public class NwPict extends JComponent
     Image pic = null;
public NwPict()
//     setLayout(null);
     setBounds(220,10,475,310);
     setBorder(new CompoundBorder(new MatteBorder(0,0,1,1,Color.gray),
                (new MatteBorder(1,1,0,0,Color.white))));
public void load(String s)
     pic = getToolkit().createImage(s);
     MediaTracker tracker = new MediaTracker(this);
     tracker.addImage(pic,0);
     try   {tracker.waitForID(0);}
     catch (InterruptedException e){}
     repaint();
public void paint(Graphics g)
     super.paint(g);
     if (pic != null) g.drawImage(pic,0,0,null);
public class NwText extends JTextArea
public NwText(String s)
     super(s);
     setLineWrap(true);
     setBounds(10,330,685,140);
     setBorder(new CompoundBorder(new MatteBorder(0,0,1,1,Color.darkGray),
                (new MatteBorder(1,1,0,0,Color.gray))));
public void setText(String s)
     if (s == "") return;
     BufferedReader in;
     char   cr = 0x0D; 
     char   lf = 0x0A;
     String so;
     super.setText("");
     try
          in = new BufferedReader(new FileReader("p.p"));
          while ((so = in.readLine()) != null) append(so+cr+lf);
     catch(FileNotFoundException e)
             System.out.println("File Not Found");
     catch(IOException e) 
          System.out.println(" io error");
public static void main (String[] args) 
     new PicText();
} Noah
Related articles