Results 1 to 15 of 15

Thread: Java question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    the G-Diffuser Senior Member pevergreen's Avatar
    Join Date
    Nov 2006
    Location
    Brisbane, Australia
    Posts
    11,585
    Blog Entries
    2

    Default Java question

    Hi everyone.

    Currently trying to do a couple things with java and not having any luck.

    I'm trying to grab the values of a couple text fields and write them to a text file (as one entry), then pull them to a j list, and be able to edit and remove single entries (comprising of multiple text fields)


    Yeah, i'm guessing its a lot more work than I realise, and may be too much to help me with, if so, just tell me

    pever.
    Quote Originally Posted by TosaInu
    The org will be org until everyone calls it a day.

    Quote Originally Posted by KukriKhan View Post
    but I joke. Some of my best friends are Vietnamese villages.
    Quote Originally Posted by Lemur
    Anyone who wishes to refer to me as peverlemur is free to do so.

  2. #2

    Default Re: Java question

    If you do not need anything fancy then you can slurp the file during startup into the JList directly (notice that this approach blocks on I/O so it is not suitable for a program where the GUI must remain responsive at all times). The following example code will be something to build on: http://download.oracle.com/javase/tu.../ListDemo.java

    Then add something like this:
    Code:
    import java.io.*;
    
    /* more code */
    
    private final DefaultListModel model;
     
    /* slurp textFile into the list mode */
    private void initList(String textFile, String encoding) {
       try {
          this.model=initList(new DefaultListModel(), textFile, encoding);
       }
       catch(Exception e) { e.printStackTrace(System.err); }
    }
    /* slurp text file into a list model: note that to use the default encoding of the host OS you should pass a null.*/
    private DefaultListModel initList(DefaultListModel m, String textFile, String encoding) throws Exception {
       BufferedReader br;
       FileInputStream fis;
       try {
           // open file stream
           fis = new FileInputStream(textFile);
           // default to the standard encoding on the OS if not specified:
           br = new BufferedReader(encoding == null? new InputStreamReader(fis) : new InputStreamReader(fis, encoding));
           for(String s = br.readLine(), s!=null; s = br.readLine()) {
              m.addElement(s);
           }
           return m;
       }
       finally {
           if(fis!=null) { fis.close(); }
           if(br!=null) { br.close(); }
       }
    }
    If you need to simply convert multiple text fields -> JList and do edit/remove functions, why not simply concatenate strings and add them to the DefaultListModel, avoiding I/O overhead of unecessary writing to files... ?
    - Tellos Athenaios
    CUF tool - XIDX - PACK tool - SD tool - EVT tool - EB Install Guide - How to track down loading CTD's - EB 1.1 Maps thread


    ὁ δ᾽ ἠλίθιος ὣσπερ πρόβατον βῆ βῆ λέγων βαδίζει” – Kratinos in Dionysalexandros.

  3. #3
    the G-Diffuser Senior Member pevergreen's Avatar
    Join Date
    Nov 2006
    Location
    Brisbane, Australia
    Posts
    11,585
    Blog Entries
    2

    Default Re: Java question

    Quote Originally Posted by Tellos Athenaios View Post
    If you need to simply convert multiple text fields -> JList and do edit/remove functions, why not simply concatenate strings and add them to the DefaultListModel, avoiding I/O overhead of unecessary writing to files... ?
    Hey Tellos, thanks for the super quick reply.

    I'm very new to java, so that was still nothing to me. I'll work off what you've given me though, that should be enough.

    Thank you very much.

    Quote Originally Posted by TosaInu
    The org will be org until everyone calls it a day.

    Quote Originally Posted by KukriKhan View Post
    but I joke. Some of my best friends are Vietnamese villages.
    Quote Originally Posted by Lemur
    Anyone who wishes to refer to me as peverlemur is free to do so.

  4. #4

    Default Re: Java question

    Well concatenating text from textfields is something like this:
    Code:
    private String concat(JTextComponent ... components) {
      StringBuilder sb = new StringBuilder();
      for(JTextComponent c: components) {
        sb.append(c.getText());
      }
      return sb.toString();
    }
    Last edited by Tellos Athenaios; 09-21-2010 at 15:14.
    - Tellos Athenaios
    CUF tool - XIDX - PACK tool - SD tool - EVT tool - EB Install Guide - How to track down loading CTD's - EB 1.1 Maps thread


    ὁ δ᾽ ἠλίθιος ὣσπερ πρόβατον βῆ βῆ λέγων βαδίζει” – Kratinos in Dionysalexandros.

  5. #5
    the G-Diffuser Senior Member pevergreen's Avatar
    Join Date
    Nov 2006
    Location
    Brisbane, Australia
    Posts
    11,585
    Blog Entries
    2

    Default Re: Java question

    Sorry to be a pain, I've gone off on a tangent to the original idea, currently trying to get it removing elements from the list

    Code:
        private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {
             int index = listemail.getSelectedIndex();
             listemail.remove(index);
    
            int size = listModel.getSize();
    
            if (size == 0) { //No email records left, disable the button
                btnremove.setEnabled(false);
    
            } else { //Select an index.
                if (index == listModel.getSize()) {
                    //removed item in last position
                    index--;
                }
    
            listemail.setSelectedIndex(index);
            listemail.ensureIndexIsVisible(index);
        }
    
        }
    Its throwing an error on listmodel, and again on adding items to the list

    Code:
         private void btnremoveActionPerformed(java.awt.event.ActionEvent evt) {
            int index = listemail.getSelectedIndex();
             listemail.remove(index);
    
            int size = listModel.getSize();
    
            if (size == 0) { //No email records left, disable the button
                btnremove.setEnabled(false);
    
            } else { //Select an index.
                if (index == listModel.getSize()) {
                    //removed item in last position
                    index--;
                }
    
            listemail.setSelectedIndex(index);
            listemail.ensureIndexIsVisible(index);
        }
    
        }
    I grabbed the base code from http://download.oracle.com/javase/tu...t.html#mutable and modified it to fit the form I've got, but I can't figure out what I should be changing listModel to.
    Man, why does java have to be so complicated.

    Edit: Having moved on, I now can't run my darn program to test the other code I've written (thanks to your first post for the platform though )
    It may work, but it can't find the main class, I think I scouted the problem, but it wants me to declare my public class in its own .java file (using netbeans 6.9)
    I only added in that public class, because without it it my "class" does not have a main method.

    I'm digging myself deeper into a hole.
    Last edited by pevergreen; 09-22-2010 at 04:57.
    Quote Originally Posted by TosaInu
    The org will be org until everyone calls it a day.

    Quote Originally Posted by KukriKhan View Post
    but I joke. Some of my best friends are Vietnamese villages.
    Quote Originally Posted by Lemur
    Anyone who wishes to refer to me as peverlemur is free to do so.

  6. #6

    Default Re: Java question

    You've copied the same code twice; but in the code sample that is supposed to remove things from the list you will notice (on careful reading) that at first you are using a variable called “listemail” (which I guess is a ListModel or DefaultListModel) and then you suddenly switch to “listModel” (different name) for what should logically remain the same list model (unless I'm missing something) and finally you switch back again to “listemail”. I expect that the code can be fixed by simply changing “listModel” to “listemail”.

    Also for ease of reading I would advise you to clean up (i.e. make consistent) the source indentation.
    - Tellos Athenaios
    CUF tool - XIDX - PACK tool - SD tool - EVT tool - EB Install Guide - How to track down loading CTD's - EB 1.1 Maps thread


    ὁ δ᾽ ἠλίθιος ὣσπερ πρόβατον βῆ βῆ λέγων βαδίζει” – Kratinos in Dionysalexandros.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Single Sign On provided by vBSSO