JListmyList = new JList<>(new String[]{"Item 1","Item 2","Item 3"}); myList.setBackground(Color.BLUE); Color listBackground = myList.getBackground();
import javax.swing.*; import java.awt.*; public class JListExample extends JFrame { private JPanel mainPanel; private JListIn this example, a new JFrame is created containing a JList with five items. The background color of the list is retrieved using the getBackground method and stored in the variable listBackground. The JList is then added to the main panel of the JFrame using a BorderLayout. Finally, the JFrame is packed and made visible. The package library of javax.swing is Java Foundation Classes (JFC).myList; public JListExample() { mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; myList = new JList<>(items); Color listBackground = myList.getBackground(); mainPanel.add(myList, BorderLayout.CENTER); getContentPane().add(mainPanel); pack(); setVisible(true); } public static void main(String[] args) { new JListExample(); } }