Hot answers tagged

14

You question is three-fold: 1. How many classes are too many? While there are some guides on some metrics like how long should a method be or how many parameters can a method have, there are no such metrics as to how many classes should a system have as a maximum. IMHO it's not so much the amount of classes that create complexity as it is the fact of ...


7

When the run() method of that new thread finishes, aka the thread finished it's job. Does it delete itself? Or does it keep existing in the memory? It mostly deletes itself. The native thread (OS resource) is typically1 destroyed, and the thread stack memory segments (OS resource) are typically deleted. The thread's thread-locals map, the runnable and ...


7

You seem to have the dependency between your Model and your View the wrong way around. A Model does not know about the existence of a View, at most the model knows that some other component in the system might want to know about changes to the model. In the MVC pattern, either the Controller informs the View that it should update itself, or the View asks ...


7

There are actually multiple ways to reduce the size of .jar files. If you really need to reduce the size of .jar file without dropping some content, I would recommend trying the following two automated tools. Compression Perhaps the easiest way is to use a different tool to create the .jar file than the standard jar distributed with the Java SDK. KZIP is ...


6

You're asking the wrong question. It's not "better to have everything crammed into the main class", nor is it "better to have a lot of classes". When we distance ourselves from such attempts at generating rigid rules to cover all cases, we arrive at a much better approach: abide by widely-accepted design principles, such as those embodied by SOLID, and we ...


5

Consider using MigLayout. It's a comprehensive layout manager that I haven't had any issues with. To get an initial taste of what it's capable of and how easy it is to use, look at the sample application linked to from the home page. Make sure you try resizing the window to see how the user interface is able to adapt to the size of the screen. The core ...


3

Using SwingUtilities.invokeLater() is not merely advantageous, it's essential, but to understand why you need to understand Swing's threading model. Updates to the GUI, via Swing, must occur on the Event Dispatch Thread (EDT), and code that does anything else (e.g. accessing some resource such as a database) should use one or more other threads. ...


3

What does it mean to say that, class JButton or class JRadioButton is-a class Container? The reason why all Swing components are derived from java.awt.Container is mostly for practical reasons which are internal to the implementation of Swing. AWT and Swing usually will not be mixed. But internally, a Swing component might realize itself using more than one ...


3

Swing listeners are the typical example for inner classes, possibly even the reason that language feature was added. So go ahead and implement it like that. From the design perspective, the action listener behaviour is really part of the form.


3

Java language "philosophy" is WORA: Java is ...intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Explanation for default given in Swing tutorial (Available Look and Feels) looks consistent with above: CrossPlatformLookAndFeel — this is ...


3

I did a lot of Swing programming in the late 90's, but I don't do very much nowadays, so I don't know if it's still an issue or not. It used to be the case that if you used a native look and feel, you'd occasionally run into layout issues when you ran it on another platform, due to slightly different control proportions and such. Using the default metal ...


3

Between your two options having the view know about your "logic" class would be the better method. This would allow you to create or modify your view without having to change your logic classes. If you put your view inside of your "logic" classes it dramatically limits your flexibility. As people mentioned in the comments read up on the MVC pattern as ...


3

Your design doesn't really violate the Observer pattern, because the pattern does not specify how subscribers get registered with the publisher. But you are violating the Single Responsibility Principle for those middle panels, because they are given the added responsibility of helping in getting the message tray subscribed. A better design would be to use ...


2

I refrain from including validation in my data model objects. I make them very simple POJOs that don't have any knowledge about the content of the values, business logic that drives them, and the state of the application. I've create verification classes that are separate from both the controller and the data model. These are used during the import of data,...


2

There's functional reactive programming. The idea is to turn time-varying entities into first-class objects. Suppose we're dealing with a mouse cursor, and we want to draw an icon 10 pixels to the right of the cursor. Imagine that there was a type TimeVarying<T> that represents a value that changes over time. TimeVarying always magically contains the ...


2

Swing developers should know the basic layouts and master at least one powerful layout. MigLayout is one such tool. The problem then becomes, "When should I use a powerful layout?" The answer is "When no simpler one will do." Use simple layouts when possible. Nest simple layouts for more complex effects. Isolate complex layouts, such as GroupLayout and ...


2

In general, SOLID principles are usually met (See Snowman's answer) But since you mentioned the Android SDK, I will go a little into detail on how SOLID principles coming too short in this particular API. Single responsibility principle. The View class is responsible for positioning drawing event processing handling a huge load of other callbacks. So, ...


2

You can use a ListModel to return placeholder values until things get loaded. On startup you only load and cache the first few dozen. then when the value is requested to fire off a task using SwingWorker running in a background thread. public class BackgroundLoader extends SwingWorker<Void, Person>{ private LinkedBlockingQueue<ID> queue = ...


2

There are multiple correct ways to implement/use the MVC pattern, in part depending on the technology you use and personal preferences. The most important aspect of the MVC pattern is that all the user-interaction related code is confined to the Controller and/or View portions. In other words, it should be possible to create a completely different UI (for ...


1

The main reason to have classes in any code is to make that code easier to follow. If you feel that by adding classes, the code becomes clearer and more descriptive and conceptually meaningful...then you're on the right track. But if you feel that by adding classes, the code becomes more muddled, sprawling, or obtuse...then don't do it. There is no simple ...


1

When it comes to design, it is usually a good idea to have an idea of design patterns. This will help you better visualize the best way to structure your application. It is worth noting though, that you should choose the design patterns depending on what you need to do, and not try to force your design to conform to a specific design pattern. At the very ...


1

At design level you have a couple of options: Every now and then redraw the entire world Redraw the entire world when it has changed Every now and then check whether or not the world has changed. If so redraw the entire world. If something changes, redraw the affected parts of the world Every now and then check for changes and only redraw the affected ...


1

Is there a less convoluted way to allow data to flow between components? Yes. It's called a model. In a model / view / controller pattern (MVC), an instance of the model is passed to the view so that the values in the model can be read by the view. The values in the model are updated by controller classes. In a Java Swing application, you generally have ...


1

Classical Model-View-Controller question. MyClass is a model. It has the domain data and implements domain logic. SWING components (JTextField, JTable) are a view. They know how to show data to the user and how to accept user input, but know nothing about your domain model. MyWindowClass is a controller. It needs to mediate your model and view - e.g. set ...


1

You have a domain model that encapsulates validation and business logic, and that is no bad thing. Consider what 'hexagonal' architecture would imply for your design. Specifically, it would recommend that you design an interface to your persistence layer, allowing different persistence mechanisms to be plugged in as adapters. When performing the business ...


1

I design my models so they don't easily get themselves into an invalid state to begin with. Then when my Interface (UI, or service), attempts to put it into such a state, an error is thrown which I can then send back to the offending source. Most of the time this means that my models have a method called isValid(), that checks itself for things that can't ...


1

https://stackoverflow.com/questions/15040989/issues-creating-a-very-accurate-swing-timer Shows swing timer being off dead accurate time by 10ms to 200ms using it to trigger frames of animation won't work well. Using it to trigger the start of an animation should be OK. The way swing timers work is one thread manages the timeouts and the actual event ...


Only top voted, non community-wiki answers of a minimum length are eligible