Feb 08

By default the column widths of a JTable are automatically set for you. To fix the width of a specified column you need to use the TableColumn for the column (there’s one for each column).

    TableColumn column = table.getColumnModel().getColumn(columnIndex);

written by objects \\ tags: , , , ,

Feb 02

You can use a MouseListener to listen for clicks on your JTable. It will report both clicks and double clicks, reporting the click count via MouseEvent.getClickCount(). However when you double click on an non-editable column in your table your MouseListener unfortunately receives two event. Firstly it receives a single click event for the first click, then it receives a second double click event for the second click. Not very useful if you want to perform different actions depending on the number of clicks.

To get around this problem we can use a Swing Timer. We’ll start the Timer after the first click to wait for the second click. If the second click happens before the timer stops then its a double click, otherwise treat it as a single click.

The following class provides an implementation of this behaviour.

public class MouseClickListener extends MouseAdapter {

    private Timer timer = new Timer(300, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // timer has gone off, so treat as a single click


	public void mouseClicked(MouseEvent e) {
		// Check if timer is running 
		// to know if there was an earlier click
		if (timer.isRunning()) {
			// There was an earlier click so we'll treat it as a double click
        } else {
        	// (Re)start the timer and wait for 2nd click


	protected void singleClick() {
		System.out.println("single click");
	protected void doubleClick() {
		System.out.println("double click");


written by objects \\ tags: , , , ,

Nov 20

Often you want to do something when the user closes your dialog window. To achieve this you can use a WindowListener the same way you would for a JFrame.

dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
         // dialog is closing, you can perform your action here

written by objects \\ tags: , ,