Java Swing How to - Display JList over the JTextArea








Question

We would like to know how to display JList over the JTextArea.

Answer

import java.awt.BorderLayout;
/*from w  w  w . j a  va2 s.c o m*/
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
  public static void main(String[] args) {
    String[] ITEMS1 = { "one", "two", "three", "four", "five" };
    String[] ITEMS2 = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

    JPanel northPanel = new JPanel();
    northPanel.add(new JCheckBox("Reminder"));
    northPanel.add(new JComboBox(ITEMS1));
    northPanel.add(new JComboBox(ITEMS2));

    JPanel p = new JPanel(new BorderLayout());
    
    p.add(northPanel, BorderLayout.NORTH);
    p.add(new JScrollPane(new JTextArea(8, 30)), BorderLayout.CENTER);
    
    JFrame frame = new JFrame();
    frame.getContentPane().add(p);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}