简体   繁体   中英

Wrapping JLabels inside a JPanel thats inside a JScrollPane

My Java is a bit rusty so please bear with me. I have a method in my GUI class that calls another class file which returns a JList. The problem im having is getting the text from the JList, you can see an example of the output below

package com.example.tests;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 import com.example.tests.IJ_runTestAFJ;
 public class GUI_v2 extends JFrame
 {  
private static final long serialVersionUID = 1L;
IJ_CommonSetup setup = new IJ_CommonSetup();


Container c;
JPanel panel;
JScrollPane userScrollPane, errorScrollPane, sysScrollPane;
JTextArea   tfUserError, tfSysError;

private JButton resetButton;
public  JList<String> errorList;


GUI_v2() 
{
    resetButton = new JButton();
    resetButton.setText("Click to populate TextArea");
    resetButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                //test.runTest_Login(stUserName,stPwd);
                updatePanel();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    panel = new JPanel();

    tfSysError = new JTextArea(10,33);
    tfSysError.setLineWrap(true);
    tfSysError.setEditable(false);
    tfSysError.setWrapStyleWord(false);
    sysScrollPane = new JScrollPane(tfSysError);
    sysScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));

    panel.add(sysScrollPane);
    panel.add(resetButton);

    c = getContentPane();
    c.add(panel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setSize(400,250); //width, height
    setLocation(600,0);
    setResizable(false);
    validate();     
}//close GUI

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
   EventQueue.invokeLater(new Runnable() {

        public void run() {
            new GUI_v2().setVisible(true);
        }
    });
}

public void updatePanel()
{       
    errorList = new JList<String>();
    errorList = setup.getErrorJList();
    tfSysError.append(errorList.getComponent(1).toString());
    validate();
}


}// end on class

IJ_CommonSetup.java

package com.example.tests;

import javax.swing.JLabel;
import javax.swing.JList;
public class IJ_CommonSetup{
/**
 * 
 */

public static String stError = new String();
public static JList<String> stJListError = new JList<String>();


public JList<String> getErrorJList(){
    String error1 = new String("TestTestTestTestTestTestTestTestTestTestTestTestTestTest ");
    String error2 = new String("ApplesApplesApplesApplesApplesApplesApplesApplesApplesApples ");
    JLabel newError1 = new JLabel();
    newError1.setText(error1);
    JLabel newError2 = new JLabel(error2);
    stJListError.add(newError1);
    stJListError.add(newError2);
            return stJListError;
}
}

im having some trouble getting labels to wrap inside a panel that's inside a Scrollpane. At the moment if the string thats added to the label is long it is aligned to the left which is fine but the label stretches outside the panel cutting off the end of the string.

  • use JTextArea(int, int) in JScrollPane

  • setEditable(false) for JTextArea

instead of JLabel s added to JPanel (in JScrollPane )

Normal text in a JLabel doesn't wrap. You can try using HTML:

String text = "<html>long text here</html";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM