Posted by Darryl Burke on November 9, 2008
Anyone using ButonGroup for the first time has scoured the API for a method that returns the selected Button. Alas, getSelection() returns a ButtonModel, not a button. And as the MVC pattern of Swing allows a visual component – a View – to share its Model with other visual components, ButtonModel rather obviously doesn’t provide a method to get the button from its model.
SelectButtonGroup attempts to address the shortcomings of ButtonGroup by providing additional methods that shorten user code and make programs using the class easy to write and maintain. The class also offers property change support on the same lines as visual components.
Read the rest of this entry »
Posted in Extensions, Swing | 4 Comments »
Posted by Rob Camick on November 8, 2008
There may be times when you want to capture output from your program and display it for the user. This is generally done by creating a console. Using Swing it is not too difficult to create a simple console using a JTextArea or JTextPane. Our message console will be able to display output written to System.out and System.err.
Read the rest of this entry »
Posted in Classes, Swing | 42 Comments »
Posted by Rob Camick on November 7, 2008
There may be times when you want to temporarily disable your frame. The easiest way to do this is to simply use setEnabled(false) on the frame. There are two things I don’t really like using this approach:
- there is no visual indication the frame is disabled
- the frame beeps at you when you click on it
Maybe you also require a different solution?
Read the rest of this entry »
Posted in Classes, Swing | 9 Comments »
Posted by Rob Camick on November 6, 2008
A layout manager has two main functions:
- determine the preferred size of the container
- layout the components in the container based on the layout rules
The FlowLayout is a strange animal. It does both of these functions. The preferred size of the container assumes all components will be laid out in a single row. The layout code will wrap components to the next row when the maximum width of the container is encountered. However, the problem is that the functions don’t talk to one another. When the components are wrapped to a new row, the preferred size doesn’t change so you never see the components on the extra row.
Read the rest of this entry »
Posted in Classes, Swing | 124 Comments »
Posted by Rob Camick on November 5, 2008
A JTable is usually displayed in a JScrollPane. This allows the table to scroll vertically or horizontally as required. Horizontal scrolling causes all columns in the table to be scrolled. In some cases you may want to prevent the leading column(s) in the table from scrolling.
Read the rest of this entry »
Posted in Classes, Swing | 48 Comments »
Posted by Rob Camick on November 4, 2008
The default Tab Action in a JTable causes focus to move to the next cell. When the end of line is reached focus move to the first cell of the next line. When the end of the table is reached focus moves to the top of the table. There may be times when you want to change this default behaviour.
Read the rest of this entry »
Posted in Classes, Swing | 4 Comments »
Posted by Rob Camick on November 3, 2008
Swing components use Actions to provide basic functionality for the component. Of course these Actions can never provide all the functionality you desire and there may be times when you need to customize the default behaviour of an Action.
Read the rest of this entry »
Posted in Classes, Swing | 2 Comments »
Posted by Rob Camick on November 2, 2008
Layout managers are a great feature of Java. I am not a big fan of layout managers that use multiple constraints to achieve a layout. This entry will propose a simple layout manager that will use a single constraint, yet provide flexibility. It was designed around the notion that you want a container that will use all the space available to it. As a component is added to the container you specify the relative amount of space that component requires. You could divide your container 50/50, or like most cars today you could provide a 60/40 split.
Read the rest of this entry »
Posted in Classes, Swing | 15 Comments »
Posted by Rob Camick on November 1, 2008
One usage for a CardLayout is to create a wizzard type application. That is a simple dialog that uses multiple panels and control is transferred to those panels by using Previous/Next buttons. In an application like this you would like the previous button to be disabled when displaying the first panel and the next button to be disabled when displaying the last panel.
Read the rest of this entry »
Posted in Extensions, Swing | 2 Comments »
Posted by Rob Camick on October 31, 2008
The CardLayout can be a flexible layout manager to use. Although I think it has two related drawbacks:
- focus is not placed on the Card as it is displayed
- there is no API to determine which Card is currently being displayed
If you could somehow solve the second problem then you could implement the first.
Read the rest of this entry »
Posted in Extensions, Swing | 6 Comments »