Выбор отдельных элементов при использовании JList и setCellRendererJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Выбор отдельных элементов при использовании JList и setCellRenderer

Сообщение Anonymous »

Мне нужно иметь возможность выбирать каждую строку addDestinationNameAndPicture, чтобы настроить ее в области списка.
class TopDestinationListFrame extends JFrame {

private DefaultListModel listModel;

public TopDestinationListFrame() {
super("Top Five Destination List");

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(900, 750);

listModel = new DefaultListModel();

//Make updates to your top 5 list below. Import the new image files to resources directory.
//added top 5 destinations and updated the pictures with the resources file.
addDestinationNameAndPicture("1. Moab, Utah (Red rocks, off-road, family fun.)", new ImageIcon(getClass().getResource("/resources/moab.jpeg")));
addDestinationNameAndPicture("2. Leavenworth, Washington (Charming Bavarian-themed village in the Cascade Mountains.)", new ImageIcon(getClass().getResource("/resources/leavenworth.jpeg")));
addDestinationNameAndPicture("3. Park City, Utah (Renowned ski resort town famous for its world class slopes.)", new ImageIcon(getClass().getResource("/resources/parkcity.jpeg")));
addDestinationNameAndPicture("4. Greenwater, Washington (Small, scenic mountain town located at the foothills of Mount Rainier.)", new ImageIcon(getClass().getResource("/resources/greenwater.jpeg")));
addDestinationNameAndPicture("5. San Diego, California (Coastal city known for its beautiful beaches, perfect weather and attractions.)", new ImageIcon(getClass().getResource("/resources/sandiego.jpeg")));

JList list = new JList(listModel);
JScrollPane scrollPane = new JScrollPane(list);

TextAndIconListCellRenderer renderer = new TextAndIconListCellRenderer();

list.setFont(new Font("Courier", Font.BOLD, 18));
list.setBackground(Color.lightGray);
list.setCellRenderer(renderer);

JLabel nameLabel = new JLabel("Developer: Haylee Raile");

getContentPane().add(nameLabel, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
}


Подробнее здесь: https://stackoverflow.com/questions/791 ... llrenderer
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»