• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help understanding how to solve some programming problems.  RSS feed

 
Cliff Karlsson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an asignement where I am supposed to create a GUI for a java application which displays a form and is able to save the entered data to file after clicking a button. The asignement have more tasks but I need some help with the basics.

Lets say I have a Swing UI with ten labels and JTextFields. The user can enter some data in all or some of the fields and after clicking a button that data is to be saved to a file. The problem I have is that before the actual data there has to be a sertain "code". If for example the user only enters something in field 3 and 5 the created file should look something like this:

#3465 SKA <data from field 3>
#6577 SKA <data from field 5>

Every field must be linked to a special ID that also needs to be written to the file. The actual program need to have 50-100 JtextFields as it is supposed to simulate a sort of tax-form where everything need to be linked to the specific tax-section.

I really can't figure out how to make this happen effectivly. I had an idea that maybe I could add the codes to a arraylist so that they match the order of the JTextFields and do some check when the button is clicked like:


But is this possible to do in some way?

 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
50-100 text fields is way to much for one screen full of input. How many tax forms do you have? How many fields per tax form? Perhaps you could use tabs, menu, selection, or some other means to chose a tax form and then just display the fields for that tax form. You'd also want your text fields to come from a List or array. You'd probably want to create a class to keep track of your text fields. Example:
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are going to make a save button for sure, you shold be a little bit familiar with writing to files in java.
I have tryed to fix the code you wrote to give you better way. pay attention to the changes . why do you need the counter ? or the i++ , I have omitted them.

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of writing code outside the class, I would first look to extend the JTextField class instead so that I can maintain consistent semantics and usage.

For example,

Since constructors are not inherited, you'd have to provide whichever ones you use or provide constructors that take the taxFormId as an additional argument. The important thing is to call the appropriate super() constructor to initialize the instance in a way that is consistent with the original JTextField.

This way, you can write code that looks like normal Swing code by programming to the JTextField API and then program to the specific behavior of your TaxFormField when necessary. This also encourages you to keep specialized tax form-specific logic separate from your general JTextField logic.

Choices like these will keep your code in line with SOLID design principles, particularly the Open-Closed Principle and the Liskov Substitution Principle. And of course, this is a more object-oriented way to add specialized behavior. After all, Java is an object-oriented language.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A point of clarification about what I said regarding overriding (edit: constructors are not inherited therefore you can't really override them) providing constructors if you're going to extend JTextField. Say for example you were using the JTextField(String, int) constructor before. When you extend the JTextField, you might provide these constructors:

The example in the JTextField JavaDocs shows a nested static Document extension. I would also look into that example and experiment with code that follows that pattern. Maybe have something like a nested static class TaxFormDocument extends PlainDocument that gets delegated the responsibility of inserting/formatting the tax id provided to the TaxFormField constructors and adding it to the text entered by the user.

These ideas all off the top of my head so you'll have to do some experimentation to see if these are workable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!