java - How to repaint GUI after a certain element was removed? -


i'm making app user able add button screen or remove (i don't have options implemented yet). so, i'm manually populating for() loop , manually removing 1 of buttons. problem after button has been removed (the removal action in main()), there's blank spot. want able repaint screen after remove 1 of buttons. in example, index 2 (block #3) has been removed, leaving empty space, before... , have no idea how repaint it. i've tried validating or repainting different places in program no success.

here's code (p.s. i'm sure code not efficient way accomplish i'm trying , i'm using setlayout(null), not preferred method, i'm trying learn things , expand on better myself , code):

import java.awt.color; import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.lineborder;  class testapp extends jframe{     jframe frame = new jframe("test program");     arraylist<jbutton> grid = new arraylist<jbutton>();      private int w = 14;     private static int amount = 102;     private static int counter = 0;      //default constructor (sets jframe)     testapp(){          frame.setlayout(null);         frame.setpreferredsize(new dimension(1186, 880));                frame.setdefaultcloseoperation(exit_on_close);         frame.setresizable(false);         paintgrid();         frame.pack();         frame.setlocationrelativeto(null);         frame.setvisible(true);     }      public void newwindow()     {         jframe select_win = new jframe("selected frame");         jpanel select_panel = new jpanel();         select_panel.setpreferredsize(new dimension(600, 800));         select_panel.setbackground(color.orange);         select_win.add(select_panel);         select_win.pack();         select_win.setresizable(false);         select_win.setvisible(true);         select_win.setlocationrelativeto(null);     }      private void paintgrid()     {            for(int = 0, y = 4; < ((amount / w) + (amount % w)); i++, y += 104)         {             for(int j = 0, x = 4; j < w && (counter < amount); j++, x += 84)             {                 addblock(counter, x, y);                 counter++;             }         }     }      //adds block     private void addblock(int index, int x, int y){         int height = 100;         int width = 80;          grid.add(new jbutton("counter: " + (counter + 1)));         (grid.get(index)).addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent e) {                 javax.swing.swingutilities.invokelater(new runnable() {                     @override                     public void run() {                         newwindow();                     }                 });             }         });         (grid.get(index)).setborder(new lineborder(color.black));         (grid.get(index)).setbackground(color.yellow);         (grid.get(index)).setvisible(true);         (grid.get(index)).setbounds(x, y, width, height);          frame.add(grid.get(index));     }        //removes block     private void removeblock(int index){         frame.remove(grid.get(index));         grid.remove(index);         amount--;         counter--;     }        public static void main(string [] args) {         javax.swing.swingutilities.invokelater(new runnable() {             @override             public void run() {                 testapp app = new testapp();                  //testing block removal                 app.removeblock(2);             }         });     } } 

as said, not use nulllayout. fix problem need 2 changes:

change layout flowlayout on constructor, this:

 frame.setlayout(new flowlayout()); 

change setbounds call setpreferredsize:

(grid.get(index)).setpreferredsize(new dimension(width, height)); 

now flowlayout automatically align items , don't need worry anymore.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -