📄Description

Configurer une double ligne dans une JList.

✅Résultat

Untitled

🖥️Code multipleLinesInAJList.java :

package multiple_lines_in_a_JList;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JList;

public class multipleLinesInAJList{

	private JFrame frame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					multipleLinesInAJList window = new multipleLinesInAJList();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public multipleLinesInAJList() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		//Début paramétrage de la fênetre
		frame = new JFrame();
		frame.setVisible(true);
		frame.setTitle("ScrollPanel");
		frame.setBounds(100, 100, 400, 260);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		//Fin paramétrage de la fênetre
		
		//Création d'une chaine de caractère comportant un espace, utilisable via 
		//les éléments graphiques car l'HTML est interprété.
		String myString = "<html> Line 2 & <br> Line 3 </html>";
		
		//Création d'un panel.
		JPanel panel = new JPanel();
		panel.setBounds(90, 37, 201, 132);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		//Création d'un tableau de chaînes de caractères.
		String[] values = new String[] {"Line1", myString, "Line4"}; //Important
		
		//Création de la JList et ajout du tableau values.
		JList list = new JList(values);
		list.setBounds(23, 26, 150, 80);
		panel.add(list);
	}
}

🌵Arborescence

Untitled