package dev.kske.minesweeper; import java.awt.BorderLayout; import java.awt.Font; import java.text.SimpleDateFormat; import java.util.Iterator; import java.util.List; import javax.swing.*; /** * Project: Minesweeper
* File: ScoreDialog.java
* Created: 16.04.2019
* Author: Kai S. K. Engelbart */ public class ScoreDialog extends JDialog { private static final long serialVersionUID = 3637727047056147815L; private JTable mtable; /** * Create the dialog. * * @param scores the scores to display * @param boardConfigName the name of the board configuration with which the scores are * associated */ public ScoreDialog(List scores, String boardConfigName) { setModal(true); setBounds(100, 100, 450, 300); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); getContentPane().setLayout(new BorderLayout(0, 0)); String[] columnNames = { "Place", "Name", "Duration", "Date" }; String[][] data = new String[scores.size()][4]; Iterator iter = scores.iterator(); for (int i = 0; i < data.length; i++) { Score s = iter.next(); data[i][0] = String.valueOf(i + 1); data[i][1] = s.getName(); data[i][2] = String.valueOf(s.getDuration()); data[i][3] = new SimpleDateFormat().format(s.getDate()); } mtable = new JTable(data, columnNames); getContentPane().add(mtable); JPanel panel = new JPanel(); getContentPane().add(panel, BorderLayout.NORTH); panel.setLayout(new BorderLayout(0, 0)); panel.add(mtable.getTableHeader(), BorderLayout.CENTER); JLabel lblHighscores = new JLabel("Highscores: " + boardConfigName); panel.add(lblHighscores, BorderLayout.NORTH); lblHighscores.setFont(new Font("Tahoma", Font.BOLD, 16)); lblHighscores.setHorizontalAlignment(SwingConstants.CENTER); } }