Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Jcombobox And File Handling !

combobox

  • Please log in to reply
32 replies to this topic

#1 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 13 April 2012 - 07:13 AM

I am creating a program in swing that uses a Jcombobox where the items in the jcombobox are linked to a text file,that is , as soon as I select any of the item in the jcombobox the corresponding text is displayed in a text area below. Now I want to add an option for the end user to be able to add items to the list and so a corresponding file gets created as soon as I add the item to the list. Also that list and its corresponding file will be accessible in the future.
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 13 April 2012 - 11:08 AM

Is there anything in particular you had a question about regarding this? Where do you need help? What's your code so far?
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 13 April 2012 - 08:28 PM

I will be using switch case to choose an item and then retrieve its corresponding file but the problem once the end user adds an item to the list how will a new case be created in the switch block for the new item and the corresponding. Because i want the new item to be accessible in future.
  • 0

#4 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 14 April 2012 - 04:14 AM

To change your code (the switch statement's case) you will have to use an editor. You will need to change your design to add new processing for new items added to the list.
When a new item is added to the list, how will you get the processing that is to be done when that item is later selected? What code would go in the case statement? Or would it use the code for an existing case?
  • 0

#5 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 14 April 2012 - 05:00 AM

the new items will be added by the end user and no programmer, using a simple add button. And i cannot find any means to add a new case to switch using the add button in the gui!

  • 0

#6 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 14 April 2012 - 05:04 AM

means to add a new case to switch

You change the source to your program by using an editor and recompiling.
As I said in my last post, you will need to change the design of your program.

What code would be added following the new case?
  • 0

#7 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 14 April 2012 - 06:03 AM

Okay let me break it down for you!
The Dropdown list contains a list of files which when clicked upon displays the text in the selected file in a textarea below. Now a switch case block is used to select and read the files using a file reader and bufferedreader and then print it onto the textarea. Now I also have an option for the end-user( normal people who are going to use the software) to add a new file in the list and the add button will use filewriter and bufferedwriter to store the new file. But now i want to access the new file in the list so i want a corresponding case to be created which will read the new files like the already created files.But i see no solution to this problem!
  • 0

#8 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 14 April 2012 - 07:13 AM

i want a corresponding case to be created

You can not change a switch statement or any java source statement when the program is executing. To add a case to a switch statement, you must edit the source file and recompile the source.

What do each of the case statements execute? Are they the same code except with different filenames and text areas?
Why not use an arraylist to hold instances of classes each one tailored with the filenames and other things needed.
Indexing into an arraylist would work exactly the same as a switch statement with case.
  • 0

#9 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 14 April 2012 - 07:16 AM

You will not be able to use a switch case structure to process the items in this list. The reason is because you want the list items to be populated after the program is compiled. You will need a different algorithm.

Let me ask you this: What sort of processing do you want to be done for each element of the list? How different are the various functions from one another for each item in the list?

In general, you will have to write one method which takes the selected item from the list (either the selected index or the string value of the selected list item) and use that to affect your program flow.

For example, suppose I wanted to make a JComboBox with a list of products. The products should be displayed by name, but each product has an associated price. Each time a product is selected from the JComboBox, I want to add the price of that item to the running total. I also want the user to be able to add new products and prices later. Now, if I do it with a switch case as you have, I have a problem: I cannot change the code without recompiling.

Instead, I should build a List of Strings, with each list item storing the name of one product. I should also build a HashMap, mapping a String type to a Double type (name->price). Now, when the program launches, I read the user's product file into the list, and populate the HashMap using the product name as the key and the price as the value. Finally, I populate the JComboList with the items in the List.

When the user selects an item from the JComboList, grab its name (String) and use it to retrieve the associated price from the HashMap. This way, you have one method which handles all cases including future cases not yet realized.
  • 1

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#10 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 14 April 2012 - 07:49 AM

This might be a very lame question. But how many characters can a String hold? because I will be storing approximately one page of data in one element of the string array.And so if i hold the text in an array element over a file will i get the line breaks exactly same as the time I added to the list?
  • 0

#11 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 14 April 2012 - 07:57 AM

How much data do you want to store in a String? Do you want to read and store the contents of all of the files in Strings?
Do you need it all at once or can you use it in pieces and leave it in the files on disk until you need it in memory and then read it in?

Depending on how you read the data from the files, you may have to put the line end characters at the end of each line as you read it into the program and save it.
  • 0

#12 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 14 April 2012 - 07:58 AM

The max length of a String is Integer.MAX_VALUE, so you shouldn't have to worry about any of that. Strings can also store line breaks and any other printable characters in them. It all depends on how you read the file. If you read it one line at a time, you won't get the line breaks, and each line will be stored in a separate element. If you read characters straight into a buffer, you'll capture line breaks and everything.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download