java - unable add multiple jpanel(each jpanel is a separate timer run concurrently) to jframe using arraylist only initial jpanel is getting addeed? -


this code in jframe class name"timer1.java" , jpanel class name "timer_ui.java"

   arraylist<timer_ui> mul_panels = new arraylist<timer_ui>();     public void jbutton2actionperformed(java.awt.event.actionevent evt){     timer_ui d_timer = new timer_ui();            mul_panels.add(d_timer);            timer_ui dis_timer = mul_panels.get(i);            i++;            dis_timer.setbackground(color.white);            dis_timer.setbounds(34, 110, 434, 178);            add(dis_timer);            height = height + 230;            setsize(new dimension(523,height));     } 

the execution of application

execution of application. 1 jpanel object add on click again there no timer added frame:

https://i.stack.imgur.com/2sxuc.jpg

don't use setbounds() set size of components.

swing designed used layout managers.

if want add more timer panels horizontally need use appropriate layout manager. maybe boxlayout, or gridlayout.

start reading section swing tutorial on layout manager more information , working examples

so basic logic might like:

jpanel toppanel = new jpanel( new borderlayout()); toppanel.add(label, borderlayout.page_start); toppanel.add(button1, borderlayout.line_start); toppanel.add(button2, borderlayout.line_end);  box timerpanel = new box.createverticalbox();  frame.add(toppanel, borderlayout.page_start); frame.add(timerpanel, borderlayout.center); 

now when want create new timer actionlistner code like:

timer_ui dis_timer = mul_panels.get(i); dis_timer.setmaximumsize( dis_timer.getpreferredsize() ); timerpanel.add( dis_timer ); timerpanel.revalidate(); timerpanel.repaint(); 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -