//Author: ^-^ Veerle ^-^ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; import java.text.DecimalFormat; public class currencyConverter2T extends JPanel //Table showing the conversion rates { private currencyConverter2K currencyKeyListener = new currencyConverter2K(); private boolean requireSave = false; //When an obvious change is made (like adding/renaming/removing a currency) this is true private final int maxCurrenciesSupported = 50; private float[][] backupConversionMatrix; //Stores the last saved conversion matrix so it can be restored private JLabel lblDummy; //Dummy label for the NW corner private JLabel[] lblCurrencies = new JLabel[maxCurrenciesSupported]; private JLabel[] lblHeaderCurrencies = new JLabel[maxCurrenciesSupported]; private JTextField[][] txtRate = new JTextField[maxCurrenciesSupported][maxCurrenciesSupported]; //Creates the max size labels and txt fields private GridLayout conversionGrid; private DecimalFormat decFormat = new DecimalFormat("#0.00"); private Color clrBackgrounds = new Color(75,141,221); //Blue private Color clrForegrounds = new Color(255,255,255); //White public currencyConverter2T () { for (int i = 0; i < maxCurrenciesSupported; i++) { lblCurrencies[i] = new JLabel("", JLabel.CENTER); //Headers are used since you can not add the same component twice to containers lblHeaderCurrencies[i] = new JLabel("", JLabel.CENTER); lblCurrencies[i].setForeground(clrForegrounds); lblHeaderCurrencies[i].setForeground(clrForegrounds); for (int p = 0; p < maxCurrenciesSupported; p++) { txtRate[i][p] = new JTextField("", 3); txtRate[i][p].addKeyListener(currencyKeyListener); } } lblDummy = new JLabel(""); lblDummy.setForeground(clrForegrounds); conversionGrid = new GridLayout(1,1,2,2); this.setLayout(conversionGrid); this.add(lblDummy); this.setBackground(clrBackgrounds); } public boolean changesMade () //Checks if any changes have been made { if (requireSave) //If obvious change like add/remove/rename a currency { return true; } else //If not obvious change, compares the values in the grid to the backup conversion matrix { for (int row = 0; row < (conversionGrid.getRows() - 1); row++) { for (int column = 0; column < (conversionGrid.getColumns() - 1); column++) { if (Float.parseFloat(txtRate[row][column].getText()) != backupConversionMatrix[row][column]) { return true; //If a change has been made } } } } return false; //If no changes were found } public void saved () //Turns of the flag requiring a save once a sucessfull save has been made { requireSave = false; } public float[][] getDefault () //Gets the backup conversion matrix so it can be restored { return backupConversionMatrix; } public void updateTable (Vector vecCurrencies, float[][] conversionMatrix, boolean changeMade) //Updates the table that the user sees { if (changeMade) //If updating without saving { requireSave = true; } else //If first time, or sucessfull save, sets the backup conversion matrix to the new one { requireSave = false; backupConversionMatrix = conversionMatrix; } //removes everything from the grid for (int row = 0; row < conversionGrid.getRows(); row++) { for (int column = 0; column < conversionGrid.getColumns(); column++) { if (row == 0) { if (column == 0) { this.remove(lblDummy); } else { this.remove(lblHeaderCurrencies[column - 1]); } } else { if (column == 0) { this.remove(lblCurrencies[row - 1]); } else { this.remove(txtRate[row - 1][column - 1]); } } } } //Number of currencies + 1 for the row/column headers conversionGrid.setRows(vecCurrencies.size() + 1); conversionGrid.setColumns(vecCurrencies.size() + 1); //Sets up the required labels and txt boxes for (int index = 0; index < vecCurrencies.size(); index++) { //Sets the captions of the labels to the currencies they represent lblCurrencies[index].setText(vecCurrencies.elementAt(index).toString()); lblHeaderCurrencies[index].setText(vecCurrencies.elementAt(index).toString()); for (int column = 0; column < vecCurrencies.size(); column++) { //Sets the text in the txt field to the conversion rate it is representing txtRate[index][column].setText("" + decFormat.format(conversionMatrix[index][column])); } } //adds them to the grid for (int row = 0; row < conversionGrid.getRows(); row++) { for (int column = 0; column < conversionGrid.getColumns(); column++) { if (row == 0) //If first row (header row) { if (column == 0) { this.add(lblDummy); } else { this.add(lblHeaderCurrencies[column - 1]); } } else { if (column == 0) //If first column (header column) { this.add(lblCurrencies[row - 1]); } else { this.add(txtRate[row - 1][column - 1]); } } } } revalidate(); //If you change items in a panel you need to revalidate it so it repaints } public float[][] getConversionMatrix () //Gets all the values in the grid and puts them in a conversionMatrix to be written to the file { int numCurrencies = conversionGrid.getRows() - 1; float[][] conversionMatrix = new float[numCurrencies][numCurrencies]; for (int row = 0; row < numCurrencies; row++) { for (int column = 0; column < numCurrencies; column++) { conversionMatrix[row][column] = Float.parseFloat(txtRate[row][column].getText()); } } backupConversionMatrix = conversionMatrix; //Since you are saving the file, the backup now equals the saved conversion matrix return conversionMatrix; } }