Java Tips Weblog

  • Blog Stats

    • 2,569,065 hits
  • Categories

  • Archives

Recent Updates

Posted by Darryl Burke on December 29, 2008

Thanks for the various comments. We’ll try to keep you updated here when changes are made to the code so you can get a new copy.

June 21, 2016 – Single Root File Chooser

  • The “Up” button was not being disabled correctly when a relative File was used as a parameter for the class.

If you are interested in receiving an RSS feed whenever this posting is updated click on the “Read the rest of this entry” link below for more information on how to do this.

Read the rest of this entry »

Posted in Uncategorized | 42 Comments »

Local Date Combo

Posted by Darryl Burke on April 22, 2015

LocalDateCombo is a Swing date picker for selecting a java.time.LocalDate from a drop-down MonthView.  As with MonthView, the no-arg constructor creates a LocalDateCombo set to the current date, with no upper or lower limits.  Additionally, this uses a default format corresponding to java.time.format.FormatStyle.MEDIUM

Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Year Month Spinner

Posted by Darryl Burke on April 13, 2015

YearMonthSpinner is a composite of two Temporal spinners, one for the month and the other for the year, used to select a java.time.YearMonth.  For ease of use, the spinner buttons are located towards the outer edges of the composite component.

Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Temporal Spinners

Posted by Darryl Burke on April 9, 2015

JSpinner was designed to work with Dates, Lists or Numbers.  With the introduction of a new Date/Time API in Java 8, wouldn’t it be great to be able to create date or time spinners that use the new classes?  SpinnerTemporalModel and SpinnerTemporalEditor make that possible.

Read the rest of this entry »

Posted in Classes, Swing | 10 Comments »

Swing and Java 8

Posted by Darryl Burke on April 5, 2015

Java 7 brought us JLayer (which I haven’t played with), Generics in JComboBox and JList, and a lonely collections-oriented JList#getSelectedValuesList().  Java 8, on the other hand, did not introduce any new Swing API.  However, enhanced language features can certainly make Swing code less verbose and more maintainable, and the new Date/Time API makes it easier than ever before to create interactive Swing components for date and/or time selection.

Read the rest of this entry »

Posted in Swing, Tips | 5 Comments »

Month View

Posted by Darryl Burke on April 2, 2015

MonthView is a composite of a YearMonthSpinner and a tabular calendar display of the selected month.  The table can be navigated with the arrow keys, and the month can be changed by the mouse scroll wheel. Optionally, a link for setting the value to the current date can be displayed.  Clicking on a displayed date sets it as the selected value.

Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Combo Box With Custom Renderer

Posted by Rob Camick on November 17, 2013

There may be times when you have a custom Object that you want to add to a JComboBox. Adding the Object to the ComboBoxModel is no problem, however getting the appropriate text to display in the combo box can be a problem. This is because the default renderer for the combo box will simply invoke the toString() method of the Object added to the combo box. Rarely will the toString() value be the text that you actually want displayed in the combo box. To solve this problem, the standard response you will find in the forums is to create a custom renderer.

Read the rest of this entry »

Posted in Classes, Swing | Leave a Comment »

Motion Using the Keyboard

Posted by Rob Camick on June 9, 2013

A simple form of animation is to have a component move across the screen. This is generally done by using one of the four arrow keys to control the direction of movement. There are two common approaches to listen for the pressing of an arrow key (or any key for that matter). One is to use a KeyListener and the other is to use Key Bindings. Lets take a look at these two approaches.

Read the rest of this entry »

Posted in Swing, Tips | 5 Comments »

Playing With Shapes

Posted by Rob Camick on May 13, 2013

Custom painting, as we all know, should be done by overriding the paintComponent(…) method of a Swing component. Chances are that when you first start to experiment with custom painting you will use one or more of the following Graphics painting methods:

  • Graphics.fillRect(…) – to paint a square or rectangle
  • Graphics.fillOval(…) – to paint a circle or oval
  • Graphics.fillPolygon(…) – to paint an arbitrary hape like a triangle, hexagon or octagon

These painting methods are easy to use, but not very flexible:

  • the painting code is programmed in the paintComponent() method of your component so it can’t be shared or reused by other components
  • you need to know which Graphics method to invoke to do the painting
  • the location where the painting is done is specified as a parameter of the Graphics method
  • the Color needs to be specified

Read the rest of this entry »

Posted in Classes, Swing | 9 Comments »

Smart Scrolling

Posted by Rob Camick on March 3, 2013

There may be times when you have a component in a JScrollPane and data is dynamically added to this component. Normally, you would like the viewport to scroll to the bottom automatically as new data is added so you can see the most recent data. However, there may also be times when you are viewing data somewhere else in the viewport and you don’t want the scrolling to happen automatically. Supporting both of these of these requirement would require a scrollpane to support smart scrolling.

Read the rest of this entry »

Posted in Classes, Swing | 19 Comments »

Combo Box With Hidden Data

Posted by Rob Camick on February 18, 2013

Typically a JComboBox is used to display a list of strings. There may be times when you need the items in the combo box to contain extra data so that you can do additional processing once an item is selected. What is the best way to do this?
Read the rest of this entry »

Posted in Classes, Swing | 13 Comments »