Results 1 to 11 of 11
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default 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

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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....
    Last edited by sukatoa; 06-03-2008 at 04:27 AM.
    freedom exists in the world of ideas

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Images from Using Swing Components Examples.
    Java Code:
    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]); // [i]toString[/i] 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; }
    }

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default 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

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default 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
    …..

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You may use System.exit(int flag)....
    freedom exists in the world of ideas

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default 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

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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?
    freedom exists in the world of ideas

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default 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);
    }


    }

    }

  10. #10
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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... :)
    freedom exists in the world of ideas

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

Similar Threads

  1. Please solve the problem related to PDf reader
    By kavithaprabhaker in forum New To Java
    Replies: 5
    Last Post: 11-23-2011, 11:08 AM
  2. Problem with JComboBox and Jlist
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 05-07-2008, 09:58 PM
  3. JList problem
    By zizou147 in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 09:50 AM
  4. Number of updated records
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:32 PM
  5. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •