Results 1 to 2 of 2
  1. #1
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Populating a combobox from a text file.

    I've tried googling for help but I never got it working.
    I'm bad in java and everyother programming language so I don't really have a clue what I should do.
    I just try something and if it doesn't work I'll try something else.

    This time I'm did not succeed with that stragedy :P

    I'm coding a small java swing program which should help me with bookkeeping..
    I want it to populate a combobox from a text file, so every line is one option to choose from.
    The lines will be short, max 15 characters or so.

    So, heres my code at the moment:

    Java Code:
    import java.awt.GridLayout;
    import java.awt.List;
    import java.awt.event.ActionEvent;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Engine extends JPanel {
    
    	
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = -1828230033027140410L;
    
    
    
    
    
    	public Engine() {
            super(new GridLayout(10,3));  //2 rows, 1 column
            JLabel label1, label2, label3, label4, label5, label6, label7, filler1, filler2, filler3, filler4, filler5, filler6, filler7,
            filler8, filler9;
            
            JComboBox answer1, answer2, answer3, answer5, answer6, answer7;
            JTextField answer4;
            JButton edit1, edit2, edit3, edit5, edit6, button1, button2;
            
     
            //Create the labels:
            label1 = new JLabel("Item type:");
            label2 = new JLabel("Item status:");
            label3 = new JLabel("Item name:");
            label4 = new JLabel("Item attributes:");
            label5 = new JLabel("Account name:");
            label6 = new JLabel("Character position:");
            label7 = new JLabel("Choose file to open:");
     
            //Create the "answers":
            answer1 = new JComboBox(itemtype);
            answer2 = new JComboBox(itemstatus);
            answer3 = new JComboBox(itemname);
            answer4 = new JTextField();
            answer5 = new JComboBox();
            answer6 = new JComboBox();
            answer7 = new JComboBox();
            
            //Create the edit buttons:
            edit1 = new JButton("Edit");
            edit2 = new JButton("Edit");
            edit3 = new JButton("Edit");
            edit5 = new JButton("Edit");
            edit6 = new JButton("Edit");
            
            //Create the fillers:
            filler1 = new JLabel("");
            filler2 = new JLabel("");
            filler3 = new JLabel("");
            filler4 = new JLabel("");
            filler5 = new JLabel("");
            filler6 = new JLabel("");
            filler7 = new JLabel("");
            filler8 = new JLabel("");
            filler9 = new JLabel("");
            
            //Create the other buttons:
            button1 = new JButton("Add item");
            button2 = new JButton("Open");
     
            //Add the components.
            add(label1);
            add(answer1);
            add(edit1);
            
            add(label2);
            add(answer2); 
            add(edit2);
            
            add(label3);
            add(answer3);
            add(edit3);
            
            add(label4);
            add(answer4);
            add(filler1);
            
            add(label5);
            add(answer5);
            add(edit5);
            
            add(label6);
            add(answer6);
            add(edit6);
            
            add(filler2);
            add(filler3);
            add(filler4);
            
            add(filler5);
            add(button1);
            add(filler6);
            
            add(filler7);
            add(filler8);
            add(filler9);
            
            add(label7);
            add(answer7);
            add(button2);
            
            //Create the listeners:
            
            button1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    button1ActionPerformed(evt);
                }
            });
    	}
    	
    	
    	
        protected void button1ActionPerformed(ActionEvent evt) {
    
    		
    	}
        
        public String itemnames() {
        	BufferedReader input = new BufferedReader(new FileReader("itemnames.txt"));
        	ArrayList<String> itemnames = new ArrayList<String>();
        	try {
        	  String line = null;
        	  while (( line = input.readLine()) != null){
        	    itemnames.add(line);
        	  }
        	}
    
        	catch (FileNotFoundException e) {
        	    System.err.println("Error, file " + "itemnames.txt" + " didn't exist.");
        	}
        	finally {
        	    input.close();
        	}
    
        }
        
    
    
    
    	/**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void engineGUI() {
            // Create and set up the window.
            JFrame mainframe = new JFrame("Mule Handler PRO - main window");
            mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainframe.add(new Engine());
            mainframe.pack();
            mainframe.setSize(400,200); // make frame 400x200
            mainframe.setResizable(false);
            mainframe.setVisible(true);
        }
         
        
        
        
        
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    engineGUI();
                }
            });
        }
        
        
        String[] itemtype = {"Amulet", "Armor", "Belt", "Boots", "Charm", "Gloves", "Helmet", "Jewel", "Ring", "Rune", "Shield" ,"Weapon"};
        String[] itemstatus = {"Non-ethereal", "Ethereal"};
        String[] itemname = itemnames.toArray(new String[]{});
    
    }

    So as you see I'm trying to read the text file with this:

    Java Code:
        public String itemnames() {
        	BufferedReader input = new BufferedReader(new FileReader("itemnames.txt"));
        	ArrayList<String> itemnames = new ArrayList<String>();
        	try {
        	  String line = null;
        	  while (( line = input.readLine()) != null){
        	    itemnames.add(line);
        	  }
        	}
    
        	catch (FileNotFoundException e) {
        	    System.err.println("Error, file " + "itemnames.txt" + " didn't exist.");
        	}
        	finally {
        	    input.close();
        	}
    
        }

    Any solutions or suggestions?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: Populating a combobox from a text file.

    Quote Originally Posted by Mike. View Post
    Any solutions or suggestions?
    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Your question is about populating a JComboBox, so all those buttons and labels and an empty actionPerformed(...) method only create clutter.

    Do you know how to read a text file one line at a time?
    Do you know how to add a String as an item in a JComboBox?
    Have you gone through the Oracle tutorials on the two subjects?

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Populating databasefields in combobox
    By aborgeld in forum New To Java
    Replies: 27
    Last Post: 05-13-2011, 10:04 AM
  2. Populating a JList from a text file - Netbeans
    By Ben1 in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 03:30 PM
  3. Replies: 0
    Last Post: 11-21-2010, 05:10 PM
  4. Populating the values in text fields
    By Inaam in forum Web Frameworks
    Replies: 2
    Last Post: 07-26-2010, 08:55 AM
  5. Replies: 3
    Last Post: 06-08-2010, 08:10 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
  •