Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally the <code>add</code> call is sufficient. </p> <p>Note: a <code>BorderLayout</code> can only contain one component in each location. So if you add another component in the <code>NORTH</code> location, your button will not be visible.</p> <p>Second note: by default a <code>JPanel</code> does not have a <code>BorderLayout</code> but a <code>FlowLayout</code>. Have you set a <code>BorderLayout</code> on that specific panel ? Otherwise the <code>BorderLayout#NORTH</code> constraint is incorrect</p> <p>All the <code>validate</code>,<code>revalidate</code>,<code>repaint</code> calls can be removed</p> <p><strong>Edit</strong></p> <p>It seems some sort of validation is needed after all. I was under the impression that Swing should be smart enough to listen for the event when something is added to a <code>Container</code>, and update whatever is necessary (a bit similar to updating a <code>TableModel</code> updates the <code>JTable</code> based on events, without the need to call <code>repaint</code> or the likes on the <code>JTable</code>).</p> <p>However, when trying this in an SSCCE, I came to the following code (different versions, only post the most elaborate version)</p> <ul> <li>without the scroll-pane, the <code>validate</code> calls seem to have no effect. I actually need to call <code>pack</code> again to make the new labels visible (not included in the SSCCE, but removing the scrollpane from the code is trivial)</li> <li><p>with the scroll-pane, the <code>validate</code> call has an effect</p> <pre><code>import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AddLabelsAtRuntime { private int fLabelCounter = 0; private JPanel fLabelPanel; private final JFrame fTestFrame; public AddLabelsAtRuntime() { fLabelPanel = new JPanel( ); BoxLayout boxLayout = new BoxLayout( fLabelPanel, BoxLayout.PAGE_AXIS ); fLabelPanel.setLayout( boxLayout ); fTestFrame = new JFrame( "Dynamically add labels" ); } private JFrame createUI(){ Container contentPane = fTestFrame.getContentPane(); contentPane.setLayout( new BorderLayout() ); JScrollPane scrollPane = new JScrollPane( fLabelPanel ); scrollPane.setPreferredSize( new Dimension( 200, 200 ) ); contentPane.add( scrollPane, BorderLayout.CENTER ); contentPane.add( createButtonPanel(), BorderLayout.SOUTH ); fTestFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); fTestFrame.pack(); return fTestFrame; } private void addLabel(){ fLabelPanel.add( new JLabel( "Label " + ++fLabelCounter ) ); } private JPanel createButtonPanel(){ JPanel buttonPanel = new JPanel( ); BoxLayout boxLayout = new BoxLayout( buttonPanel, BoxLayout.LINE_AXIS ); buttonPanel.setLayout( boxLayout ); JButton validateButton = new JButton( "Add + validate" ); validateButton.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { addLabel(); fLabelPanel.validate(); fTestFrame.validate(); } } ); buttonPanel.add( validateButton ); JButton noValidateButton = new JButton( "Add" ); noValidateButton.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { addLabel(); } } ); buttonPanel.add( noValidateButton ); JButton packButton = new JButton( "Add + pack" ); packButton.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { addLabel(); fTestFrame.pack(); } } ); buttonPanel.add( packButton ); return buttonPanel; } public static void main( String[] args ) { EventQueue.invokeLater( new Runnable() { @Override public void run() { AddLabelsAtRuntime addLabelsAtRuntime = new AddLabelsAtRuntime(); addLabelsAtRuntime.createUI().setVisible( true ); } } ); } } </code></pre></li> </ul>
    singulars
    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. 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.
    3. VO
      singulars
      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