Multiple Choice Java Swing
MotivationI took a course in University where the exam consisted mostly of multiple choice questions. Somewhere along the term, I managed to acquire a question bank consisting of 1000+ questions. The question bank covered a large fraction of the content covered in this course and was therefore a very good study resource. Unfortunately, the format of this question bank was a collection of MS Word documents. This program was created to fix that. MultiChoiceMultiChoice is a Java swing program used to spit out multiple choice questions saved as JSON files. It contains two major components:.
Java AWT Choice. The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits Component class. AWT Choice Class Declaration. AWT Multiple Choice Questions and Answers AWT questions and answers with explanation for interview, competitive examination and entrance test. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.
A parser that converts the original MS Word document to a JSON. A Java swing GUI used to display the questionsCompilation and reading of the JSON files used Google's GSON library.
MS Word to JSON ParserThis is an unimportant but crucial part of the program. There was a lot of 'hacking' done in this component since the parser required specifically for this one use case (see Motivation). No long-term engineering decisions were considered for this part of the project.The MS Word document was very poorly formatted and only had a certain degree of consistently. To simplify the parsing process, the content of the MS Word document was copied and pasted into a text editor. This allowed me to avoid using any libraries to read from the MS Word document directly.
This also means that any figures and tables that were included in the document was also lost.Luckily, there were at most roughly ten figures and tables per document. As such, these were recovered manually by skimming through the document and saving them as images. In order to recover the correct references for the questions, I simply analyzed question text and added an appropriate reference each time I see the phrase 'Figure x.x' or 'Table x.x' where x is a number. Java Swing GUIThis is the main component of this project. For each question, the GUI displays. Question Text.
Relevant Table or Figure. Multiple Choice Answers. Options including:. 50/50. Crazy Automatic Answer Mode. Image Feedback. RandomThe 50/50 option removes half the of the multiple choice answers (excluding the correct answer, of course).
The Crazy Automatic Answer Mode grays out all the incorrect answer to allow the user to become familiar with only the correct answer (on the event that the question bank reflect exactly what's going to be on the exam). The Image Feedback option displays varying images as a feedback to the user depending on whether you answered the question correctly. Lastly, the Random option is used to randomize the order of the questions.
I'm just wondering whether it is good practice to use multiple JFrames?Bad (bad, bad) practice. User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems. A nightmare to code and maintain:. A offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed.
Multiple frames do not. A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior.There are any number of ways of displaying many elements in one GUI, e.g.:. (short ). Good for:. Showing wizard like dialogs. Displaying list, tree etc. Selections for items that have an associated component.
Flipping between no component and visible component. typically used for an. for groups of components. A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing. far many well.layered components.
typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need.
As mentioned above, will minimize/restore according to the parent doing so. As items in a (simple example below). As nodes in a.But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have or instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs. Many imagesIn this case where the multiple elements are images, it would be better to use either of the following instead:. A single JLabel (centered in a scroll pane) to display whichever image the user is interested in at that moment.
As seen in. A single row JList. The 'single row' part of that only works if they are all the same dimensions.
Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).
@ryvantage 'Should (Excel) be MDI?' Good question. I feel it should be offered to the user both ways (certainly not only in MDI form). For example: 1) I currently use TextPad, and by configuration at my choice, it opens separate instances, that each offer multiple documents shown in a list. 2) Although I'll typically use FF in tabbed mode, occasionally I drag a tab off to a new window.
The common factor in the examples is user choice. Deliver the app. 'however the user wants it'.–Aug 1 '13 at 9:49. @AndrewThompson You've just countered your own argument with your last comment. In your main answer you say this is bad practice and should never be done, but in your comment above you say you sometimes like SDI and we should offer our users the choice. Surely, this is exactly what user417896 was saying above. This is one of my biggest pet hates about my fellow developers.
The fact that many of them become religiously fanatical about so-called 'best-practices'. We wouldn't have the innovative UIs we have today if we all stuck to 'best-practices' and didn't think outside the square.–Aug 15 '13 at 21:47.
The multiple JFrame approach has been something I've implemented since I began programming Swing apps. For the most part, I did it in the beginning because I didn't know any better. However, as I matured in my experience and knowledge as a developer and as began to read and absorb the opinions of so many more experienced Java devs online, I made an attempt to shift away from the multiple JFrame approach (both in current projects and future projects) only to be met with.
Resistance from my clients! As I began implementing modal dialogs to control 'child' windows and JInternalFrames for separate components, my clients began to complain! I was quite surprised, as I was doing what I thought was best-practice!
But, as they say, 'A happy wife is a happy life.' Same goes for your clients. Of course, I am a contractor so my end-users have direct access to me, the developer, which is obviously not a common scenario.So, I'm going to explain the benefits of the multiple JFrame approach, as well as myth-bust some of the cons that others have presented.
Ultimate flexibility in layout - By allowing separate JFrames, you give your end-user the ability to spread out and control what's on his/her screen. The concept feels 'open' and non-constricting. You lose this when you go towards one big JFrame and a bunch of JInternalFrames. Works well for very modularized applications - In my case, most of my applications have 3 - 5 big 'modules' that really have nothing to do with each other whatsoever. For instance, one module might be a sales dashboard and one might be an accounting dashboard. They don't talk to each other or anything.
However, the executive might want to open both and them being separate frames on the taskbar makes his life easier. Makes it easy for end-users to reference outside material - Once, I had this situation: My app had a 'data viewer,' from which you could click 'Add New' and it would open a data entry screen.
Initially, both were JFrames. However, I wanted the data entry screen to be a JDialog whose parent was the data viewer. I made the change, and immediately I received a call from an end-user who relied heavily on the fact that he could minimize or close the viewer and keep the editor open while he referenced another part of the program (or a website, I don't remember). He's not on a multi-monitor, so he needed the entry dialog to be first and something else to be second, with the data viewer completely hidden. This was impossible with a JDialog and certainly would've been impossible with a JInternalFrame as well. I begrudgingly changed it back to being separate JFrames for his sanity, but it taught me an important lesson.
Myth: Hard to code - This is not true in my experience. I don't see why it would be any easier to create a JInternalFrame than a JFrame. In fact, in my experience, JInternalFrames offer much less flexibility.
I have developed a systematic way of handling the opening & closing of JFrames in my apps that really works well. I control the frame almost completely from within the frame's code itself; the creation of the new frame, SwingWorkers that control the retrieval of data on background threads and the GUI code on EDT, restoring/bringing to front the frame if the user tries to open it twice, etc. All you need to open my JFrames is call a public static method open and the open method, combined with a windowClosing event handles the rest (is the frame already open? Is it not open, but loading?
Etc.) I made this approach a template so it's not difficult to implement for each frame. Myth/Unproven: Resource Heavy - I'd like to see some facts behind this speculative statement.
Although, perhaps, you could say a JFrame needs more space than a JInternalFrame, even if you open up 100 JFrames, how many more resources would you really be consuming? If your concern is memory leaks because of resources: calling dispose frees all resources used by the frame for garbage collection (and, again I say, a JInternalFrame should invoke exactly the same concern).I've written a lot and I feel like I could write more. Anyways, I hope I don't get down-voted simply because it's an unpopular opinion. The question is clearly a valuable one and I hope I've provided a valuable answer, even if it isn't the common opinion.A great example of multiple frames/single document per frame vs single frame/multiple documents per frame is Microsoft Excel. Some of MDI benefits:.
it is possible to have a few windows in non rectangular shape - so they don't hide desktop or other window from another process (e.g. Web browser). it is possible to open a window from another process over one Excel window while writing in second Excel window - with MDI, trying to write in one of internal windows will give focus to the entire Excel window, hence hiding window from another process.
it is possible to have different documents on different screens, which is especially useful when screens do not have the same resolutionSDI (Single-Document Interface, i.e., every window can only have a single document):MDI (Multiple-Document Interface, i.e., every window can have multiple documents). While I understand your argument, I would still prefer to have everything in one JFrame and a big parent JTabbedPane; but with the possibility to open a second window (or even more) where the layout can be different, offering hence a hybrid behaviour where SDI lovers are happy and MDI ones as well. In all cases, I always considered JInternalFrame as a horrible pattern which gives you all the inconvenients of both worlds. The flexibility they offer just sucks and they eat away a lot of precious screen space for no real purposes.–Sep 30 '14 at 21:03. I'd like to counter the 'not user friendly' argument with an example that I have just been involved with.In our application we have a main window where the users run various 'programs' as separate tabs. As much as possible we have tried to keep our application to this single window.One of the 'programs' they run presents a list of reports that have been generated by the system, and the user can click on an icon on each line to pop open a report viewer dialog. This viewer is showing the equivalent of the portrait/landscape A4 page(s) of the report, so the users like this window to be quite big, almost filling their screens.A few months ago we started getting requests from our customers to make these report viewer windows modeless, so that they could have multiple reports open at the same time.For some time I resisted this request as I did not think this was a good solution.
However, my mind was changed when I found out how the users were getting around this 'deficiency' of our system.They were opening a viewer, using the 'Save As' facility to save the report as a PDF to a specific directory, using Acrobat Reader to open the PDF file, and then they would do the same with the next report. They would have multiple Acrobat Readers running with the various report outputs that they wanted to look at.So I relented and made the viewer modeless. This means that each viewer has a task-bar icon.When the latest version was released to them last week, the overwhelming response from them is that they LOVE it.
It's been one of our most popular recent enhancements to the system.So you go ahead and tell your users that what they want is bad, but ultimately it won't do you any favours.SOME NOTES:. It seems to be best practice to use JDialog's for these modeless windows.
Use the constructors that use the new ModalityType rather than the boolean modal argument. This is what gives these dialogs the task-bar icon. For modeless dialogs, pass a null parent to the constructor, but locate them relative to their 'parent' window.
Version 6 of Java on Windows has a which means that your main window can become 'always on top' without you telling it. Upgrade to version 7 to fix this. It's been a while since the last time i touch swing but in general is a bad practice to do this. Some of the main disadvantages that comes to mind:.It's more expensive: you will have to allocate way more resources to draw a JFrame that other kind of window container, such as Dialog or JInternalFrame.Not user friendly: It is not easy to navigate into a bunch of JFrame stuck together, it will look like your application is a set of applications inconsistent and poorly design.It's easy to use JInternalFrame This is kind of retorical, now it's way easier and other people smarter.