0

I have a JTable which uses a JButton for one of the headers on a column.

Without an ActionListener the button seems to function normally, I can see it visually click. However when I add in an ActionListener which should pop up a JOptionPane the application stops redrawing itself, stops responding to any actions, and doesn't draw the option pane.

public class ButtonHeaderRenderer extends JButton implements TableCellRenderer, ActionListener
{
    int pushedColumn;

    public ButtonHeaderRenderer(Icon image, JTableHeader header, ActionListener actionListener)
    {
        pushedColumn = -1;
        setIcon(image);
        setForeground(header.getForeground());
        setBackground(header.getBackground());
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        setMargin(new Insets(0, 0, 0, 0));
        addActionListener(this);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        setText((value == null) ? "" : value.toString());
        boolean isPressed = (column == pushedColumn);
        getModel().setPressed(isPressed);
        getModel().setArmed(isPressed);
        return this;
    }

    public void setPressedColumn(int col)
    {
        this.pushedColumn = col;
    }

    public void actionPerformed(ActionEvent e)
    {
        JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
    }
}

Does anyone know what might be causing the issue?

edit: It seems that creating a new thread in the actionPerformed method and creating the JOptionPane in that works. This seems like a hack however, I've used JOptionPane in other places and it works fine without starting a new thread.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lithium
  • 5,102
  • 2
  • 22
  • 34
  • Had you tried running your ActionListener without TableCellRenderer, since sometimes that gives some issues. I am not an expert, but try testing that, that might can narrow your search down. Regards – nIcE cOw Jan 23 '12 at 19:38
  • The ActionListener works fine when it is not called from the button in the table header. I have buttons in table cells and they are able to use the same listener correctly. – Lithium Jan 23 '12 at 20:00
  • Have you taken a threaddump to see what is blocking your application ? For example by using jstack or jconsole – Robin Jan 23 '12 at 21:58
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 24 '12 at 02:08
  • an actionListener on a rendering component doesn't make sense: they are just passive stamps, never part of the hierarchy. If you assume otherwise, strange things are certain to happen ... simply don't. – kleopatra Jan 24 '12 at 10:29
  • @Lithium : Glad something worked for you. Regards – nIcE cOw Jan 25 '12 at 19:31

1 Answers1

1

Try to call the JOptionPane inside SwingUtilities.invokeLater()

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    nononono .. the base problem is a mis-use of the renderer (nobody wants to see an optionPane on every header repaint, invoked or not ;-) – kleopatra Jan 24 '12 at 10:31