The Artima Developer Community
Sponsored Link

Java Answers Forum
A question on GUI

6 replies on 1 page. Most recent reply: Mar 18, 2002 4:53 AM by Ali Ahmed

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Ali Ahmed

Posts: 5
Nickname: destiny
Registered: Mar, 2002

A question on GUI Posted: Mar 16, 2002 2:00 PM
Reply to this message Reply
Advertisement
As this is my first hands on experience on GUI, I am stuck.
working on A 15 multiple choice questions(labels) each with 3 possible answers(radio buttons)

1.I am trying to create checkboxGroups but dont know how to specify the number of checkboxes i.e want several in rows and columns.(question(1)/answers(3))*15) so i need 45 checkboxes for answers and 15 labels for questions next to it.

2. once that is done, I need the checkboxes to refer the text(answers) from the textfile should be possible i think.

any help will be appreciated

this is what i have so far, just a start

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class TheQuiz {
//private static Vector Quiz;

public static void main(String[] args) {

Frame f=new QuestionFrame("The Quiz");
f.setSize(1000,500);
f.setVisible(true);
}
}
// Frame class
class QuestionFrame extends Frame {
public QuestionFrame(String title) {
// Set title, layout, and background color
super(title);
setLayout(new FlowLayout());
setBackground(Color.blue);

Label Question= new Label("Question");
CheckboxGroup answerGroup=new CheckboxGroup();
Checkbox ans1box=new Checkbox("Answer1" , answerGroup, true);
Checkbox ans2box=new Checkbox("Answer2" , answerGroup,false);
Checkbox ans3box=new Checkbox("Answer3" , answerGroup,false);

}


// Read quiz from text file
//readQuiz();
public static void readQuiz () {

try {
FileReader fileIn= new FileReader ("Quiz.txt");
BufferedReader in =new BufferedReader(fileIn);

while (true) {
String line=in.readLine();
if (line == null)
break;
System.out.println(line);
}
Quiz = (Vector) in.readObject();
in.close();
}
catch (Exception e) {
System.out.println(" Quiz.txt does not exist or " + "cannot be read\n");
Quiz = new Vector();
}
}


}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: A question on GUI Posted: Mar 16, 2002 6:14 PM
Reply to this message Reply
Why do you want to use checkBoxes instead of JRadioButtons?

Also, do you really want all the questions to appear on one form? Wouldn't it be nicer to have a form that shows one question at a time? It would have previous and next buttons for navigating from one question to another.

Also, why do you use Vector instead of ArrayList? And even more critically (even though it is commented out), why is your list of Quiz questions static? Speaking of that, I would guess you would want to create a class called Question (or whatever you like) and then have a question List -- containing instances of that class.

Finally, if you want to do a readObject(), then you'll need an ObjectInputStream, not a BufferedReader.

Ali Ahmed

Posts: 5
Nickname: destiny
Registered: Mar, 2002

Re: A question on GUI Posted: Mar 17, 2002 1:03 PM
Reply to this message Reply
Firstly thanks for your feedback Matt, appreciated.

now answering your questions.
Is JradioButton different from normal radio buttons?
I think i am using the normal radio buttons?.

All questions on one form because as this is my first GUI experience I am making things easy for myself and hence all on one page otherwise i dont mind questions at a time.

Why Vectors, not ArrayList? because had enough of arrays in my life and want to experience LinkedLists or vectors and hence i chose the later.

Why the list of quiz question STATIC? as you have guessed I want to create a Question class and then have a question list containing instance of that class.

and finally you are right The BufferedReader was a mistake, it should have been the ObjectInputStream.

thanks

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: A question on GUI Posted: Mar 17, 2002 2:18 PM
Reply to this message Reply
> Is JradioButton different from normal radio buttons?
> I think i am using the normal radio buttons?.

You appear to be using Checkboxes. A group of check boxes allows you to check zero to all available options, where as a group of radio buttons lets you choose just one option. I guess it depends upon what kind of multiple choice questions you have; some allow multiple answers, some only allow one answer. ...Ah ha, I've just looked at the documentation for CheckBoxGroup and it appears that Sun is at fault! It has the behavior of a radio group. So they are just confusing the nomenclature. This appears to be a mistake in AWT that was corrected in Swing (which has JCheckBox and JRadioButton, instead of using checkboxes as radio buttons). Oh well. I would still use JRadioButton where I want radio buttons and JCheckBox where I want checkboxes, just to keep the code and concepts clear.

> All questions on one form because as this is my first
> GUI experience I am making things easy for myself and
> hence all on one page otherwise i dont mind questions
> at a time.

I think it will be more difficult to try and lay out so many controls on one pane. You will probably even have to make it scrollable. Having a simple layout with controls that are updated to reflect the current question is much easier to implement.

> Why Vectors, not ArrayList? because had enough of
> arrays in my life and want to experience LinkedLists
> or vectors and hence i chose the later.

An ArrayList and a Vector both implement the List interface (Vector was retrofitted to it), which is what you'd most likely be using. Also, if you are using the List interface and are really enamored of linked lists, then you would instantiate a LinkedList object and use that. The point is that the Vector has been kept for backward compatibility, but it is recommended that you use List (with one of its implementations, such as ArrayList or LinkedList) for new development. Finally, for your application, ArrayList is probably better than LinkedList because you will probably not be adding and removing Question items in random locations in the list; you will more likely fill the list once, then navigate through it. For such a small collection, it hardly matters, though, since the performance difference would be imperceptible.

> Why the list of quiz question STATIC? as you have
> guessed I want to create a Question class and then
> have a question list containing instance of that
> class.

That doesn't answer the question of why it is static. Maybe you are not clear on the meaning of static. Look a few days back for a post where I explained that and posted a little sample program to illustrate the difference between static and instance variables. A static variable is one single variable that is shared for every instance of the class. I think if you have a Quiz class, that each instance of it should be able to maintain its own list of Questions, therefore the collection should not be static.

Ali Ahmed

Posts: 5
Nickname: destiny
Registered: Mar, 2002

Re: A question on GUI Posted: Mar 17, 2002 4:36 PM
Reply to this message Reply
>CheckboxGroupVsJradioButton

After looking at the documentation on JradioButton it looks like there is no diference between the two and you confirmed that too but after all this was not the "BIG" issue but was good to know.

>I think it will be more difficult to try and lay out >so many controls on one pane. You will probably even >have to make it scrollable. Having a simple layout >with controls that are updated to reflect the current >question is much easier to implement.

I think you are right there!

>Vector & LinkedList Vs ArrayList
after considering this,I think vector would be out of question since the size of the task is fixed here.
lol I am not really enamoured of LinkedLists just thought of using that for a change.

My algorithm here is to take a question list and generate a LinkedList of questions; then build a class whose constructor takes a LinkedList( of questions) and generate the window. ( so I believe LinkedList makes sense here)
then to use a loop for reading the list of questions and generate a row at a time. each row representing a question and its 3 possible answers BUT i believe the tricky part will be with the listeners, a question like HOW CAN I GENERATE ALL THESE LISTENERS FOR THE BUTTONS? so that if user clicks the button i can check if it corresponds to the correct answer.
but few thoughts in my mind are
Firstly the 3 listeners for (the answers) to a question can be the same, then if i can pass that question object as an argument for the listener constructor then I could check the label of the button against that question object. DOES THESE MAKE SENSE HERE?

finally according to my understanding a static variable is one that belong to a class, but not to any particular instance of that class so really it should have been instance variable.


Thanks again of your feedbacks

Enthusiasts we are but experts maybe not!!!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: A question on GUI Posted: Mar 17, 2002 4:48 PM
Reply to this message Reply
As far as your listeners go, I would not attach any to the radios/checkboxes. I would instead have a "next" or "OK" button. This is because someone may click on a choice and then change their mind. If the thing reacts to your first choice, then it is a pain to change your answer. Actually, my first sentence isn't exactly true; I would have a shared listener for the radios that enables the "next" button (which would be disabled whenever a new question is presented -- this way you ensure that all questions are answered).

In general, it is not a good idea to use the text of any control for purposes of logic. Localization aside, you will want your questions and answers to be in a text file and you will want to be able to change their text. Instead, keep an array of (in your case) of three radio buttons and just register the correct answer by its offset (in other words, your Question object could have two wrong answers and one right answer; just keep track of where the right answer is positioned).

Ali Ahmed

Posts: 5
Nickname: destiny
Registered: Mar, 2002

Re: A question on GUI Posted: Mar 18, 2002 4:53 AM
Reply to this message Reply
The whole point here is that one can't change his/her mind once clicks on the choice.

>In general, it is not a good idea to use the text of >any control for purposes of logic. Localization aside, >you will want your questions and answers to be in a >text file and you will want to be able to change their >text. Instead, keep an array of (in your case) of >three radio buttons and just register the correct >answer by its offset (in other words, your Question >object could have two wrong answers and one right >answer; just keep track of where the right answer is >positioned).

And that will be HOW?

Flat View: This topic has 6 replies on 1 page
Topic: URGENT!!!     .......Opening a web site from JEditorPane Previous Topic   Next Topic Topic: Adding name values to a SRC URL string

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use