Issue with updating JButtons on switching between JTabbedPane tabs

98 views Asked by At

So the problem I'm having is probably easiest shown in pictures:

This first picture is a shot of the game board enter image description here

Each of those squares - 'Plots' - are JButtons.

On clicking one of the JButtons above the Plot display - i.e, 'Buy Produce' - the associated action will happen:

enter image description here

So far, so good.

But, on switching to either of the other Tabs - 'Commodities' or 'Help' - and switching back to the 'Game' tab, I've only ever been able to have this happen:

enter image description here

My thinking is that the steps I'm taking to update the Plot display after Buying Produce should work fine to update the display after switching Tabs, but it doesn't.

Here is the code I use to update the display when Buying Produce:

buyProduce.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            try {
                buyProduce();
                plot_panel.removeAll();
                createPlots();
                labelStore.get("Cash_Label").setText(game.roundedWorth());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BankruptException e) {
                JOptionPane.showMessageDialog(frame,
                        "Bankrupt after attempt to purchase Produce");
            }
        }
    });

I've tried doing this with a ChangeListener on the TabbedPane, only to have the Plot display go 'blank' like in the 3rd picture.

tabbedPane.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent arg0) {
            plot_panel.removeAll();
            createPlots();
        }

    });

However, the display is only 'blank' like this until I perform - through one of the buttons above the display - an action that updates the display.

(If, for example, when the display is 'blank' I 'Buy Produce', everything appears as it should.)

Can anybody here tell, from what I've posted, what is going wrong and where?

I'm happy to post more code if needed, but thought I should try and keep this as neat as possible.

Thanks in advance, Doug.

EDIT: I didn't want to waste people times with a poor MCVE, so here - http://1drv.ms/1Hx5sA1 - is a link to a compressed folder with all the stuff in it.

If people don't want to trawl through that, then I'll try and switch to a JTable.

1

There are 1 answers

1
javapadawan On

When removing from plot_panel and re-creating, call

plot_panel.updateUI();