Created
February 25, 2019 15:17
-
-
Save uvlad7/8b3f9f68446832fae73b81797b8f0660 to your computer and use it in GitHub Desktop.
УП 2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Australia|Canberra | |
Belarus|Minsk | |
Belgium|Brussel | |
Brazil|Brasilia | |
Canada|Ottawa | |
Egypt|Cairo | |
Russia|Moskow | |
United Kingdom|London | |
United States|Washington |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
View view = new View(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.TreeMap; | |
public class View extends JFrame { | |
private JList list; | |
private Map<String, String> countries; | |
public View() { | |
super("Countries"); | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
list = new JList(); | |
countries = new TreeMap<>(); | |
try (Scanner scanner = new Scanner(new File("src\\countries.txt"))) { | |
scanner.useDelimiter("[|]|[\r\n]+"); | |
String country, capital; | |
while (scanner.hasNext()) { | |
country = scanner.next(); | |
capital = scanner.next(); | |
countries.put(country, capital); | |
} | |
} | |
//list.setListData(countries.entrySet().stream().map((Map.Entry<String, String> el) -> el.getKey() + " - " + el.getValue()).toArray()); | |
list.setListData(countries.entrySet().toArray()); | |
list.setCellRenderer(new ListRenderer()); | |
list.setFixedCellHeight(64); | |
add(new JScrollPane(list)); | |
pack(); | |
setSize(450, 300); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} catch (FileNotFoundException | ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} | |
class ListRenderer extends DefaultListCellRenderer { | |
private Font font; | |
public ListRenderer() { | |
font = new Font("Verdana", Font.PLAIN, 24); | |
} | |
@Override | |
public Component getListCellRendererComponent(JList list, Object value, int index, | |
boolean isSelected, boolean cellHasFocus) { | |
var valueEntry = (Map.Entry<String, String>) value; | |
var valueString = valueEntry.getKey() + " - " + valueEntry.getValue(); | |
JLabel label = (JLabel) super.getListCellRendererComponent( | |
list, valueString, index, isSelected, cellHasFocus); | |
if (isSelected) { | |
label.setIcon(new ImageIcon("src\\" + valueEntry.getKey() + ".png")); | |
} | |
label.setHorizontalTextPosition(JLabel.RIGHT); | |
label.setFont(font); | |
return label; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment