Java Tips Weblog

  • Blog Stats

    • 1,301,107 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.

Auguest 11, 2014 – Row Number Table

  • The RowNumberTable was not updating correctly when a new row was inserted into the main JTable.

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

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 | 3 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 | 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 | 4 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 | 9 Comments »

Tile Icon

Posted by Darryl Burke on April 22, 2012

The usual way to repeat, or tile a background image across and down a GUI component is to write nested loops in a paintComponent(…) override.  TileIcon extends ImageIcon to provide this functionality and can fill the background of a component that can display an Icon.

Read the rest of this entry »

Posted in Extensions, Swing | 2 Comments »

Thumbnail Icon

Posted by Darryl Burke on April 14, 2012

In the last two posts, we presented StretchIcon and ShrinkIcon, both used for scaling images to fit a component whose size is determined by the size and layout of its container.  ThumbnailIcon extends ShrinkIcon to reduce an image when needed to fit within a defined size, padding and centering it horizontally or vertically as required.  Like ShrinkIcon, this class does not magnify the image.

Read the rest of this entry »

Posted in Extensions, Swing | Leave a Comment »

Shrink Icon

Posted by Darryl Burke on April 8, 2012

When writing about StretchIcon, published earlier, I mentioned that the image chosen for a StretchIcon should be large enough that it does not get magnified in fitting it to the component, since drawing an image larger than its natural size can lead to pixelation.  ShrinkIcon extends StretchIcon to ensure such pixelation doesn’t happen.

Read the rest of this entry »

Posted in Extensions, Swing | Leave a Comment »

Stretch Icon

Posted by Darryl Burke on March 31, 2012

An implementation of the Icon interface reports its size via the two methods getIconWidth() and getIconHeight().  It’s normal to expect that the Icon’s paintIcon(Component, Graphics, int, int) method will respect these bounds.  If I had designed Swing’s interaction with Icons, I would have made sure of that by setting a clip to the Graphics passed to paintIcon(…).

Luckily for me, I didn’t design Swing, as that would have made StretchIcon impossible.

Read the rest of this entry »

Posted in Extensions, Swing | 6 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 | 3 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 97 other followers