java - Changing JLabel icon dynamically after already adding to GUI? -


i'm having trouble changing icon deep in gui using swing components. i'm creating chess game fun java , want right side of gui respond when piece taken showing taken piece in grid. problem whenever call seticon() function within jlabel new image , add appropriate jpanel, not update. works when seticon() first time, after added gui, can't change way have been trying to. here screenshots know i'm getting at:

initial state

after

as can tell, pawn has been taken right panel not reflect despite efforts.

i did research , following question similar: relevant question

camickr responded in saying 2 instances of jlabel trying update believe going in case. set of icons on right panel null when setting gui initial state. here code this:

for (int = 0; < 16; i++) {     piece1labels[i] = new jlabel();     piece2labels[i] = new jlabel();     piece1panels[i] = new chessspace(playerdeadpiecetile);     piece2panels[i] = new chessspace(playerdeadpiecetile);     piece1labels[i].setpreferredsize(new dimension(67,66));     piece2labels[i].setpreferredsize(new dimension(67,66));     piece1labels[i].seticon(null);     piece2labels[i].seticon(null);     piece1panels[i].add(piece1labels[i]);     piece2panels[i].add(piece2labels[i]);     player1piecepanel.add(piece1panels[i]);     player2piecepanel.add(piece2panels[i]); } 

and here me trying change 1 of panels after initialization has been called on first panel in piece1panels array of chessspace extend jpanel:

//try change right panel icon after gui setup     piece1labels[0] = new jlabel();     piece1panels[0] = new chessspace(playerdeadpiecetile);     piece1labels[0].setpreferredsize(new dimension(67,66));     piece1labels[0].seticon(new imageicon("c:/users/shasta/workspacejava/chess/images/terranpawn.jpg"));     piece1panels[0].add(piece1labels[0]); 

piece1labels , piece1panels variables of class extending jframe. believe problem i'm updating class variable , not updating instance added gui.

edit: alican ozgoren & hovercraft full of eels pointed out shouldn't have declared new instances of jlabel, redundant.

the following line of code seems want:

//try change right panel icon after gui setup     piece1labels[0].seticon(new imageicon("c:/users/shasta/workspacejava/chess/images/terranpawn.jpg")); 

as noted, set icon of jlabel. 1 more thing -- don't keep reading in icons you're doing here:

piece1labels[0].seticon(new imageicon(          "c:/users/shasta/workspacejava/chess/images/terranpawn.jpg")); 

instead read icons in once @ beginning of program, , store them in variables get:

piece1labels[0].seticon(terranpawnicon); 

i read them in resources not files can later store images in jar file , use them.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -