JListmyList = new JList<>(new String[]{"Java", "Python", "JavaScript"}); Color selectionForeground = myList.getSelectionForeground(); // Use selectionForeground to set the text color of the selected item(s)
JListIn this example, we create a JList of string colors and set the selectionForeground to white. This will make the text color of the selected item(s) white, making them more visible against the background color of the list. Both of these examples demonstrate the usage of getSelectionForeground() in a Java program using the javax.swing package library.myList = new JList<>(new String[]{"Red", "Green", "Blue"}); myList.setSelectionForeground(Color.WHITE);