How to TDD a JFrame? - java

I'm working on a new project, where I want to display some data on the screen. I set myself to using TDD which is new for me, but I love the idea and get along quite OK so far.
I set up a JFrame, add a Textarea and put text there, but how can I properly test this? Or is this wrong thinking in the TDD context on my side? I want to be sure (in the TDD way), that the data gets displayed correctly! The creation of the text that gets displayed is properly covered with tests, but the displaying is not.
Here is a completely simplified example:
public class MyTextDisplay {
public static void main(String[] args) {
JFrame my_frame = new JFrame("DisplaySomeText");
my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(5, 20);
textArea.setEditable(false);
my_frame.add(textArea);
my_frame.setVisible(true);
//this would be in a separate method
textArea.append("Hello World");
}
}

TDD requires you to think about things a little bit differently. You first determine what you are going to test, and how you are going to test it before you actually write any code for the solution.
For GUI's, this can become quite tricky, and, in all honesty, your GUI should never contain any logic that could be in a separate layer. For example, the value that is displayed should come from an object that has nothing to do with the GUI, but that can be tested individually. That allows you to develop the main business logic (model and controller) separate from the display (view). This is the MVC pattern. Test Driven Development simply means that you test what you can before you write code, and, as you add more code, more tests will start to pass.
I'd rather focus on my design and ensure that whatever is generating the text value works as expected. The GUI should be "dumb" and only focus on displaying or retrieving values, with little, if any concern for if the displayed values are indeed correct.
As a GUI is notoriously difficult to test with automated tools (properly test), I'd avoid that as much as possible and decouple my GUI from my actual application as much as I can. Then you can test the GUI once, to make sure it displays what it's supposed to, and focus on the business logic without having to do continuous tests on the GUI as you are not touching that code.

Related

Same component on multiple JFrame instances

I'm working on a game project. I created a GUI class and I want to create an instance of it for each player of the game. So each player will have one screen of the game.
Some information will be displayed on a specific player screen and some will be common. To display common informations, I created a static JLabel in which I put an icon. But I have a problem, the icon only appears on one of the screens.
Why does this happen?
Short answer: you can't. A basic rule of the Swing library is that a component will only be displayed in one and only one container, usually the last container that the component was added to.
The longer answer: you don't want to. You want to structure your program in such a way that the program's model, its logical underpinnings, is well separated from its view, the GUI itself. So what you want to do is have each GUI element share the same model, and use this information to create its own unique view. This has advantages as well, since if a player wants to display elements in a different perspective or point of view, they can do this without affecting what the other players see.
Side note: note that a single ImageIcon can be shared and viewed in multiple separate locations of your GUI since it is not a Swing component, but the JLabel can't be viewed in multiple locations.
Side note 2: Be sure to read: The Use of Multiple JFrames: Good or Bad Practice?, as this contains information pertinent to your program structure.
Side note 3, regarding:
To display common informations, I created a static JLabel...
I would avoid using static fields if at all possible. While this is fine to do in small simple toy programs, it does not scale well since the more static fields you use, the greater the risk of increasing the coupling of your code, and thus increasing its (cyclomatic) complexity, and thus increasing its risk of having hard to cure bugs. Follow good OOP practices by hiding your information, increasing your class cohesion and reducing code coupling, and you'll be a happy coder with cleaner, easier to debug and enhance programs.

What is the functional difference between creating a Java frame inside the main class and creating a Java Frame inside a constructor? [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI.
I'm just wondering whether it is good practice to use multiple JFrame windows?
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 modal dialog 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.:
CardLayout (short demo.). 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.
JInternalFrame/JDesktopPane typically used for an MDI.
JTabbedPane for groups of components.
JSplitPane 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.
JLayeredPane far many well ..layered components.
JToolBar 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 JList (simple example below).
As nodes in a JTree.
Nested layouts.
But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have JDialog or JOptionPane instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.
Many images
In 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 ImageViewer.
A single row JList. As seen in this answer. 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).
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... get this... 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 (SDI) vs single frame/multiple documents per frame (MDI) 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 resolution
SDI (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):
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 bug which means that your main window can become 'always on top' without you telling it. Upgrade to version 7 to fix this
Make an jInternalFrame into main frame and make it invisible. Then you can use it for further events.
jInternalFrame.setSize(300,150);
jInternalFrame.setVisible(true);
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 ( or with more spare time) than us have already think through the Desktop and JInternalFrame pattern, so I would recommend to use it.
Bad practice definitely. One reason is that it is not very 'user-friendly' for the fact that every JFrame shows a new taskbar icon. Controlling multiple JFrames will have you ripping your hair out.
Personally, I would use ONE JFrame for your kind of application. Methods of displaying multiple things is up to you, there are many. Canvases, JInternalFrame, CardLayout, even JPanels possibly.
Multiple JFrame objects = Pain, trouble, and problems.
I think using multiple Jframes is not a good idea.
Instead we can use JPanels more than one or more JPanel in the same JFrame.
Also we can switch between this JPanels. So it gives us freedom to display more than on thing in the JFrame.
For each JPanel we can design different things and all this JPanel can be displayed on the single JFrameone at a time.
To switch between this JPanels use JMenuBar with JMenuItems for each JPanelor 'JButtonfor eachJPanel`.
More than one JFrame is not a good practice, but there is nothing wrong if we want more than one JFrame.
But its better to change one JFrame for our different needs rather than having multiple JFrames.
If the frames are going to be the same size, why not create the frame and pass the frame then as a reference to it instead.
When you have passed the frame you can then decide how to populate it. It would be like having a method for calculating the average of a set of figures. Would you create the method over and over again?
It is not a good practice but even though you wish to use it you can use the singleton pattern as its good. I have used the singleton patterns in most of my project its good.

Java Observable/Observer vs just passing references

I'm trying to get a grasp on a couple concepts. Lets imagine you have a JFrame and in that JFrame there are two panels, we'll say left_half and right_half. If I click a button in right_half, I want something to change in left_half. The issue is that the right half doesn't know the left half exists. So, you could tell the Frame, but technically neither panel knows the Frame exists either, right? The Frame can change the panels, but panels can't change the Frame, or so it seems to me.
So, I pass a reference to the Frame into the panels. Now the right_half can call Frame.setVariable(data) and the Frame can, from that same method, say left_half.setStuff(data). That just seems wrong to me and I have been looking for a way to do it without passing references up and down the hierarchy.
Next, someone says "That's why Observable exists!!" Cool, I think to myself. I then found many confusing examples of how to use Observable that didn't help at all. Finally I see this one, which makes sense.
http://www.javaquizplayer.com/blogposts/blogpost7.html
However, it has this: "observable.addObserver(mainWindow);" mainWindow is the equivalent of Frame in my example above, and it had to pass a reference to attach the Obserable to! So even with Observable, I have to pass references down the hierarchy?
It just seems wrong. If it's not wrong, that's fine... I can do it this way. However, my question once all that back-story is finished is simply this: how are you supposed to pass data between two panels? I'm okay with abstract answers if they're in plain language, I'm okay with code samples if they're short and easy to follow. I'm not a pro Java coder, I can't just search through 29 API pages and 1400 lines of code and just suddenly understand how it works... yet. I'll get there.
The observable pattern (in the form of event listeners) is OK and is good practice. You see, even though RightPanel knows someone might be listening to all its events, it does not know who is listening. Well, technically, it could go through all its listeners and use reflection to find out who they are; now, that would be bad practice.
As it is, RightPanel knows someone might be listening to it, and that's all. Components are always aware someone might be listening, since the whole Swing is based on it. What matters is that RightPanel compiles without LeftPanel (or frame), and is completely decoupled from it, except via the listener interface. Not only cool, but standard.
As a side note, your class structure does not have to mirror your nesting panel hierarchy. Depending on what you are doing, it may be entirely fine to control behavior of both left and right panels from inner classes within Frame. (Personally, I'd use a JPanel, not a JFrame, since it allows for more flexibility, but this can be easily refactored). Otherwise, you risk splitting your View/Controller into too many closely coupled classes, and that would violate encapsulation and cause a lot of boilerplate code. Normally, I don't code big fat classes and try to refactor them into smaller ones; Swing is usually an exception. Better one big fat class than a maze of densely coupled classes. Unless of course you have a reusable component or some piece of functionality that can be clearly and intuitively decoupled; not just a couple of buttons or checkboxes that have no meaning in and of themselves. The simple fact that some subcomponents are situated within a certain panel in a component tree should not be a major factor IMHO.
For that matter, I usually don't nest JPanels; I use MigLayout and make all components into siblings. Matter of taste! I'd just encourage you to check out MiGLayout first and see whether you like it.

Best practise for GUI Components ActionListener?

I did a big GUI Based App and I have now many many Action stuff around there...
I have different Listeneres, like ActionListener, KeyAdapters, ...
Everything should run threaded, so my GUI isnt freezing when do long time operations. So im using SwingWorker in every ActionListener...
Currently its working like this: I have my JComponents, bound on one single ActionListener. In this Listener I decide what to do, based on the actionCommand(). The I call a method, which contains my SwingWorker and the Action which should be performed.
Ii dont like this concept anymore, because my class is getting longer and longer and i dont have an overview about all the functionallity. So I decided to do it another way...
I thought Factory Methods would be a great thing, but here is my problem with them: I need sometimes data from my GUI, eg: when pressing JButton x, what is in JTextField y and so on...
so what is the best practise for this? Should I just give my Factory a instance of my complete GUI? Or a ArrayList of Components? Another problem is that need to change values from my GUI, eg: press button x and then filter a JTable... how should i do that?
doing it like this = myFactory.process(this); isnt really that what i want...
Take a look at JGoodies Binding: it emphasizes the use of PM (PresentationModel) where all the GUI state is stored (and bound to the actual GUI components).
Every View has an associated PM that makes the link with the domain model. PM can live without the View (but the reverse is not true).
PM should not have any GUI-related dependency (so that it is unit testable without the GUI), hence no reference to a JTextField, JButton...
However, PM normally includes Actions that are attached to buttons from the View. Actions are not actual GUI components (although they belong to javax.swing package.
I said "normally" because some Actions may need to display a message box, open a new window... This kind of actions should then be put in another class. Note that Karsten Lentzsch (JGoodies author) doesn't talk about this case in his presentations (this is my own way of dealing with this case).

How to refactor Netbeans generated GUI code?

I had created a GUI in Netbeans through Netbeans Swing GUI creator. So I just dragged and dropped the Swing Components from the "palette" window and all the code was generated by netbeans.
Now the code is too long to maintain (approx. 10,000 lines). So some experts on SO suggested me to refactor my code.
I can refactor the code that was generated by me but I don't know how to refactor the code generated by the Netbeans as It doesn't allow editing in its generated code.
Any suggestions?
10.000 lines of code sounds like you have everything in that single class.
Start by splitting your source into Model, View and Control (MVC).
You might also be able to extract some JPanels into separate classes. One way to do this is to create a new JPanel (new file), and cut/paste your compoments from one main panel into that new JPanel. Save and compile your new panel.
Then go back to your main frame, select Beans -> Choose Bean from your Palette and choose the newly created class (com.example.YourPanel for example).
Make sure to have a backup of your application before you try this.
Well - if the code is generated, I don't see any advantages in refactoring it as long as the tool which generated it can handle it. The tool (meaning the designer in this case) will "destroy" all your refactoring work as soon as it updates the code.
However, you should split your Control/Window/... into multiple controls - then the code will automatically get shorter and you will be able to maintain your UI more easily.
As a conclusion: Do not refactor the generated code but do refactor your control.
Handcode the GUI code with layoutmanagers.
Using GUI builder tools, makes it nearly impossible to refactor GUI code. I have to use these idiotic Intellij Swing GUI designer forms. I now cannot even rename my packages in Eclipse because it wont be updated in the forms.XML file.
Stay away from GUI builders. If you want to build really complex, maintainable GUIs then do it by hand by using GridBagLayout and all the rest.
If you have to use netbeans, because of project limitations (e.g the rest of the team is, or requirements say to) then use Matisse to break up the huge form into smaller panels, each of which the designer can edit. You can do that by creating a new form, and cutting and pasting panels from the big form into the new form.
But at the same time, make sure all the business logic is moved out of the UI classes.
If you do not have to use matisse / netbeans, you can open the project in Eclipse, and edit the forms using WindowBuilder, it will do it in real java code instead of the uneditable form, so you can then chop and edit it to your heart's content.
You can extract the application logic into a separate subclass. Then, directly use the subclass. I succeeded with the following method.
Members defined by us that are relevant to the application logic moved to the newly created subclass.
Components access modifier made "protected" (they are "private" by
default). To do so: Right click -> Properties -> Code (tab) -> Set
"Variable modifier" to "protected"
Event handling methods moved to the subclass - When you are adding events to a component using properties pane it changes initComponents() function by adding the relevant code like in the following code sample. Here definition of btnNum6ActionPerformed() is added to the class with an empty body. Unfortunately btnNum6ActionPerformed() is private and no way to change the access modifier using NetBeans IDE. Hence, they cannot be overridden. To get rid of this, you can define another intermediary function and call it inside btnNum6ActionPerformed(). It is better to make the base class and its intermediary event handling functions abstract.
btnNum6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNum6ActionPerformed(evt);//Definition of this method is added too
}
});

Categories