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.
Try to call the JOptionPane inside SwingUtilities.invokeLater()