0

My swing label is messing up my layout once the text in the label changes, CODE:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package question3_test;

/**
 *
 * @author jackandjill
 */
public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        title_lbl = new java.awt.Label();
        side1_lbl = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        side1_tf = new javax.swing.JTextField();
        side2_tf = new javax.swing.JTextField();
        side3_tf = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        result_lbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(200, 100));

        jPanel1.setMaximumSize(new java.awt.Dimension(200, 200));

        title_lbl.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        title_lbl.setText("AREA OF TRIANGLE");

        side1_lbl.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        side1_lbl.setLabelFor(side1_tf);
        side1_lbl.setText("Side 1:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setLabelFor(side2_tf);
        jLabel2.setText("Side 2:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setLabelFor(side3_tf);
        jLabel3.setText("Side 3:");

        side1_tf.setColumns(6);
        side1_tf.setMinimumSize(new java.awt.Dimension(60, 20));

        side2_tf.setColumns(6);
        side2_tf.setMinimumSize(new java.awt.Dimension(60, 20));

        side3_tf.setColumns(6);
        side3_tf.setMinimumSize(new java.awt.Dimension(60, 20));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Calculate Area");
        jButton1.setBorder(null);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        result_lbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        result_lbl.setText("jLabel4");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(side1_lbl)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(side1_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(10, 10, 10)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(side3_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                    .addComponent(side2_tf, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addComponent(title_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(30, 30, 30))
            .addComponent(result_lbl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(title_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(side1_lbl)
                    .addComponent(side1_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(side2_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(side3_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(result_lbl)
                .addGap(0, 21, Short.MAX_VALUE))
        );

        title_lbl.getAccessibleContext().setAccessibleName("Area of Triangle");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        result_lbl.setText("This is getting longer,This is getting longer,This is getting longer");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel result_lbl;
    private javax.swing.JLabel side1_lbl;
    private javax.swing.JTextField side1_tf;
    private javax.swing.JTextField side2_tf;
    private javax.swing.JTextField side3_tf;
    private java.awt.Label title_lbl;
    // End of variables declaration
}

So when I run this I get this

enter image description here

Then after I press the Calculate Area button I get this

enter image description here

Had a similar problem with another application, asked a question here And I was told to set a fixed column size did that, here as well but still the same effect.

Community
  • 1
  • 1
noobprogrammer
  • 513
  • 5
  • 14
  • also was just thinking, not applicable to my application, if the label is really indeed that long how to make it to go to the next line? like Multiline kind of thing. – noobprogrammer Oct 15 '12 at 03:03
  • read up on layout managers and be wary of GUI editors. – mre Oct 15 '12 at 03:11
  • I did read up on GUI editors, but since I am here can u kidnly suggest some modifications to the existing code, as I dont have time to rebuild the application from scratch again tks, any quick fix – noobprogrammer Oct 15 '12 at 03:15
  • *"any quick fix"* Hire a consultant (quickly). Voting to close as 'too localized'. – Andrew Thompson Oct 15 '12 at 03:17
  • @AndrewThompson haha wish I could! can I hire you? – noobprogrammer Oct 15 '12 at 03:18
  • I'm available, but I doubt you could afford me. – Andrew Thompson Oct 15 '12 at 03:18
  • I maybe, but if you could show me the picture of the working application first (not the code). – noobprogrammer Oct 15 '12 at 03:19
  • How about I create another panel to hold the dynamic label and ensure that this panel cannot extend the size, so no matter what the controls in the panel the panel wont extend, is that a good idea? – noobprogrammer Oct 15 '12 at 03:23
  • @noobprogrammer I was (largely) being sarcastic since the site discourages people asking questions from trawling for employees/tutors, or consultants trawling for work. Unless a question is asked and answered on the site, it is of no benefit to the SO community. *"How about I create another panel"* I typically make GUIs with a number of 'nested panels' so I can mix different layouts together. I am not sure if it would help here. On other matters, a `JLabel` is intended for single line text but can be coaxed to line-wrap if formatted using HTML/CSS. – Andrew Thompson Oct 15 '12 at 03:29
  • @AndrewThompson same here, so when I asked for a quickfix I was reffering to some other way of doing it, rather than hiring people to do it, which doesn't really improve my knoweledge and defeats the purpose of stackoverflow. But html/css is just not my thing so I will figure out a way of doing it. – noobprogrammer Oct 15 '12 at 03:35
  • 1
    *"I will figure out a way of doing it."* Use a (non-editable) [`JTextArea`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html) instead. It is intended to be multi-line, and the [constructors](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#constructor_summary) offer variants that accept a row-count. – Andrew Thompson Oct 15 '12 at 03:39

3 Answers3

2

Don't try to use GroupLayout, or the GUI editor, to compose your entire application. Instead, use something like this GroupPanel in the CENTER of a BorderLayout. Add a JLabel to the NORTH and ControlPanel to the SOUTH. A JTextArea in the ControlPanel is good for wrapping appended text.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

You can use html to have a multiline label. Just stretch your jlabel width's a bit. In your case try this

mylabel.setText("<html>This is getting longer  <br> This is getting longer <br>This is getting longer</html>");

Heres a screenshot

enter image description here

loQ
  • 2,147
  • 18
  • 21
  • this might also help you http://stackoverflow.com/questions/7666496/newline-character-in-jlabel-settext – loQ Oct 15 '12 at 03:55
1

I do not know what form-building tool you are using, and I don't use one so I can't give you specific instructions. I think this is a textbook case of the dangers of using such a tool without understanding what it does, and though you may (or may not) be able to fix it with my suggestions, I predict you will continually have this and worse problems if you do not understand what the tool is doing underneath the covers.

It appears that all the horizontal structures on the UI screen are somehow right-aligning themselves; even before you click your button, if you widen the UI screen, all your fields move off to the right. I don't know why this is; perhaps you have set the group field(s) to Alignment.TRAILING somewhere.

As a quick fix, you might try moving the result field to a different panel than the rest of the fields, so that even if it gets long, it is the only thing that gets truncated. I don't know how to do that in your GUI builder, and it doesn't really fix the whole problem. But it could get you past the point where a long string in the bottom field shifts everything to the right.

Good luck.

arcy
  • 12,845
  • 12
  • 58
  • 103