Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help changing the background. It gives me an error everytime I do something with the background. Also I am trying to figured out

I need help changing the background. It gives me an error everytime I do something with the background. Also I am trying to figured out how to put picture side-by-side. The picture are one after another.

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

import java.awt.Color;

public class TopFiveDestinationList {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

TopDestinationListFrame topDestinationListFrame = new TopDestinationListFrame();

topDestinationListFrame.setTitle("Top 5 Destination List");

topDestinationListFrame.setVisible(true);

}

});

}

}

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();

//Top 5 destinations

addDestinationNameAndPicture("1. Puerto Rico (Puerto Rico is a Caribbean island and unincorporated U.S. territory with a landscape of mountains, waterfalls and the El Yunque tropical rainforest.)" , new ImageIcon(getClass().getResource("/resources/san-juan.jpg")));

addDestinationNameAndPicture("2. Cayman Island", new ImageIcon(getClass().getResource("/resources/picture-2.jpg")));

addDestinationNameAndPicture("3. Costa Rica", new ImageIcon(getClass().getResource("/resources/picture-3.jpg")));

addDestinationNameAndPicture("4. Peru", new ImageIcon(getClass().getResource("/resources/picture-4.jpg")));

addDestinationNameAndPicture("5. Hawaii", new ImageIcon(getClass().getResource("/resources/picture-5.jpg")));

JList list = new JList(listModel);

JScrollPane scrollPane = new JScrollPane(list);

TextAndIconListCellRenderer renderer = new TextAndIconListCellRenderer(2);

list.setCellRenderer(renderer);

getContentPane().add(scrollPane, BorderLayout.CENTER);

}

private void addDestinationNameAndPicture(String text, Icon icon) {

TextAndIcon tai = new TextAndIcon(text, icon);

listModel.addElement(tai);

}

}

class TextAndIcon {

private String text;

private Icon icon;

public TextAndIcon(String text, Icon icon, ) {

this.text = text;

this.icon = icon;

}

public String getText() {

return text;

}

public Icon getIcon() {

return icon;

}

public void setText(String text) {

this.text = text;

}

public void setIcon(Icon icon) {

this.icon = icon;

}

}

class TextAndIconListCellRenderer extends JLabel implements ListCellRenderer {

private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);

private Border insideBorder;

public TextAndIconListCellRenderer() {

this(0, 0, 0, 0);

}

public TextAndIconListCellRenderer(int padding) {

this(padding, padding, padding, padding);

}

public TextAndIconListCellRenderer(int topPadding, int rightPadding, int bottomPadding, int leftPadding) {

insideBorder = BorderFactory.createEmptyBorder(topPadding, leftPadding, bottomPadding, rightPadding);

setOpaque(true);

}

public Component getListCellRendererComponent(JList list, Object value,

int index, boolean isSelected, boolean hasFocus) {

// The object from the combo box model MUST be a TextAndIcon.

TextAndIcon tai = (TextAndIcon) value;

// Sets text and icon on 'this' JLabel.

setText(tai.getText());

setIcon(tai.getIcon());

if (isSelected) {

setBackground(list.getSelectionBackground());

setForeground(list.getSelectionForeground());

} else {

setBackground(list.getBackground());

setForeground(list.getForeground());

}

Border outsideBorder;

if (hasFocus) {

outsideBorder = UIManager.getBorder("List.focusCellHighlightBorder");

} else {

outsideBorder = NO_FOCUS_BORDER;

}

setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));

setComponentOrientation(list.getComponentOrientation());

setEnabled(list.isEnabled());

setFont(list.getFont());

return this;

}

// The following methods are overridden to be empty for performance

// reasons. If you want to understand better why, please read:

//

// http://java.sun.com/javase/6/docs/api/javax/swing/DefaultListCellRenderer.html#override

public void validate() {}

public void invalidate() {}

public void repaint() {}

public void revalidate() {}

public void repaint(long tm, int x, int y, int width, int height) {}

public void repaint(Rectangle r) {}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions

Question

Define Conventional Marketing.

Answered: 1 week ago

Question

Define Synchro Marketing.

Answered: 1 week ago

Question

Define marketing concepts.

Answered: 1 week ago

Question

1 what does yellow colour on the map represent?

Answered: 1 week ago