Using the source code given, update the simple list to your top five chosen destinations. Include the destination title, a short description, and a small image for each. This will help ensure that you have fulfilled the acceptance criteria of the user story.
Add images for each destination and at least one additional customization, color scheme, or the like to make the control as elaborate as you want. Be sure to include clear and concise comments to explain your changes.
What I have tried:
here is my java project that I am trying to run as an application.
1 package src;
2 import java.awt.*;
3 import javax.swing.*;
4 import javax.swing.border.*;
5
6
7 public class TopFiveDestinationList {
8 public static void main(String[] args) {
9 SwingUtilities.invokeLater(new Runnable() {
10 public void run() {
11 TopDestinationListFrame topDestinationListFrame = new TopDestinationListFrame();
12 topDestinationListFrame.setTitle("Top 5 Destination List");
13 topDestinationListFrame.setVisible(true);
14 }
15 });
16 }
17 }
18
19
20 class TopDestinationListFrame extends JFrame {
21
24 private static final long serialVersionUID = 1L;
25 private DefaultListModel<textandicon> listModel;
26
27 public TopDestinationListFrame() {
28 super("Top Five Destination List");
29
30 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
31 setSize(900, 750);
32
33 listModel = new DefaultListModel<textandicon>();
34
35
36
37 Color listBackground = new Color(245, 245, 245);
38 Color selectionBackground = new Color(32, 136, 203);
39 Color selectionForeground = Color.WHITE;
40
41
42 addDestinationNameAndPicture("1. Alborg, Denmark - A cultural gem with cobbled streets and modern art.", new ImageIcon(getClass().getResource("/resources/Aalborg.jpg")));
43 addDestinationNameAndPicture("2. Cartagena, Colombia - A luxury escape with rich history and vibrant life.", new ImageIcon(getClass().getResource("/resources/Cartagena.jpg")));
44 addDestinationNameAndPicture("3. Sonoma County, USA - Wine country with innovative businesses and beautiful landscapes.", new ImageIcon(getClass().getResource("/resources/Sonoma.jpg")));
45 addDestinationNameAndPicture("4. Australian Outback - Remote adventures with expedition ships.", new ImageIcon(getClass().getResource("/resources/Outback.jpg")));
46 addDestinationNameAndPicture("5. Himalayan Hideaway - Breathtaking altitudes and serene beauty.", new ImageIcon(getClass().getResource("/resources/Himalayas.jpg")));
47
48
49 JList<textandicon> list = new JList<>(listModel);
50 list.setBackground(listBackground);
51 list.setSelectionBackground(selectionBackground);
52 list.setSelectionForeground(selectionForeground);
53
54
55 TextAndIconListCellRenderer renderer = new TextAndIconListCellRenderer(2);
56 list.setCellRenderer(renderer);
57
58
59 JScrollPane scrollPane = new JScrollPane(list);
60 getContentPane().add(scrollPane, BorderLayout.CENTER);
61
62
63 JLabel nameLabel = new JLabel("Created by Jordan Schwartz");
64 nameLabel.setHorizontalAlignment(JLabel.CENTER);
65 getContentPane().add(nameLabel, BorderLayout.SOUTH);
66 }
67
68 private void addDestinationNameAndPicture(String text, ImageIcon icon) {
69 TextAndIcon tai = new TextAndIcon(text, icon);
70 listModel.addElement(tai);
71 }
72 }
73
74
75 class TextAndIcon {
76 private String text;
77 private Icon icon;
78
79 public TextAndIcon(String text, Icon icon) {
80 this.text = text;
81 this.icon = icon;
82 }
83
84 public String getText() {
85 return text;
86 }
87
88 public Icon getIcon() {
89 return icon;
90 }
91
92 public void setText(String text) {
93 this.text = text;
94 }
95
96 public void setIcon(Icon icon) {
97 this.icon = icon;
98 }
99 }
100
101
102 class TextAndIconListCellRenderer extends JLabel implements ListCellRenderer<object> {
103
106 private static final long serialVersionUID = 1L;
107
108 private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
109
110 private Border insideBorder;
111
112 public TextAndIconListCellRenderer() {
113 this(0, 0, 0, 0);
114 }
115
116 public TextAndIconListCellRenderer(int padding) {
117 this(padding, padding, padding, padding);
118 }
119
120 public TextAndIconListCellRenderer(int topPadding, int rightPadding, int bottomPadding, int leftPadding) {
121 insideBorder = BorderFactory.createEmptyBorder(topPadding, leftPadding, bottomPadding, rightPadding);
122 setOpaque(true);
123 }
124
125 public Component getListCellRendererComponent(JList list, Object value,
126 int index, boolean isSelected, boolean hasFocus) {
127
128 TextAndIcon tai = (TextAndIcon) value;
129
130
131 setText(tai.getText());
132 setIcon(tai.getIcon());
133
134 if (isSelected) {
135 setBackground(list.getSelectionBackground());
136 setForeground(list.getSelectionForeground());
137 } else {
138 setBackground(list.getBackground());
139 setForeground(list.getForeground());
140 }
141
142 Border outsideBorder;
143
144 if (hasFocus) {
145 outsideBorder = UIManager.getBorder("List.focusCellHighlightBorder");
146 } else {
147 outsideBorder = NO_FOCUS_BORDER;
148 }
149
150 setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
151 setComponentOrientation(list.getComponentOrientation());
152 setEnabled(list.isEnabled());
153 setFont(list.getFont());
154
155 return this;
156 }
157
158
159
160
161
162
163 public void validate() {}
164 public void invalidate() {}
165 public void repaint() {}
166 public void revalidate() {}
167 public void repaint(long tm, int x, int y, int width, int height) {}
168 public void repaint(Rectangle r) {}
169 }
But when I attempt to run this as a java application this is the error messages I get.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at src.TopDestinationListFrame.<init>(TopFiveDestinationList.java:42)
at src.TopFiveDestinationList$1.run(TopFiveDestinationList.java:11)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Please help me. Thank you.