Tips for Swing UI Layout
1. Avoid using JEditorPane and
Most Swing components in Java 6 have a
meaningful baseline. However, some seem to have no correct baseline (for no
apparent good reason). JEditorPane and JTextPane are examples of such
components. This means that it is impossible to have these components aligned
on their baseline. Thus, when using those components, you don’t know exactly
how they will be aligned with other components in the same row; this will
depend on the LayoutManager you use.
DesignGridLayout aligns these components
on the top of their “box”, which is not very beautiful, but nothing
better is possible until those components are able to return a decent baseline
Hence the best is to not use such
components at all if possible. You should prefer JTextArea when it fits your
setColumns() on JTextField and JTextArea:
Most LayoutManagers use (or can use)
components preferred size to perform an optimal layout.
In Swing, many components are able to
determine the optimal preferred width based on their content. For instance,
JLabel, JButton, JList, JTable belong to this category and for these you don’t
need to explicitly set the preferred width.
For JTextField and JTextArea, however,
this is not the case, by calling setColumns() (or using the constructor that
takes a columns argument), you make sure these components will have the right
use TitledBorder to separate groups of information:
Developerd use Swing TitledBorder around
several sub-panels in order to separate groups of information inside a form.
The major problem with this approach is that every sub-panel has its own
LayoutManager, and LayoutManagers are disconnected from each other, hence you
are likely to have bad alignment between sub-panels.
setPreferredScrollableViewportSize() on JTable:
Unfortunately, JTable does not have a
setVisibleRowCount() method as in JList. Hence, you need to find another way to
set a preferred size (in terms of number of rows) to the JTable but avoid that
it displays “partial” rows. The default JTable preferred height shows
20 rows (independently of the actual number of rows in the model) which is
generally more than what you would want to show.
You can use :
static public void setTableHeight(JTable
table, int rows)
int width = table.getPreferredSize().width;
int height = rows * table.getRowHeight();
5. Use Thread.sleep
instead of Thread.yield
Use Thread.sleep(1) instead of
Thread.yield() as Thread.yield can, according to java spec, simply return
control to the caller without doing anything; and even Thread.sleep(0) can
6. Size of Thread Pool
You should design the size of the thread
pool such that the average number of runnable threads is not significantly
greater than the number of processors. You should also keep tasks reasonable
small and independent of one another.
7. Put all
components that can vary in height in a JScrollPane
Some components allow you to display
several “lines” of information (JTable, JList, JTextArea…) In most
cases, it is impossible to know exactly how many lines will be displayed. Hence
by putting those components in a JScrollPane you make sure the user will be
able to vertically scroll to see all available data.
In addition, some components (such
JTextArea) were specifically coded to be embedded in a JScrollPane, if you
don’t, they will not look good (e.g. no border).