Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually you should provide a piece of code so we can understand it better. But generally i think i could understand what you mean (if i'm not wrong).</p> <p>Basically you need the layout manager on the second form. here is a code sample, i hope it can generally answer your need.</p> <p>Code for the first form</p> <pre><code>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class FirstForm extends JFrame { JButton fbtn = new JButton("Show F2"); JSlider fslider = new JSlider(1, 10); SecondForm fsecond = new SecondForm(); public FirstForm(){ setSize(200,200); getContentPane().setLayout(new BorderLayout()); getContentPane().add(fbtn,BorderLayout.NORTH); getContentPane().add(fslider,BorderLayout.CENTER); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); fbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { fsecond.setVisible(true); } }); fslider.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { fsecond.setBtnCount(fslider.getValue()); } }); } public static void main(String[] args) { // TODO code application logic here java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new FirstForm().setVisible(true); } }); } } </code></pre> <p>Code for the second form</p> <pre><code>import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; public class SecondForm extends JFrame { Vector fbtns = new Vector(); int fshowbtncount=1; GridBagConstraints gbc = new GridBagConstraints(); public SecondForm(){ setSize(200, 200); setLocation(201, 0); getContentPane().setLayout(new GridBagLayout()); fbtns.add(new JButton("Button ".concat(String.valueOf(fbtns.size()+1)))); invalidateForm(); } private void invalidateForm(){ gbc.fill = GridBagConstraints.VERTICAL; for (int i=0; i&lt;fbtns.size(); i++) { getContentPane().remove((Component)fbtns.get(i)); } int y=0; for (int i=0; i&lt;fshowbtncount; i++) { gbc.gridx=0; gbc.gridy=y++; getContentPane().add((Component)fbtns.get(i),gbc); } pack(); invalidate(); } public void setBtnCount(int cnt){ if (cnt&gt;=0 &amp;&amp; cnt!=fshowbtncount) { fshowbtncount = cnt; while (fbtns.size()&lt;cnt) { fbtns.add(new JButton("Button ".concat(String.valueOf(fbtns.size()+1)))); } invalidateForm(); } } } </code></pre> <p>Hope this can help you out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload