Tag Archives: Java

Display a JList with a Background Image

Ever wanted to use an image as a background of your JList component? Unfortunately the Swing library does not provide a simple method to set a image as a background. In order to do so, you are force to do some custom painting inside the paintComponent method. In the case of a JList, our custom painting has to be performed inside a custom implementation of a ListCellRenderer. Check out the video below to see it in action.

READ MORE »

68 Vector-based Java Icons

I was searching for some icons to use in my Swing GUI program and came across the Tango Desktop Project. It is an icon library for free and open source software. Instead of just taking the icon images from the library, I took it a step further. I use the SVG version of the icon and created an Icon java class that uses Java 2D to draw the similar icons. The biggest benefit is you can create an icon of any size without losing image quality. You are free to download and use the icons in your GUI programs.

READ MORE »

JTable Mastery: convertColumnIndexToModel

Study the code below. There is something wrong with it! Can you guess what it is?

public class JTableViewToModelColumn {
	public static void main(String[] args) {
		JFrame f = new JFrame("View To Model Column Example");

		final DefaultTableModel model = new DefaultTableModel();
		model.setColumnIdentifiers(new String[]{"Col A", "Col B", "Col C"} );
		model.setRowCount(10);
		final JTable table = new JTable(model);

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int col = table.columnAtPoint(e.getPoint());
				System.out.println("You click on column " + model.getColumnName(col));
			}
		});

		f.add(new JScrollPane(table), BorderLayout.CENTER);
		f.pack();
		f.setVisible(true);
	}
}

READ MORE »

How to add a Gradient Background to any JComponent

Almost every “web 2.0″ application have a cool linear gradient background instead of the plain old boring single color solid background. Adding a gradient background to your application is very simple with only a few lines of additional code. In the example program, I am using Java2D and the GradientPaint class to draw the linear gradient background.

Below is the program that I used to create the image above. We override the paintComponent of the JPanel (or any JComponent) and fill the background with a gradient paint. The begin and end colors for the gradient paint are derived from the component’s background color using the brighter and darker variations.

READ MORE »

How to create a Callout Border

Using the power of Java2D, I was able to create more advance and stylish borders. Using a combination of a round rectangle and a triangle shape, I created the callout border shown on the left. This is also very similar to the balloon tips on the Windows OS. Decorate a JLabel with a callout border and you got an awesome tooltip replacement.

Here is how I did it…

READ MORE »

4 Ways to Capture a Screenshot

The ability to create a screenshot programmatically in Java code can be very useful. I personally use it to create my animated transitions.

Here are 4 ways to create a screenshot in Java.

READ MORE »

How to format numbers in a JTable with a NumberCellRenderer

NumberCellRendererDemo

By default, the JTable show the cell values as regular text using a JLabel. To display numbers properly, you are force to pre-format the number as string values before setting them in the table model. There is a better way, by using your own implementation of TableCellRenderer. This will allow you to store the values in their intrinsic form inside the table model.

READ MORE »

Why adopt a Rich Client Platform (RCP)?

Ten years ago, I became a founding member of a company on a quest to build a tool for Fixed Income Analysis. Our goal was to build our own rich client desktop application for performing analysis in a niche market. If we had to do it again today, I would highly recommend NetBeans RCP and save ourselves much of the pain of rolling your own desktop platform. Leveraging NetBeans RCP would have allowed us to focus on delivering more of our core values.

READ MORE »

How to create a custom Swing Border

Dash Border

A dash border in Java Swing

Eventually every Swing developer comes to a point where the borders packaged with Java Swing is considered too plain. Either you will be googling for your next border or you become adventurous and create your own border. This tutorial is for the adventurous. I will teach you how to create your own dashing border. Anyone who done web development is already familiar with the border styled with alternating lines and spaces.

READ MORE »

Easy animated transitions in Java Swing with TimingFrameworks

clockCreating animated transitions use to be a tedious task in Java Swing… until TimingFrameworks came along. TimingFrameworks (https://timingframework.dev.java.net/), written by Chet Haase, is a library for making Java animation and timing-based control easier. There are two good introductory articles on their website worth reading.

Crash course in animation

Below is the code to move a JFrame 100 pixels from left to right in 1 second.

READ MORE »