Results 1 to 2 of 2
- 05-11-2012, 10:41 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 12
- Rep Power
- 0
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?
- 05-12-2012, 04:35 AM #2
Re: Populating a combobox from a text file.
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Populating databasefields in combobox
By aborgeld in forum New To JavaReplies: 27Last Post: 05-13-2011, 10:04 AM -
Populating a JList from a text file - Netbeans
By Ben1 in forum AWT / SwingReplies: 1Last Post: 01-13-2011, 03:30 PM -
JOptionPane with a comboBox with data read from a text file
By rdmapes in forum AWT / SwingReplies: 0Last Post: 11-21-2010, 05:10 PM -
Populating the values in text fields
By Inaam in forum Web FrameworksReplies: 2Last Post: 07-26-2010, 08:55 AM -
Combobox from plain text file (plain text file database)
By cotarelo in forum AWT / SwingReplies: 3Last Post: 06-08-2010, 08:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks