|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-03-2008, 01:04 AM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
JList and records, what to do to solve the problem
I´d like to list a series of image file names, and to be able to select them, one by one. The class JList seems suitable for that idea. Pasting a list (in the form of a array) turns out to be no problem, for example:
String[] test = { "pic1", "pic2", "picetc"};
Piclist = new JList(test);
Piclist.setSelectionMode(ListSelectionModel.SINGLE _INTERVAL_SELECTION);
Piclist.setLayoutOrientation(JList.VERTICAL);
Piclist.setVisibleRowCount(-1);
JScrollPane PiclistScroller = new JScrollPane(Piclist);
PiclistScroller.setPreferredSize(new Dimension(200, 500));
textPanel.add(PiclistScroller);
And the image names show up just fine.
But what happens when I do the following:
private Img[] img;
private static class Img { // the image file info record array
String name, textinfo, infotextcolor, infotextsize;
int duration, numberpic, fade, infotextx, infotexty;
}
img = new Img[numberpic]; // according the number of pic
for ( i = 0; i < numberpic; i++)
img[i] = new Img();
When I try to pass the "name" field into the object:
Piclist = new JList(img.name);
I get “can not find symbol variable name”
WHY?
"Img[what ever number in the range].name" holds a picture file name.
So Img.name should point to that array in general, or not?
What should I do to solve this problem
Saludos willemjav
|
|

06-03-2008, 04:24 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
name is not an Array or Objects nor Array of Strings....
You may review the documentation about JList if you have time.
And, to be able to dynamically changing the contents of that JList,
You may stop using arrays, instead use DefaultListModel...
that model will do the rest....
As the content of that model changes, the content of the JList also changes....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by sukatoa : 06-03-2008 at 04:27 AM.
|
|

06-03-2008, 04:59 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
Images from Using Swing Components Examples.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class StoreTest implements ListSelectionListener {
ImageStore[] stores;
JList list;
JLabel imageLabel;
JLabel nameLabel;
ImageIcon icon;
public StoreTest(BufferedImage[] images) {
String[] names = { "chin", "glasses", "hat", "teeth" };
stores = new ImageStore[images.length];
for(int i = 0; i < stores.length; i++) {
stores[i] = new ImageStore(images[i], names[i]);
}
}
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) {
int index = list.getSelectedIndex();
BufferedImage image = stores[index].image;
icon.setImage(image);
imageLabel.repaint();
nameLabel.setText(stores[index].name);
nameLabel.repaint();
}
}
private JPanel getContent() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
panel.add(getList(), gbc);
panel.add(getLabelPanel(), gbc);
return panel;
}
private JList getList() {
DefaultListModel model = new DefaultListModel();
for(int i = 0; i < stores.length; i++) {
model.addElement(stores[i]); // toString method called
}
list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
return list;
}
private JPanel getLabelPanel() {
icon = new ImageIcon(stores[0].image);
imageLabel = new JLabel(icon, JLabel.CENTER);
nameLabel = new JLabel(stores[0].name, JLabel.CENTER);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(imageLabel, gbc);
panel.add(nameLabel, gbc);
return panel;
}
public static void main(String[] args) throws IOException {
String[] ids = { "-c---", "--g--", "---h-", "----t" };
String prefix = "images/geek/geek";
String ext = ".gif";
BufferedImage[] images = new BufferedImage[ids.length];
for(int i = 0; i < images.length; i++) {
String path = prefix + ids[i] + ext;
images[i] = ImageIO.read(new File(path));
}
StoreTest test = new StoreTest(images);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class ImageStore {
BufferedImage image;
String name;
public ImageStore(BufferedImage image, String name) {
this.image = image;
this.name = name;
}
public String toString() { return name; }
}
|
|

06-03-2008, 09:04 PM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
important tiny class
Thanks Sukatoa, but I did not understand very much what you wrote about (due to my limited understanding of java), but thanks any way for your fast response.
Hardwired, slamming out the code (probably in no-time), thank you too for your quick response. At first sight I did not understand much, but I’ll study it carefully (because your code is worth studying).
But let me explain some more what I am planning to do. I moved on from the Image displaying applet (with some minor problems still).
Now I am planning to write a parent application for that applet. This application will prepare the picture for display (size, crop, format etc.) and it will additionally set some important parameters, like an info text (several lines, if needed), position, size, color of that text and the individual duration of each picture.
The JList class will be of importance because it will show the list of images to select and edit, and most importantly it will allow me to change the order by dragging a list-member to an other position of the list (for the moment I have no idea how to that, but that will be one of the questions to ask in future). All this data will be stored in a text file and the applet will use that data for display (it already does, but for the moment the text file is edited manually)
The heart of all this forms a small tiny class called Img:
private static class Img { // the image file info record array
String name, textinfo, infotextcolor, infotextsize;
int duration, numberpic, fade, infotextx, infotexty;
}
The array object of that class contains all the data (distributed over its several fields).
So Sukatoa, Img[3].name is actual an array pointing to a specific picture file name.
Hardwired I do need this tiny class and I have to get the img.name array completely into the JList for display and I do not know how to do that. The tiny class works fine (containing all data for display) at the image displaying applet and I would like to use the same idea for the edit application!
The photo edit application consists of several classes. Getting data form on object to another is an other problem. The project is ambitious and I´ll work on it some time…. Anyway how does one eat an elephant? byte by byte (hahaha?)
willemjav
|
|

06-06-2008, 07:28 PM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
JList and a bad programming example
The image file names are stored in the ARRAY img[i].names (or what should I call it else, Sukatoa?). Since I cannot get img.names into JList I did the following:
String[] help = new String[numberpic];
for (int i = 0; i < numberpic; i++)
help[i] = img[i].name;
Piclist = new JList(help);
….. etc
And, funny enough, all picture names now appear in the list.
Of course Hardwired this is a good example of bad java programming!
But it works (I wonder what you have to say to it)!
And I still do not know why java does not allow me to do the most simple and obvious thing, which is:
Piclist = new JList(img.name);
(Sukatoa, I am looking into this model thing of PList and I might need some time to understand its concept).
Saludos willemjav
By the way,
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
is the default code to close a gui application.
What would be the way to terminate an application according the following condition:
if (path == null)
….??? Code that exits application
else
…..
|
|

06-07-2008, 04:46 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
|
You may use System.exit(int flag)....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-08-2008, 08:44 PM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
dragging along in JList
Thanks the exit code works (I suppose it is System.exit(-1)  ,
And as I said, I do have the image names listed in JList . The next question is, could I drag (select and drag) a selection or even multi-selection of the listed fields into an other position of the same list? I am aware of the interface named MouseMotionListener etc. Is this option present in JList or do I have to find an alternative (I am still looking over Hardwired´s code and hope to find some clues to repair the awkward help-array solution)
Saludos
willemjav
|
|

06-09-2008, 06:08 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
And as I said, I do have the image names listed in JList . The next question is, could I drag (select and drag) a selection or even multi-selection of the listed fields into an other position of the same list?
May i know what's your purpose of implementing drag/select or
multi-selection on the list?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-09-2008, 09:22 AM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
dragging along in JList2
Since I am a musician (and my colleagues see my program activity with interest and suspicion) I will be more than delighted to tell you ALL (ignoring probably your statement of being efficient).
The application will consist of several classes of different files (the image applet, I worked on before, has just one single class with inner classes).
The object of the first class (Imageloader.java) is created in the next class (Setimages.java):
ImageLoader load = new ImageLoader();
-It opens a file dialogue asking to select a file folder (where the applet etc. will be, probably the file folder of your web site).
-Next it will look for the file folder Imagestore. If the folder is present the object stops, if not it will create a folder with that name and put a new textfile into that folder called Slidessource.txt (with one single text line). The class path of the folder will be available by calling:
filepath = load.Imagestorepath;
and in case the user selects cancel of the file dialogue:
if (filepath == null) { // if user checks cancel, exit prog.
System.exit(-1);
}
else
……….
The filepath will help to open the text file in the next class (Setimages). The first line of the text file Slidessource.txt sets the variable picnumber and if picnumber=0, there are no images present yet etc.
If picnumber>0, it will load all the images etc.
Now, finally turning to your question, the list of images in JList makes it possible to select each image for editing. But it should also be possible to change to order of appearance of each image at the applet (depending of the order of the image names of the Slidesource.txt fikle) by simply changing the order of the list!
Here is the code of the first class any comment is welcome:
package setimages;
import java.io.FileWriter;
import javax.swing.*;
import java.io.*;
public class ImageLoader extends JPanel{
public static final String IMAGEFOLDER="Imagestore",
IMAGETEXTFILE="Slidessource.txt";
public File Imagestorepath;
public boolean imagestorepresent;
private JFileChooser fileDialog;
public ImageLoader() {
Imagestorepath = Openfiledialogue();
imagestorepresent = LookforImagestore(Imagestorepath, IMAGEFOLDER);
System.out.println("Imagestore present? " + imagestorepresent + " path " + Imagestorepath);
}
public File Openfiledialogue() {
if (fileDialog == null)
fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Set the work directory");
fileDialog.setSelectedFile(null); // No file is initially selected.
fileDialog.setFileSelectionMode(fileDialog.DIRECTO RIES_ONLY); // select only directories
int option = fileDialog.showOpenDialog(this);
// (Using "this" as a parameter to showOpenDialog() assumes that the
// readFile() method is an instance method in a GUI component class.)
if (option == JFileChooser.APPROVE_OPTION) // if dir is selected return File
return fileDialog.getSelectedFile(); // if not return null, end program
else
return null;
}
public boolean LookforImagestore (File Imagestorepth,
String imagefoldername) {
File dir = new File(Imagestorepth + "/" + imagefoldername);
if (dir.isDirectory() == false) {
if (dir.exists() == false)
System.out.println("There is no such directory! " + Imagestorepth + "/" + imagefoldername);
else
System.out.println("That file is not a directory. " + imagefoldername);
try {
CreateImagefolder(Imagestorepth); //creates folder and text file
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Cannot create the folder " + Imagestorepth + "/" + imagefoldername);
}
return false;
}
else
return true; //open the existing Imagefolder in xx class
}
public void CreateImagefolder (File rootbasepth) throws IOException {
File file = new File(rootbasepth +"/Imagestore"); // creates the Imagestore folder
if(file.exists()==false){
file.mkdirs();
}
File dir = new File(rootbasepth +"/Imagestore/Slidessource.txt"); // creates the text file
if(dir.exists()==false){
dir.createNewFile();
}
Writeinfoline(rootbasepth +"/Imagestore/Slidessource.txt"); // writes the single info line
} // no pic present numberpic=0
public void Writeinfoline(String pth) {
PrintWriter outputStream;
try {
outputStream = new PrintWriter (new FileWriter(pth));
outputStream.println("dragstraimageslideappletelma snou2008 0 400 400 0");
outputStream.close();
System.out.println("written and closed " + pth);
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Cannot write to " + pth);
}
}
}
|
|

06-09-2008, 04:52 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
|
|
Now, finally turning to your question, the list of images in JList makes it possible to select each image for editing. But it should also be possible to change to order of appearance of each image at the applet (depending of the order of the image names of the Slidesource.txt fikle) by simply changing the order of the list!
You may have a copy of those updated image names, reset the ListModel, doing sort operation on the list of updated image names, then add them again to the ListModel....
Here is the code of the first class any comment is welcome:
My comment: You forgot to use a codetag... 
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-11-2008, 03:28 PM
|
|
Senior Member
|
|
Join Date: Dec 2007
Location: Spain
Posts: 235
|
|
|
what is a codetag
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|