Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,191 members, 7,953,684 topics. Date: Thursday, 19 September 2024 at 11:12 PM

A Problem With Java Gui Design - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / A Problem With Java Gui Design (2736 Views)

Learning To Program With Java by the Fulaman / Hope For C++ Newbies: Gui Toolkits / How To Create A Simple Calculator Using Java Programing Language GUI (2) (3) (4)

(1) (Reply) (Go Down)

A Problem With Java Gui Design by Danyl(m): 7:11pm On Sep 01, 2010
good day everyone out there. Iam having a problem with a program. I want to write a GUI that contains Questions which is going to be held by JLabels and answers held by JRadioButtons and ButtonGroup Objects but the problem is that i dont know how to code it such that the questions will be coming out one after the other at the click of a "Previous" and "Next" Buttons , and that users will select an option from the JRadioButtons and then click submit at the end, and more so i have tried to use Canvas to draw Strings that holds the questions but its not working can somebody please help me out. I'll somuch appreciate your timely response thank you
Re: A Problem With Java Gui Design by mj(m): 11:31pm On Sep 01, 2010
You should use arrays for the questions, if you dont know much about arrays then ready more on it.
Re: A Problem With Java Gui Design by Shimao(m): 1:53am On Sep 02, 2010
Though could be done in different ways, try this;

Abstract your questions into objects of a Question class, which holds at least the question and its options and answer.
Use a List (preferably) or an Array to hold your question objects;
Keep a counter to hold the current position in your question pool;
Increment or decrement your counter with each click of next or previous;
Use the value of your counter as a key to retrieve a question from the pool;
Update your interface to display the current question and its options;
Use a generic ActionListener to set the answer on a current question;


Fill in the gaps as required. The rest should be intuitive.
Re: A Problem With Java Gui Design by chukxy: 9:48am On Sep 02, 2010
@Danyl , lets do it this way. Lets use MVC architecture. You know what I mean:

M - Model
V- View
C- Controller


For the model, we are going to use any of the database such as: ms excel,ms access,mysql,ms- sql, oracle etc.
Any one you feel comfortable using is ok. The next thing is for you to design the table and I think the following fields should come handy:
1. question field(varcha)
2.choice1 field(varcha)
3.choice2 field(varcha)
4.choice3 field(varcha)
5.choice4 filed(varcha)
6.answer field(int)
7.question id(should be the primary key to avoid duplication of question)
After designing and creating the table, you use batch upload to upload the data as you wish and make the initial value of the answer field to be zero.

For the view part of it, you create JLabel objects- one will hold the value of the question , one to hold the result of the candidate,one to hold the time , etc. and other thing you deem fit. Create JRadioButton to hold the four choices as reflected in the table and add same in the Button group. Create 7 button objects to get user inputs and I think the following buttons should come handy

1.StartTest
2.EndTest
3.First
4.Last
5.Next
6.previous
7.Exit



For the controller, you should add actionListener in each of the button listed above and write the event handling code in your action performed method. In this case, when the user clicks on the start test button, all the data you stored in the table will be returned to your resultset object(make sure you set it to editable(true)). then retrieve the result one after the other based on the user click( e.g First button, will retrieve the first question in table and set question label with the value of question one, also will set the values of the radion button accordingly). When the user select the radio button of his choice, it will automatically modify the value of the answer field since it is editable.The modification is based on if the user gets the answer correct, the value can change to one , but if the user gets it wrong the answer field remains zero. At the end of the test, you select the answer field and sum the total marks and update your answer label . Also reset the answer field in your table to the former value to enable other user make use of it.


I think if you do it this way, you can even make online test and the sacability and maintainability will be easy.
Re: A Problem With Java Gui Design by SayoMarvel(m): 3:36pm On Sep 02, 2010
@chukxy Don't you think this MVC approach may seem too complicated to our friend? (with all due respect sir). An easeir way is to use a Map (this is in Java Collections Framework). Create a class named Question. Make sure your class Question extends interface java.io.Serializable (shortly, I'll tell you why). Let the Question class have the following fields; String questionString, String[] options, int answerIndex. Make sure you obey the rules of encapsulation (if you don't understand that, you have to abandon that project and pick up a textbook because you may not be ripe enough for the project, just joking :-)). Now our Question class (that knows the answer to each question is ready to be used). Create another class named QuestionBuilder (let this class use java.util.Scanner's readLine() method to accept Strings from the command line to represent questionString and options and also and int representing the answerIndex using java.util.Scanner's nextInt() method.) Pass this data to your class Question's constructor to create a Question object.
It should look like this
Question quest = new Question(qusetionString, options, answerIndex);
Serialize the object (this saves your object in an actual file -- maybe "data.txt" but you can't read it in NotePad beacuse its actually not a text file but a binary file).
Your code should look like this.
java.io.FileOutputStream fos = new java.io.FileOutputStream(new java.io.File("data.txt"wink);
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(fis);
oos.writeObject(quest); //repeat this action for every Question objec created.

Afterwards, do this close the stream to release the file resource
your code should look like this.
oos.close();


Now we have all our Questions (that know their answers in a file data.txt, bingo!!!).

The next thing is for our application (who wants to be a millionaire) to read the Questions from the file and place each Question in a Map. Here it will be a map of Map<Integer, Question> (don't be scared but this has definitely led you to GENERICS or what do you expect? a ten lines program?).
The Integer will be your SerialNumber (this will enable your next and prevous buttons to retrieve the questions) and it will also be your KEY. The Question will be your Value.

Sorry you have to Deserialize your data.txt to read the Questions into the Map.
you should have something like this.

java.io.FileInputStream fis = new java.io.FileInputStream(new java.io.File("data.txt"wink);
java.io.ObjectInputStream ois = new java.io.ObjectInputStream(fis);
Question actualQuestion = (Question) ois.readObject(quest); //repeat this action while there is still more data in the file (you know how ta do dat, don't you?)


Remember to do all these I/O in a try catch finally context because they throw a lot of exceptions and they don't get tired of it (like IOEXception, SecurityException, ClassCastException, and lots more).

Now you have a Map of serial numbers and Questions (ofcouse with embedded answers). Integrating this into your GUI should not be a problem.

map.add(1, actualQuestion); //repeat this to input each question read from the data.txt file (don't be funny, that 1 will change to 2 in the next value read so you better keep a counter

something like this

int a = 0;
map.add(a, actualQuestion);
a++;


Since i assumed you understand the concepts of abstraction and encapsulation you should have public methods like this in your Questions class getOptions(), getAnswer();
Integrate this into your GUI.


You should have something like this

if(Question.getAnswer() == buttonGroup.getSelectedIndex())
//answer correct
else
//wrong answer


I hope I've done some justice to your problem.

I KNOW I HAVE TO DO THIS. I ALSO GREW UP PARTLY ON NAIRALAND. A BIG RESPECT TO THOSE THAT BROUGHT ME UP.


IF YOU WANT ME TO DEVELOP THE APPLICATION FOR YOU WITH COMPLETE FULLY DOCUMENTED AND INDENTED SOURCE CODE ALONG WITH OTHER RESOURCES, PACKAGED .exe or .jar file, pictures (e.g for the next/previous buttons, splash screen etc) everything, I can do it for you. All you have to do is mail me through marvelindaclub@yahoo.com.
Have a nice day.
Re: A Problem With Java Gui Design by Shimao(m): 6:08am On Sep 03, 2010
I could be mistaken but it seems to me the problem the guy has is with the GUI, how data is displayed and input obtained from user, and not how data is stored or retrieved or the workings of his system.
More so you guys seem to have expanded the SuD and would probably leave the guy confused.

@Danyl Two objects you will certainly need are;
Question - abstracts your question, its options, answers (correctAnswer, userAnswer), and probably has a mark() method. Degree of granularity is left to you.
QuestionBank - abstracts retrieval of question from whatever storage mechanism you use and holds your Question objects, preferably in a Collection. It should keep a counter, to know the current point in the quiz and test for the end of the quiz, and have getter methods; nextQuestion() and previousQuestion().

You retrieve Question from the QuestionBank and have it displayed on your GUI. You can have a QuestionScreen (JPanel), which holds the current question and has a displayQuestion(Question q) method which is used to update your JLabels as appropriate - use a JLabel to display your question and not a Canvas. When a user clicks one of your JRadioButton, you set the corresponding option in the userAnswer in the current question. your next and previous navigation button would then be used to change questions. NB. you can even use the previous button to display the user's answer if the question has already been attempted.

At the end of the quiz, all you need to do is to loop through all the Question in the QuestionBank, calling the mark() method and totaling the correct answers.

You get the drift?
Re: A Problem With Java Gui Design by Shimao(m): 6:11am On Sep 03, 2010
By the way, the QuestionBank should be a singleton.
Re: A Problem With Java Gui Design by gozzilla(m): 10:32am On Sep 03, 2010
@Danyl when you need help with you code, the best thing to do is post it. With the code anyone wanting to help will have a good idea of how much will be helping, and how much will be "confusing". So post the code or at least some of it.
Re: A Problem With Java Gui Design by SayoMarvel(m): 4:55pm On Sep 03, 2010
@Shimao Nice one there, I just noticed that my GUI relates too much with the core implemetation of the program which can pose some little problems if I the need arises to change the UI. Anyway, Its because I just provided that solution "on the fly". If I really want to solve that problem, I will make use of a very good object oriented approach. Maybe you should read this thing very well, this guy seems to me like he doesn't know how to build the core engine for this program too (he doesn't just have problem with the GUI). If the guy has the core engine in place, the GUI should not be a problem for someone with considerable experience to have built the engine in the first place (with all due respect). Java2D API should handle all the desired painting of the questions on the screen (you can even go the extra mile of animating the questions e.g letting them fly-in, dissolve/fade away the answered questions, make the questions text vibrate when the user is taking too long to answer them, etc), and Java Swing UI ToolKit (in collaboration with AWT, awt.event, swing.event) should provide all the components that will make up the GUI and also the event handling mechanisms.

@gozzilla True one there. We're all guessing on this guys ability thats why we are addressing the problem from diffrent levels. If the guy should upload his code, we'll know how far he has gone and how we can help.

Nice day guys, gat to work,
Re: A Problem With Java Gui Design by chukxy: 11:13am On Sep 04, 2010
@SayoMarvel , you are partly right as it took me  more time than I anticipated to do it. However, the time It took me to get it done
would be compensated by its flexibility, easy to maintain by novice in programming. Please, I would want to compare your method to better the method I  adopted, if you don't mind. The screen shoot is attached.

Re: A Problem With Java Gui Design by SayoMarvel(m): 11:06pm On Sep 04, 2010
@chukxy I already knew that your approach will already be time consuming to develop and also very prone to errors since invalid data can easily be fed into your questions bank (since there's no regex employed to control the input into your excel file). Believe me, your program can misbehave (crash) unexpectedly. Imagine an answer field (row in your excel file) expected to hold int or char values, someone that helps you to build questions can mistakenly type 'aa' instead of 'a' and such errors may be difficult to track down in an environment of hundreds (maybe thousands) of questions. Also, it may not be so interesting typing in a cell (in your excel spreadsheet) then press the forward key to focus the next cell and so on until you get to the end of the last cell where you press the down key and then a series of left keys to reposition the focus to the first cell on the next line (imagine doing this for 500 questions!). Also your program will have to import a lot of classes to handle the DB interaction (class loading and unloading can reduce program performance even if it will be minimal). The best way you can guarantee your data input is to make your own QuestionBuilder class (shouldn't be more that 3KB).
Anyway, I respect your aproach so much that why we are on this forum (to share ideas), i never thought of it. Its so professional. I only did not employ it because I so much believe in Java IO since writeObject() and readObject() will save and read entire objects (without you worrying about how the JVM does that) compared to you reading the fields(in your excel spreadsheet) and passing them to the constructor to have your objects built.
I must say that I really do not like your GUI. Try to download some cool look and feels (e.g Substance, Alloy, Quaqua -- for Macintosh look etc).
Re: A Problem With Java Gui Design by chukxy: 11:33pm On Sep 04, 2010
@SayoMarvel , Thanks for the response and observation;however, I do not agree with you in terms of my program being prone to errors as I equally developed a class to take care of batch upload of data to any database and the class would ensure that only  correct data is uploaded to database.The only issue I had with my program was it took me more time than I anticipated, so I want to get a better way to do it with less time. In terms of GUI, I did not pay attention to that as I concentrated on the functionality. Anyway thanks.
Re: A Problem With Java Gui Design by SayoMarvel(m): 4:24pm On Sep 07, 2010
@chukxy Okay bro. You didn't mention that you created a class that ensures proper input. I feel very cool with your approach. I think we can get glued (U and I). I have prospective projects/contracts and I'm going to need a software development gang or army or team (whatever you like calling it). I will like to know more of your programming life. my email is marvelindaclub@yahoo.com and my Yahoo! Messenger ID is marvelsayo. Speeding up development time is all about employing the shortest approach (not at the expence of quality) and also creating your own personal library. mail me so that we can have a better discussion. Using objects with standard Java IO object streams is the fastest way to do this work. Another approach is to use human editable text files (with appropriate delimiters) but this approach is error-prone and a little more involved to program. Don't forget to mail me. Have a nice day.
Re: A Problem With Java Gui Design by SayoMarvel(m): 6:13am On Sep 09, 2010
@CHUKXY believe me when I say I've carefully thought this thing out and I came out with a wonderful architecture more sophisticated than what I explained previously. I'm still waiting for your mail.
Re: A Problem With Java Gui Design by chukxy: 9:16am On Sep 09, 2010
Hi SayoMarvel ,I just sent you mail. I am delighted to hear that you got an improved architecture. I can't wait to tap from it.
Hope to hear from you soon.Have a nice day!
Re: A Problem With Java Gui Design by sunnyben: 11:00am On Sep 10, 2010
@Danyl

I quite understand you. The solution for that is CardLayout. It's very easy to use. I will also suggest you use NetBeans for your design, it's what i've been using for my Java GUI's.
OR

Just put your data(questions and answers to be displayed) in an array. You then create an Event listener for next button which increments and so changes the display.

All the best
Re: A Problem With Java Gui Design by Danyl(m): 7:18pm On Sep 20, 2010
thanks @all i really appreciate your timely response i will get back to you guys as soon as am over with the stuff am on.
Re: A Problem With Java Gui Design by naijaswag1: 1:09pm On Sep 25, 2010
One

Re: A Problem With Java Gui Design by naijaswag1: 1:13pm On Sep 25, 2010
@ Danyl I am a newbie like you who is struggling to stand om my feet.When I saw this post some three day ago I decided to try it out.Since I started reading some months ago,this my first try at writing a complete program that does something.I used the implementation suggested by chukxy because I have never connected to database,query it and update it.It took me two days to complete but it has opened my eyes to a new level of programming.

@chukxy please can you become my mentor.I have been studying java and as at this point I have acquired knowledge of the basics.There is virtually no question that I can not answer but I need to start putting one or two together under someone's kind direction.I realized that learning how to program is one thing,putting this down to building working program is another.That is where I am stuck right now.If I can have someone tell me do this,put this here and there i will be beta and start earning a living from programming because right now I am unemployed.There are existed programs which you have written before,you can give it to me as assignment and even others you can imagine to help me get started.
Re: A Problem With Java Gui Design by chukxy: 9:35am On Sep 26, 2010
Hi naija_swag , that was a great and good chase, more power to your elbow. Yes, it is always good to put your programming skill to practice by doing some projects that come on your way, by so doing, you would develop confidence to tackle more challenging tasks or projects.
Concerning being your mentor,all I will say is that we can always learn together.You can reach me through my website www.softwebkey.com.
Do have a wonderful weekend!

(1) (Reply)

Friendzone / My Python Guessing Game... Pythonistas Please Come In / Java And Oracle Which Is Best?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 70
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.