Mitch
Mitch

Reputation: 1039

JLabel in group layout - freeze size

I have a JLabel in a group layout. The JLabel is populated with HTML text, so that it automatically wraps when the text is too long for a single line.

If the JLabel is already showing two lines and I set new text on the JLabel that will also result in two lines, the size of the JLabel temporarily changes and then goes back to the same size as it was before. This temporary change in size causes an unnecessary visible shift in the surround elements.

Is there a way to prevent this temporary change of size, so that the display doesn't shift?

I looked at the min/max/preferred sizes, but before setting the size, the height was the same for all 3, so doesn't seem like setting a maximum would help.

An example is shown below. To run the example 1) Run it. 2) Scroll the list to the very bottom, so that 20 shows in the list. 3) Click the button. The bottom of the list will now show 19, because the list changed size because the label changed size, even though the label need not change size.

    import java.util.Date;

    public class LabelSizeTest extends javax.swing.JFrame {

      public LabelSizeTest() {
        initComponents();
      }

      private void initComponents() {

        lblTest = new javax.swing.JLabel();
        btnTest = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblTest.setText("<html>test asd asdf asdf asdf asdf asdf asdf asdf asdf end");
        lblTest.setName("lblTest"); // NOI18N

        btnTest.setText("Click Me");
        btnTest.setName("btnTest"); // NOI18N
        btnTest.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTestActionPerformed(evt);
          }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
          String[] strings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
          public int getSize() { return strings.length; }
          public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
              .addGroup(layout.createSequentialGroup()
                .addComponent(btnTest)
                .addGap(0, 165, Short.MAX_VALUE))
              .addComponent(jScrollPane1))
            .addContainerGap())
        );
        layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(btnTest)
            .addContainerGap())
        );

        pack();
      }

      private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {                                        
        lblTest.setText(lblTest.getText() + "x");
      }                                       

      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            new LabelSizeTest().setVisible(true);
          }
        });
      }

      private javax.swing.JButton btnTest;
      private javax.swing.JList jList1;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JLabel lblTest;
    }

Upvotes: 2

Views: 978

Answers (1)

Arijit
Arijit

Reputation: 1674

I made two changes in your code:(You can use any one of them) first: The HTML text

<html><body style='width:200 px'>test asd asdf asdf asdf asdf asdf asdf asdf asdf end

Second: Set the setPreferredSize of the Jlabel

    Dimension D = lblTest.getSize();
    lblTest.setPreferredSize(D);

Check the full working code below:(The first change is implemented here, the second change is commented out)

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

import java.util.Date;

public class LabelSizeTest extends javax.swing.JFrame {

    JScrollBar vertical;
  public LabelSizeTest() {
    initComponents();
  }

  private void initComponents() {

    lblTest = new javax.swing.JLabel();
    btnTest = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);




    lblTest.setText("<html><body style='width:200 px'>test asd asdf asdf asdf asdf asdf asdf asdf asdf end");
    lblTest.setName("lblTest"); // NOI18N


    btnTest.setText("Click Me");
    btnTest.setName("btnTest"); // NOI18N
    btnTest.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {

        btnTestActionPerformed(evt);

      }
    });

    jScrollPane1.setName("jScrollPane1"); // NOI18N
    vertical = jScrollPane1.getVerticalScrollBar();

    jList1.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jList1.setName("jList1"); // NOI18N
    jScrollPane1.setViewportView(jList1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(btnTest)
            .addGap(0, 165, Short.MAX_VALUE))
          .addComponent(jScrollPane1))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(lblTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btnTest)
        .addContainerGap())
    );

    pack();



        //Dimension D = lblTest.getSize();
        //System.out.println(D);
        //lblTest.setPreferredSize(D);
  }

  private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {  

    lblTest.setText(lblTest.getText() + "x  dsfsd  sdfsdf  dfgdfg  dfgdfg  dfgdfg  dgfdgf  dfgfg  fgdfgf  fgdg");
    vertical.setValue( vertical.getMaximum());
    pack();
  }                                       

  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new LabelSizeTest().setVisible(true);
      }
    });
  }

  private javax.swing.JButton btnTest;
  private javax.swing.JList jList1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JLabel lblTest;
}

Upvotes: 0

Related Questions