I make a JTable where I put the recorded text to textfile but I failed in my code. So I hope you please help me with my project.
jtble.jpg
I would like to move names towards in my draw line.how to do it like in this picture? sorry for my grammar because i'm comportable being filipino or tagalog language.
I wish you will help me! ;(
Codes here:
try{ File file = new File("Database.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line = br.readLine())!=null){ String data[] = line.split("-"); String[][] matrix = new String[data.length][]; int r = 0; for (String row : data) { matrix[r++] = row.split("-"); } model = new DefaultTableModel(matrix, col); table = new JTable(model){ public boolean isCellEditable(int row, int column){ return false; } }; } } catch (IOException ex) { ex.printStackTrace(); }
whole code:
import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.table.*; import java.io.*; public class Administrator extends JFrame { private String col []= {"Account No.","PinCode","First Name", "Last Name", "MI","BirthDate","Sex","E-mail"}; private JScrollPane scroll; private JTable table; private DefaultTableModel model; public Administrator(){ //ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg")); final JButton back = new JButton("back"); back.setFont(new Font("Tahoma", Font.BOLD, 12)); back.setLocation(20,480); back.setSize(70,30); //back.setPreferredSize(new Dimension(90,30)); back.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dispose(); } } ); try{ File file = new File("Database.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line = br.readLine())!=null){ String data[] = line.split("-"); String[][] matrix = new String[data.length][]; int r = 0; for (String row : data) { matrix[r++] = row.split("-"); } model = new DefaultTableModel(matrix, col); table = new JTable(model){ public boolean isCellEditable(int row, int column){ return false; } }; } } catch (IOException ex) { ex.printStackTrace(); } DefaultTableCellRenderer cent = new DefaultTableCellRenderer(); cent.setHorizontalAlignment( JLabel.CENTER ); table.getColumnModel().getColumn(0).setCellRenderer(cent); table.getColumnModel().getColumn(1).setCellRenderer(cent); table.getColumnModel().getColumn(2).setCellRenderer(cent); table.getColumnModel().getColumn(3).setCellRenderer(cent); table.getColumnModel().getColumn(4).setCellRenderer(cent); table.getColumnModel().getColumn(5).setCellRenderer(cent); table.getColumnModel().getColumn(6).setCellRenderer(cent); table.getColumnModel().getColumn(7).setCellRenderer(cent); table.getColumnModel().getColumn(0).setPreferredWidth(100); table.getColumnModel().getColumn(1).setPreferredWidth(100); table.getColumnModel().getColumn(2).setPreferredWidth(100); table.getColumnModel().getColumn(3).setPreferredWidth(100); table.getColumnModel().getColumn(4).setPreferredWidth(50); table.getColumnModel().getColumn(5).setPreferredWidth(100); table.getColumnModel().getColumn(6).setPreferredWidth(50); table.getColumnModel().getColumn(7).setPreferredWidth(120); scroll = new JScrollPane(table); scroll.setLocation(20, 320); scroll.setSize(750,150); Container pane = getContentPane(); pane.setLayout(null); pane.add(back); pane.add(scroll); //setIconImage(new ImageIcon("/images/icon.jpg").getImage()); //setLayout(new BorderLayout()); //JLabel www = new JLabel(new ImageIcon(getClass().getResource("/images/admin.jpg"))); //add(www); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setTitle("Account Profile"); setSize(800,550); setResizable(false); setVisible(true); setLocation(110, 30); } public static void main(String [] args){ new Administrator(); }
Here the sample TextFile records:
07-12345-Wilson-Dacles-T.-February 1, [email]1995-male-lordwils02@gmail.com[/email]-taken 21-1234-Wilson-Dacles-T.-February 1, [email]1995-male-lordwils02@gmail.com[/email]-Taken 69-123-JohnPaul-Gonzales-T.-November 1, [email]1995-male-lorwils02@gmail.com[/email]-single 31-12121-31121-212-212-January 2, 2013-male-212-312121-2121-2121-2121-2121-January 1, 2013-male-2121-2121