Java Tips Weblog

  • Blog Stats

    • 1,809,906 hits
  • Categories

  • Archives

Archive for the ‘Classes’ Category

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 | 8 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 »

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 | 7 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 | 11 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 | 11 Comments »

Drag Layout

Posted by Rob Camick on October 23, 2011

We have been taught that you should always use a layout manager to layout components on a container. But what about the times when you want the ability to randomly position components on a panel by dragging them with a mouse? Should we use a null layout or can we still use a layout manager?
Read the rest of this entry »

Posted in Classes, Swing | 5 Comments »

Table Column Manager

Posted by Rob Camick on May 8, 2011

When using a JTable the user has some control over the view of the columns in the table. The columns can be resized and they can be reordered. One feature that is not supported is the ability to hide and reshow a column.
Read the rest of this entry »

Posted in Classes, Swing | 21 Comments »