Untitled
unknown
plain_text
2 years ago
5.5 kB
16
Indexable
package zad1; import javax.swing.*; import javax.swing.table.TableCellRenderer; import java.awt.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CountryTable extends JTable { private javax.swing.JTextField jTextField1; private javax.swing.JLabel jLabel; private javax.swing.JLabel jlabel2; ArrayList<String> name = new ArrayList<String>(); ArrayList<String> capital = new ArrayList<String>(); ArrayList<String> population = new ArrayList<String>(); List<String> parts = new ArrayList<String>(); List<String> parts1 = new ArrayList<String>(); JTable j; // private void jTextFieldKeyPressed(java.awt.event.KeyEvent evt) { // char c = evt.getKeyChar(); // if (Character.isLetter(c)) { // jTextField1.setEditable(false); // jLabel.setText("Please enter number only"); // } else // jTextField1.setEditable(true); // // } public void reader() { try (BufferedReader br = new BufferedReader(new FileReader("C:/Users/kacpe/Desktop/UTP1_MK_S27662/data/countries.txt"))) { String line; int counter = 0; boolean firstLineProcessed = false; for (int j = 0; j < 9; j += 3) { for (int i = 0; i < 3; i++) { while ((line = br.readLine()) != null) { if (!firstLineProcessed) { parts1 = Arrays.asList(line.split("\t")); columnNames[0] = parts1.get(0); columnNames[1] = parts1.get(1); columnNames[2] = parts1.get(2); columnNames[3] = parts1.get(3); firstLineProcessed = true; } if (counter == 0) { counter++; i--; break; } parts = Arrays.asList(line.split("\t")); name.add(i, parts.get(j)); capital.add(i, parts.get(j + 1)); population.add(i, parts.get(j + 2)); } } } } catch (IOException e) { e.printStackTrace(); } } public CountryTable(String countriesFileName) { } public String[] columnNames = new String[4]; public JTable create() { reader(); String[] columnNames1 = {columnNames[0], columnNames[1], columnNames[2], columnNames[3], "Data"}; String[][] data = { {name.get(0), capital.get(0), population.get(0), "", ""}, {name.get(1), capital.get(1), population.get(1), "", ""}, {name.get(2), capital.get(2), population.get(2), "", ""} }; j = new JTable(data, columnNames1) { @Override public boolean isCellEditable(int row, int col) { if (col == 2) { return true; } else { return false; } } @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int col) { Component c = super.prepareRenderer(renderer, row, col); if (col == 2) { Object cellValue = getValueAt(row, col); if (cellValue instanceof Integer) { int intValue = (Integer) cellValue; c.setForeground(getColor(intValue)); } else { c.setForeground(getForeground()); } } else { c.setForeground(getForeground()); } return c; } private Color getColor(int intValue) { Color color = null; if (intValue > 0) { color = Color.GREEN; } else if (intValue < 0) { color = Color.RED; } else { color = getForeground(); } return color; } } ; j.setBounds(40, 50, 300, 400); JScrollPane sp = new JScrollPane(j); return j; } } package zad1; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class Main { private JTable ctab; public void createTable(String countriesFileName) throws Exception { ctab = new CountryTable(countriesFileName).create(); } public void showGui() { SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame f = new JFrame("Countries table"); f.add( new JScrollPane(ctab) ); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setVisible(true); } }); } public static void main(String[] args) { Main main = new Main(); try { main.createTable("data/countries.txt"); main.showGui(); } catch(Exception exc) { } } }
Editor is loading...