Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JTable Problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    philippines
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy JTable Problem

    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

    how to do it like in this picture? sorry for my grammar because i'm comportable being filipino or tagalog language.
    I would like to move names towards in my draw line.
    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
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JTable Problem

    Please repost the code. It is lacking indentations. All statements should not start in the first column.

    Also copy and paste the contents of the text file. It's not possible to copy anything from an image.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with JTable and MySQL
    By mija in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2012, 03:29 PM
  2. problem with JTable
    By deependeroracle in forum AWT / Java Swing
    Replies: 3
    Last Post: March 17th, 2012, 09:41 AM
  3. [SOLVED] JTable Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 0
    Last Post: October 15th, 2010, 04:33 PM
  4. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  5. Problem of getting result than SQL to JTable
    By MS_Dark in forum Exceptions
    Replies: 1
    Last Post: March 10th, 2009, 06:26 AM