Reading a txt file into a swing component

Thread Solved

Join Date: Jan 2008
Posts: 20
Reputation: dbwalters is an unknown quantity at this point 
Solved Threads: 0
dbwalters dbwalters is offline Offline
Newbie Poster

Reading a txt file into a swing component

 
0
  #1
Nov 4th, 2008
Hello, I was hoping that someone might be able to help me with this program:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.text.*;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.FileNotFoundException;
  9.  
  10. public class CustomerInfo extends JFrame implements ActionListener
  11. {
  12. //construct components
  13. JTextPane textPane = new JTextPane();
  14.  
  15. //initialize data in arrays
  16. public void readFile()
  17. {
  18. BufferedReader br = null;
  19. String[] custID = new String[100];
  20. String[] firstName = new String[100];
  21. String[] lastName = new String[100];
  22. String[] addressOne = new String[100];
  23. String[] addressTwo = new String[100];
  24. String[] myCity = new String[100];
  25. String[] myState = new String[100];
  26. String[] myCountry = new String[100];
  27. String[] productionID = new String[100];
  28. int num = 0;
  29.  
  30. try
  31. {
  32. br = new BufferedReader(new FileReader("Database.txt"));
  33. String line = null;
  34.  
  35. while ((line = br.readLine()) != null)
  36. {
  37. String[] values = line.split(",");
  38. if (values.length > 1)
  39. {
  40. custID[num] = values[0];
  41. firstName[num] = values[1];
  42. lastName[num] = values[2];
  43. addressOne[num] = values[3];
  44. addressTwo[num] = values[4];
  45. myCity[num] = values[5];
  46. myState[num] = values[6];
  47. myCountry[num] = values[7];
  48. productionID[num] = values[8];
  49. num++;
  50. }
  51. }
  52. }
  53. catch (FileNotFoundException ex)
  54. {
  55. ex.printStackTrace();
  56. }
  57. catch (IOException ex)
  58. {
  59. ex.printStackTrace();
  60. }
  61. finally
  62. {
  63. try
  64. {
  65. if (br != null)
  66. br.close();
  67. }
  68. catch (IOException ex)
  69. {
  70. ex.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. //construct instance of CustomerInfo
  76. public CustomerInfo()
  77. {
  78. super("Customer Information Database");
  79. }
  80.  
  81. //create the menu system
  82. public JMenuBar createMenuBar()
  83. {
  84. //create an instance of the menu
  85. JMenuBar mnuBar = new JMenuBar();
  86. setJMenuBar(mnuBar);
  87.  
  88. //construct and populate the File menu
  89. JMenu mnuFile = new JMenu("File", true);
  90. mnuFile.setMnemonic(KeyEvent.VK_F);
  91. mnuFile.setDisplayedMnemonicIndex(0);
  92. mnuBar.add(mnuFile);
  93.  
  94. JMenuItem mnuFileExit = new JMenuItem("Exit");
  95. mnuFileExit.setMnemonic(KeyEvent.VK_X);
  96. mnuFileExit.setDisplayedMnemonicIndex(1);
  97. mnuFile.add(mnuFileExit);
  98. mnuFileExit.setActionCommand("Exit");
  99. mnuFileExit.addActionListener(this);
  100.  
  101. //construct and populate the Edit menu
  102. JMenu mnuEdit = new JMenu("Edit", true);
  103. mnuEdit.setMnemonic(KeyEvent.VK_E);
  104. mnuEdit.setDisplayedMnemonicIndex(0);
  105. mnuBar.add(mnuEdit);
  106.  
  107. JMenu mnuEditSearch = new JMenu("Search");
  108. mnuEditSearch.setMnemonic(KeyEvent.VK_R);
  109. mnuEditSearch.setDisplayedMnemonicIndex(3);
  110. mnuEdit.add(mnuEditSearch);
  111.  
  112. JMenuItem mnuEditSearchByName = new JMenuItem("by Last Name");
  113. mnuEditSearchByName.setMnemonic(KeyEvent.VK_L);
  114. mnuEditSearchByName.setDisplayedMnemonicIndex(3);
  115. mnuEditSearch.add(mnuEditSearchByName);
  116. mnuEditSearchByName.setActionCommand("last");
  117. mnuEditSearchByName.addActionListener(this);
  118.  
  119. JMenuItem mnuEditSearchByProdNum = new JMenuItem("by Product Number");
  120. mnuEditSearchByProdNum.setMnemonic(KeyEvent.VK_P);
  121. mnuEditSearchByProdNum.setDisplayedMnemonicIndex(3);
  122. mnuEditSearch.add(mnuEditSearchByProdNum);
  123. mnuEditSearchByProdNum.setActionCommand("production");
  124. mnuEditSearchByProdNum.addActionListener(this);
  125.  
  126. JMenuItem mnuEditSearchByState = new JMenuItem("by State");
  127. mnuEditSearchByState.setMnemonic(KeyEvent.VK_S);
  128. mnuEditSearchByState.setDisplayedMnemonicIndex(3);
  129. mnuEditSearch.add(mnuEditSearchByState);
  130. mnuEditSearchByState.setActionCommand("state");
  131. mnuEditSearchByState.addActionListener(this);
  132.  
  133. return mnuBar;
  134. }
  135.  
  136. //create the content pane
  137. public Container createContentPane()
  138. {
  139.  
  140. //construct and populate the north panel
  141. JPanel northPanel = new JPanel();
  142. northPanel.setLayout(new FlowLayout());
  143.  
  144. //create the JTextPane and center panel
  145. JPanel centerPanel = new JPanel();
  146. setTabsAndStyles(textPane);
  147. textPane = addTextToTextPane();
  148. JScrollPane scrollPane = new JScrollPane(textPane);
  149. scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  150. scrollPane.setPreferredSize(new Dimension(500, 200));
  151. centerPanel.add(scrollPane);
  152.  
  153. //create Container and set attributes
  154. Container c = getContentPane();
  155. c.setLayout(new BorderLayout(10,10));
  156. c.add(northPanel,BorderLayout.NORTH);
  157. c.add(centerPanel,BorderLayout.CENTER);
  158.  
  159. return c;
  160. }
  161.  
  162. //method to create tab stops and set font styles
  163. protected void setTabsAndStyles(JTextPane textPane)
  164. {
  165. //create Tab Stops
  166. TabStop[] tabs = new TabStop[2];
  167. tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  168. tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  169. TabSet tabset = new TabSet(tabs);
  170.  
  171. //set Tab Style
  172. StyleContext tabStyle = StyleContext.getDefaultStyleContext();
  173. AttributeSet aset = tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
  174. textPane.setParagraphAttributes(aset, false);
  175.  
  176. //set Font Style
  177. Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  178.  
  179. Style regular = textPane.addStyle("regular", fontStyle);
  180. StyleConstants.setFontFamily(fontStyle, "SansSerif");
  181.  
  182. Style s = textPane.addStyle("italic", regular);
  183. StyleConstants.setItalic(s, true);
  184.  
  185. s = textPane.addStyle("bold", regular);
  186. StyleConstants.setBold(s, true);
  187.  
  188. s = textPane.addStyle("large", regular);
  189. StyleConstants.setFontSize(s, 16);
  190. }
  191.  
  192. //method to add new text to the JTextPane
  193. public JTextPane addTextToTextPane()
  194. {
  195. Document doc = textPane.getDocument();
  196. try
  197. {
  198. //clear previous text
  199. doc.remove(0, doc.getLength());
  200.  
  201. //insert title
  202. doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
  203.  
  204. //insert detail
  205. for (int j = 0; j < lastName.length; j++)
  206. {
  207. doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
  208. doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
  209. doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
  210. doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
  211. doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
  212. doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
  213. doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
  214. doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
  215. doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
  216. }
  217. }
  218. catch (BadLocationException ble)
  219. {
  220. System.err.println("Couldn't insert text.");
  221. }
  222.  
  223. return textPane;
  224. }
  225.  
  226. //event to process user clicks
  227. public void actionPerformed(ActionEvent e)
  228. {
  229. String arg = e.getActionCommand();
  230.  
  231. //user clicks Exit on the File menu
  232. if (arg == "Exit")
  233. System.exit(0);
  234.  
  235. //user clicks title on the Search submenu
  236. if (arg == "last")
  237. search(arg, last);
  238.  
  239. //user clicks studio on the Search submenu
  240. if (arg == "production")
  241. search(arg, production);
  242.  
  243. //user clicks state on the Search submenu
  244. if (arg == "state")
  245. search(arg, state);
  246. }
  247.  
  248. //method to search arrays
  249. public void search(String searchField, String searchArray[])
  250. {
  251. try
  252. {
  253. Document doc = textPane.getDocument(); //assign text to document object
  254. doc.remove(0,doc.getLength()); //clear previous text
  255.  
  256. //display column titles
  257. doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
  258.  
  259. //prompt user for search data
  260. String search = JOptionPane.showInputDialog(null, "Please enter the "+ searchField);
  261. boolean found = false;
  262.  
  263. //search arrays
  264. for (int i = 0; i < lastName.length; i++)
  265. {
  266. if (search.compareTo(searchArray[i])==0)
  267. {
  268. doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
  269. doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
  270. doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
  271. doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
  272. doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
  273. doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
  274. doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
  275. doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
  276. doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
  277. found = true;
  278. }
  279. }
  280. if (found == false)
  281. {
  282. JOptionPane.showMessageDialog(null, "Your search produced no results.", "No results found",JOptionPane.INFORMATION_MESSAGE);
  283. }
  284. }
  285. catch (BadLocationException ble)
  286. {
  287. System.err.println("Couldn't insert text.");
  288. }
  289. }
  290.  
  291. //main method executes at run time
  292. public static void main(String args[])
  293. {
  294. JFrame.setDefaultLookAndFeelDecorated(true);
  295. CustomerInfo f = new CustomerInfo();
  296. f.readFile();
  297. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  298. f.setJMenuBar(f.createMenuBar());
  299. f.setContentPane(f.createContentPane());
  300. f.setSize(600,375);
  301. f.setVisible(true);
  302. }
  303. }

I am getting an error starting at line 205 with the variable "lastName". I am getting the 'cannont resovle symbol' error with this variable amd the other array variable I had set up to read the columns of a text file. I am thinking that since the technique of reading the text file is embedded in its own prodecure that maybe the variables are not recongized by other procedures.

I would appreciate any help that anyone can provide. I am at a loss as to what to do. Here is a copy of the text file I am using. Thanks in advance.
  1. CustID,FirstName,Last Name,Address1,Address2,City,State,Country,ProductID
  2.  
  3. 101,William,Jones,1242 Elm St,,Chicago,IL,US,5462
  4.  
  5. 102,Antoine,Sargeant,83 Main Ave,Apt 3A,Castle Rock,OR,US,13569
  6.  
  7. 103,Linda,Montoya,51198 3rd Ave,,Brooklyn,NY,US,5462
  8.  
  9. 104,Bernard,St John,Alwyn's Mews,128 Dennis Alley,London,,UK,9073-C
  10.  
  11. 105,Knute,Olmstead,3135 Kirche,,Malmo,,SE,6215-G
  12.  
  13. 106,Karen,Noyes,163 Morris AVe,,Denver,CO,US,45123
  14.  
  15. 107,Sam,Ng,9081 Allard St,,San Lucia,CA,US,13569
  16.  
  17. 108,Carl,Johnson,62161 3rd St,,Morningside,IN,US,5462
  18.  
  19. 109,Janet,Smith,1531 Maple Grove Rd,,Maple Grove,MA,US,45123
  20.  
  21. 110,Trevor,Beckwith,999 Archibald Ct,5G,Little Farthington,,UK,9073-B
  22.  
  23. 111,Mark,Jonson,15245 Lingle Terrace,,Two Forks,AL,US,5462
  24.  
  25. 112,Millie,Dingle,16 Sixth St,Ste. 11345,Austin,TX,US,5498
  26.  
  27. 113,David,Winegard,7045 Portman Dr.,,East Millersville,OH,US,13569
  28.  
  29. 114,Nathan,MacDonald,61 Freedom Pl,,Carthage,MD,US,45123
  30.  
  31. 115,Cathy,Walton,2 Main St,,Four Mile, SD,US,5462
  32.  
  33. 116,Stephen,Morrisson,712 Lovely Dr,,Allard,MO,US,5462
  34.  
  35. 117,Jacques,Millefois,77B Ave Fouchard,,Arles,,FR,6215-A
  36.  
  37. 118,Andy,Dorn,359 Park Ln,,Memphis,TN,US,13569
  38.  
  39. 119,Tonya,Williams,793 Forest Dr,213,Dallas,Morris Glen,NJ,US,5462
  40.  
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Reading a txt file into a swing component

 
0
  #2
Nov 4th, 2008
Line 205? Where's line 205? Please repost using Java code tags. which will add line numbers, or highlight line 205 in red.


[code=JAVA]
// paste code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: dbwalters is an unknown quantity at this point 
Solved Threads: 0
dbwalters dbwalters is offline Offline
Newbie Poster

Re: Reading a txt file into a swing component

 
0
  #3
Nov 4th, 2008
Sorry about that. I didn't think about adding the line numbers. Here it is again...hopefully.


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.text.*;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.FileNotFoundException;
  9.  
  10. public class CustomerInfo extends JFrame implements ActionListener
  11. {
  12. //construct components
  13. JTextPane textPane = new JTextPane();
  14.  
  15. //initialize data in arrays
  16. public void readFile()
  17. {
  18. BufferedReader br = null;
  19. String[] custID = new String[100];
  20. String[] firstName = new String[100];
  21. String[] lastName = new String[100];
  22. String[] addressOne = new String[100];
  23. String[] addressTwo = new String[100];
  24. String[] myCity = new String[100];
  25. String[] myState = new String[100];
  26. String[] myCountry = new String[100];
  27. String[] productionID = new String[100];
  28. int num = 0;
  29.  
  30. try
  31. {
  32. br = new BufferedReader(new FileReader("Database.txt"));
  33. String line = null;
  34.  
  35. while ((line = br.readLine()) != null)
  36. {
  37. String[] values = line.split(",");
  38. if (values.length > 1)
  39. {
  40. custID[num] = values[0];
  41. firstName[num] = values[1];
  42. lastName[num] = values[2];
  43. addressOne[num] = values[3];
  44. addressTwo[num] = values[4];
  45. myCity[num] = values[5];
  46. myState[num] = values[6];
  47. myCountry[num] = values[7];
  48. productionID[num] = values[8];
  49. num++;
  50. }
  51. }
  52. }
  53. catch (FileNotFoundException ex)
  54. {
  55. ex.printStackTrace();
  56. }
  57. catch (IOException ex)
  58. {
  59. ex.printStackTrace();
  60. }
  61. finally
  62. {
  63. try
  64. {
  65. if (br != null)
  66. br.close();
  67. }
  68. catch (IOException ex)
  69. {
  70. ex.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. //construct instance of CustomerInfo
  76. public CustomerInfo()
  77. {
  78. super("Customer Information Database");
  79. }
  80.  
  81. //create the menu system
  82. public JMenuBar createMenuBar()
  83. {
  84. //create an instance of the menu
  85. JMenuBar mnuBar = new JMenuBar();
  86. setJMenuBar(mnuBar);
  87.  
  88. //construct and populate the File menu
  89. JMenu mnuFile = new JMenu("File", true);
  90. mnuFile.setMnemonic(KeyEvent.VK_F);
  91. mnuFile.setDisplayedMnemonicIndex(0);
  92. mnuBar.add(mnuFile);
  93.  
  94. JMenuItem mnuFileExit = new JMenuItem("Exit");
  95. mnuFileExit.setMnemonic(KeyEvent.VK_X);
  96. mnuFileExit.setDisplayedMnemonicIndex(1);
  97. mnuFile.add(mnuFileExit);
  98. mnuFileExit.setActionCommand("Exit");
  99. mnuFileExit.addActionListener(this);
  100.  
  101. //construct and populate the Edit menu
  102. JMenu mnuEdit = new JMenu("Edit", true);
  103. mnuEdit.setMnemonic(KeyEvent.VK_E);
  104. mnuEdit.setDisplayedMnemonicIndex(0);
  105. mnuBar.add(mnuEdit);
  106.  
  107. JMenu mnuEditSearch = new JMenu("Search");
  108. mnuEditSearch.setMnemonic(KeyEvent.VK_R);
  109. mnuEditSearch.setDisplayedMnemonicIndex(3);
  110. mnuEdit.add(mnuEditSearch);
  111.  
  112. JMenuItem mnuEditSearchByName = new JMenuItem("by Last Name");
  113. mnuEditSearchByName.setMnemonic(KeyEvent.VK_L);
  114. mnuEditSearchByName.setDisplayedMnemonicIndex(3);
  115. mnuEditSearch.add(mnuEditSearchByName);
  116. mnuEditSearchByName.setActionCommand("last");
  117. mnuEditSearchByName.addActionListener(this);
  118.  
  119. JMenuItem mnuEditSearchByProdNum = new JMenuItem("by Product Number");
  120. mnuEditSearchByProdNum.setMnemonic(KeyEvent.VK_P);
  121. mnuEditSearchByProdNum.setDisplayedMnemonicIndex(3);
  122. mnuEditSearch.add(mnuEditSearchByProdNum);
  123. mnuEditSearchByProdNum.setActionCommand("production");
  124. mnuEditSearchByProdNum.addActionListener(this);
  125.  
  126. JMenuItem mnuEditSearchByState = new JMenuItem("by State");
  127. mnuEditSearchByState.setMnemonic(KeyEvent.VK_S);
  128. mnuEditSearchByState.setDisplayedMnemonicIndex(3);
  129. mnuEditSearch.add(mnuEditSearchByState);
  130. mnuEditSearchByState.setActionCommand("state");
  131. mnuEditSearchByState.addActionListener(this);
  132.  
  133. return mnuBar;
  134. }
  135.  
  136. //create the content pane
  137. public Container createContentPane()
  138. {
  139.  
  140. //construct and populate the north panel
  141. JPanel northPanel = new JPanel();
  142. northPanel.setLayout(new FlowLayout());
  143.  
  144. //create the JTextPane and center panel
  145. JPanel centerPanel = new JPanel();
  146. setTabsAndStyles(textPane);
  147. textPane = addTextToTextPane();
  148. JScrollPane scrollPane = new JScrollPane(textPane);
  149. scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  150. scrollPane.setPreferredSize(new Dimension(500, 200));
  151. centerPanel.add(scrollPane);
  152.  
  153. //create Container and set attributes
  154. Container c = getContentPane();
  155. c.setLayout(new BorderLayout(10,10));
  156. c.add(northPanel,BorderLayout.NORTH);
  157. c.add(centerPanel,BorderLayout.CENTER);
  158.  
  159. return c;
  160. }
  161.  
  162. //method to create tab stops and set font styles
  163. protected void setTabsAndStyles(JTextPane textPane)
  164. {
  165. //create Tab Stops
  166. TabStop[] tabs = new TabStop[2];
  167. tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  168. tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
  169. TabSet tabset = new TabSet(tabs);
  170.  
  171. //set Tab Style
  172. StyleContext tabStyle = StyleContext.getDefaultStyleContext();
  173. AttributeSet aset = tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
  174. textPane.setParagraphAttributes(aset, false);
  175.  
  176. //set Font Style
  177. Style fontStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  178.  
  179. Style regular = textPane.addStyle("regular", fontStyle);
  180. StyleConstants.setFontFamily(fontStyle, "SansSerif");
  181.  
  182. Style s = textPane.addStyle("italic", regular);
  183. StyleConstants.setItalic(s, true);
  184.  
  185. s = textPane.addStyle("bold", regular);
  186. StyleConstants.setBold(s, true);
  187.  
  188. s = textPane.addStyle("large", regular);
  189. StyleConstants.setFontSize(s, 16);
  190. }
  191.  
  192. //method to add new text to the JTextPane
  193. public JTextPane addTextToTextPane()
  194. {
  195. Document doc = textPane.getDocument();
  196. try
  197. {
  198. //clear previous text
  199. doc.remove(0, doc.getLength());
  200.  
  201. //insert title
  202. doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
  203.  
  204. //insert detail
  205. for (int j = 0; j < lastName.length; j++)
  206. {
  207. doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
  208. doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
  209. doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
  210. doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
  211. doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
  212. doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
  213. doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
  214. doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
  215. doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
  216. }
  217. }
  218. catch (BadLocationException ble)
  219. {
  220. System.err.println("Couldn't insert text.");
  221. }
  222.  
  223. return textPane;
  224. }
  225.  
  226. //event to process user clicks
  227. public void actionPerformed(ActionEvent e)
  228. {
  229. String arg = e.getActionCommand();
  230.  
  231. //user clicks Exit on the File menu
  232. if (arg == "Exit")
  233. System.exit(0);
  234.  
  235. //user clicks title on the Search submenu
  236. if (arg == "last")
  237. search(arg, last);
  238.  
  239. //user clicks studio on the Search submenu
  240. if (arg == "production")
  241. search(arg, production);
  242.  
  243. //user clicks state on the Search submenu
  244. if (arg == "state")
  245. search(arg, state);
  246. }
  247.  
  248. //method to search arrays
  249. public void search(String searchField, String searchArray[])
  250. {
  251. try
  252. {
  253. Document doc = textPane.getDocument(); //assign text to document object
  254. doc.remove(0,doc.getLength()); //clear previous text
  255.  
  256. //display column titles
  257. doc.insertString(0,"CUST ID\tLAST NAME\tFIRST NAME\tADDRESS 1\tADDRESS 2\tCITY\tSTATE\tCOUNTRY\tPRODUCTION ID\n",textPane.getStyle("large"));
  258.  
  259. //prompt user for search data
  260. String search = JOptionPane.showInputDialog(null, "Please enter the "+ searchField);
  261. boolean found = false;
  262.  
  263. //search arrays
  264. for (int i = 0; i < lastName.length; i++)
  265. {
  266. if (search.compareTo(searchArray[i])==0)
  267. {
  268. doc.insertString(doc.getLength(), custID[j] + "\t", textPane.getStyle("bold"));
  269. doc.insertString(doc.getLength(), firstName[j] + "\t", textPane.getStyle("regular"));
  270. doc.insertString(doc.getLength(), lastName[j] + "\t", textPane.getStyle("regular"));
  271. doc.insertString(doc.getLength(), addressOne[j] + "\t", textPane.getStyle("regular"));
  272. doc.insertString(doc.getLength(), addressTwo[j] + "\t", textPane.getStyle("regular"));
  273. doc.insertString(doc.getLength(), myCity[j] + "\t", textPane.getStyle("regular"));
  274. doc.insertString(doc.getLength(), myState[j] + "\t", textPane.getStyle("regular"));
  275. doc.insertString(doc.getLength(), myCountry[j] + "\t", textPane.getStyle("regular"));
  276. doc.insertString(doc.getLength(), productionID[j] + "\n", textPane.getStyle("regular"));
  277. found = true;
  278. }
  279. }
  280. if (found == false)
  281. {
  282. JOptionPane.showMessageDialog(null, "Your search produced no results.", "No results found",JOptionPane.INFORMATION_MESSAGE);
  283. }
  284. }
  285. catch (BadLocationException ble)
  286. {
  287. System.err.println("Couldn't insert text.");
  288. }
  289. }
  290.  
  291. //main method executes at run time
  292. public static void main(String args[])
  293. {
  294. JFrame.setDefaultLookAndFeelDecorated(true);
  295. CustomerInfo f = new CustomerInfo();
  296. f.readFile();
  297. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  298. f.setJMenuBar(f.createMenuBar());
  299. f.setContentPane(f.createContentPane());
  300. f.setSize(600,375);
  301. f.setVisible(true);
  302. }
  303. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Reading a txt file into a swing component

 
0
  #4
Nov 4th, 2008
You have a scoping issue. You define lastName in function readFile. lastName is not a class data member, so it goes out of scope once the readFile function is over. You try to use it in the addTextToTextPane function. You can't do that. If you intend lastName to be global/class variable, you should declare it at a global scale. Anything initially declared inside of a function is not a global/class variable.
Last edited by VernonDozier; Nov 4th, 2008 at 4:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: dbwalters is an unknown quantity at this point 
Solved Threads: 0
dbwalters dbwalters is offline Offline
Newbie Poster

Re: Reading a txt file into a swing component

 
0
  #5
Nov 4th, 2008
That's the conclusion that I was thinking about also...I am at a lost as to the best way to go about making those array variables global...I dont suppose you have any suggestions? One example would be a good start for me to learn from.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 197
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Reading a txt file into a swing component

 
0
  #6
Nov 4th, 2008
  1. public class anExample{
  2. private Object obj;
  3. private static Object obj2;
  4. public Object obj3;
  5. public static Object obj4;
  6.  
  7. public static void main(String[] args){
  8. }
  9.  
  10. //other methods
  11.  
  12. }

From a method inside the class with an instance of anExample:
You can refer to (use) obj and obj3.

From a static method inside the class without an instance of anExample:
You can use obj2 and obj4. Being static means the class has one copy of them that is shared by the entire class.

From a method inside a different class, with an instance of anExample:
You can refer to obj3

From a method inside a different class, without an instance of anExample:
you can refer to obj4


So the answer to your question would be the following, although I didn't look at your code. Either use a public or protected variable in your main driver class (the one thats run when you first start the program), use a private instance variable and provide accessors and mutators, or you could use a static variable. In either case, if you wanted easy access to the variable in both of your classes, consider using constructors that take the parameter as an argument.
Last edited by BestJewSinceJC; Nov 4th, 2008 at 5:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 20
Reputation: dbwalters is an unknown quantity at this point 
Solved Threads: 0
dbwalters dbwalters is offline Offline
Newbie Poster

Re: Reading a txt file into a swing component

 
0
  #7
Nov 4th, 2008
Thanks for the pointers...I think I am going to try something like this:

  1. public class Global {
  2. public static int = 37;
  3. public static String s = "aaa";
  4. }
  5.  
  6. public class test {
  7. public static void main(Strings args[])
  8. {
  9. Global.x = Global.x + 100;
  10. Global.s = "bbb";
  11. }
  12. }

From what I see in this example, I will go ahead and implement something similar and rewrite my array varaiables accordingly......I'll reply with the results.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 197
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Reading a txt file into a swing component

 
0
  #8
Nov 4th, 2008
Yes, the above example will work, as long as you declare x properly. It should say public static int x = 37;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Reading a txt file into a swing component

 
0
  #9
Nov 4th, 2008
Originally Posted by dbwalters View Post
Thanks for the pointers...I think I am going to try something like this:

  1. public class Global {
  2. public static int = 37;
  3. public static String s = "aaa";
  4. }
  5.  
  6. public class test {
  7. public static void main(Strings args[])
  8. {
  9. Global.x = Global.x + 100;
  10. Global.s = "bbb";
  11. }
  12. }

From what I see in this example, I will go ahead and implement something similar and rewrite my array varaiables accordingly......I'll reply with the results.
Why create a new class? How about just changing lines 18 - 28 from your original code and split them up? Put the declarations at the class level, put the rest of the lines in the constructor or keep it them in the readFile function...

  1. public class CustomerInfo extends JFrame implements ActionListener
  2. {
  3. String[] custID;
  4. String[] firstName;
  5. String[] lastName;
  6. String[] addressOne;
  7. String[] addressTwo;
  8. String[] myCity;
  9. String[] myState;
  10. String[] myCountry;
  11. String[] productionID;
  12.  
  13.  
  14. public void readFile()
  15. {
  16. BufferedReader br = null;
  17. custID = new String[100];
  18. firstName = new String[100];
  19. lastName = new String[100];
  20. addressOne = new String[100];
  21. addressTwo = new String[100];
  22. myCity = new String[100];
  23. myState = new String[100];
  24. myCountry = new String[100];
  25. productionID = new String[100];
  26. int num = 0;
  27.  
  28. // rest of code
  29. }
  30. }

Now you can use the variables in lines 3 - 11 globally. Or again, if you are only calling readFile once, you can put the new commands in the constructor.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 197
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Reading a txt file into a swing component

 
0
  #10
Nov 4th, 2008
@ Vernon: I got the impression that he isn't creating a new class, he was just giving an example of what he thought the concepts were, and he's going to use those concepts in the class he currently has. No?
Last edited by BestJewSinceJC; Nov 4th, 2008 at 5:26 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC