Results 1 to 3 of 3

Thread: Updating JTabel

  1. #1
    drwk is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Unhappy Updating JTabel

    Hi!

    I'm rather new to Java and I got a problem with JTabel.

    The program works like this:

    the program reads a txt file and inserts all the data, that is splitted with (,), into the tabel. And when the program closes all the data that is in the table gets written into the the same txt file.

    My problem is now that I dont know how to to update the cells in the tabel. I've tried with the fireTablexx methods but I dont really get the hang of it.

    Any form of help is appreciated. And if the code is requiered just tell and ill post it.

  2. #2
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    78
    Rep Power
    0

    Default

    Hi,

    Your explanation of the problem was a bit hard for me to visualize. I am sure many people on the forum would find it easier to help you if they see your code and notice where you are going wrong. However, I will still suggest that you read more about the JTable swing component on the Sun's official website at the link below.
    How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    I hope this helps.
    Menre

  3. #3
    drwk is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Well I got 2 working classes. One that creates the gui and one that has an arraylist.

    The ArrayList one :


    import javax.swing.Icon;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import java.util.ArrayList;
    import java.util.Observer;

    public class BookModell extends AbstractTableModel implements Observer {

    public ArrayList<Book> books = new ArrayList<Book>();
    public void add(Book b) {
    books.add(b);
    fireTableRowsInserted(
    books.size()-1, books.size()-1);
    }
    public void remove(Book b){
    int index = books.indexOf(b);
    books.remove(b);
    fireTableRowsDeleted(index,index);
    }
    public int getColumnCount(){
    return 4;

    }
    public int getRowCount(){
    return books.size();
    }
    public Object getValueAt(int row, int col){
    Book b = books.get(row);
    switch (col){
    case 0: return b.getTitel();
    case 1: return b.getIsbn();
    case 2: return b.getImage();
    case 3: return b.isAvailable();
    default: return null;
    }
    }
    public void setValueAt(int row, int col){
    fireTableCellUpdated(row, col);
    }
    public boolean isCellEditable(int row, int col) {
    return true;
    }

    public Class <?> getColumnClass(int col){
    switch(col){
    //case 0: return Icon.class;
    case 3: return Boolean.class;
    default: return super.getColumnClass(col);
    }
    }
    public String getColumnName(int col){
    switch (col){
    case 0: return "Titel";
    case 1: return "Isbn";
    case 2: return "Bild";
    case 3: return "Available";
    default: return null;
    }
    }

    }

    The Gui one : Warning for wierd read/write try and catch blocks :)

    import java.awt.Panel;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.BorderLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.ConvolveOp;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.security.Permission;
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;
    import java.util.Observer;
    import java.util.StringTokenizer;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.*;

    public class Modell extends AbstractTableModel implements Observer{

    //JButton save = new JButton("Spara");
    BookModell bm = new BookModell();

    public Modell(){
    //String data3;
    int count = 0;
    //Skapar Gui
    JFrame frame = new JFrame("Databas");
    JPanel panel = new JPanel();
    frame.setSize(500,500);
    BookModell bm = new BookModell();
    ArrayList <String> aS2 = new ArrayList<String>();

    //reads the txt file and puts the data in 4 arrays, split(,), and imports it to the bm arraylist.

    try{
    BufferedReader reader = new BufferedReader(new FileReader ("C:\\hej.txt"));
    String line ;
    while((line = reader.readLine()) !=null){
    aS2.add(line);
    count = count + 1;
    }
    for (String readline : aS2){
    String [] temp = null;
    temp = readline.split(",");
    String t1 = temp[0];
    String t2 = temp[1];
    String t3 = temp[2];
    String tx = temp[3];
    Boolean t4 = Boolean.parseBoolean(tx);
    Book GG = new Book (t1,t2,t3,t4);
    System.out.println(GG);
    bm.add(GG);
    }
    reader.close();
    }
    catch ( Exception ex){System.out.println("Exception:" + ex.getMessage());}


    JTable table = new JTable(bm);
    panel.add(new JScrollPane(table));
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);



    //reads the table and writes it in the txt file
    try{

    FileWriter fs = new FileWriter("C:\\hej.txt");
    BufferedWriter ws = new BufferedWriter(fs);


    for(int i = 0; i<=2; i++){ //Antal rader
    if (i >= 1){
    System.out.println("den går igenom");
    ws.write("\r\n");
    }
    for(int j = 0; j<=3; j++){ //antal kolumner
    if(j == 3){

    ws.write((String) table.getValueAt(i,j).toString());
    }
    else{
    ws.write((String) table.getValueAt(i,j)+",");
    }}
    }
    ws.close();
    }catch (Exception e){
    System.err.println("Error"+ e.getMessage());
    }



    //Table listener.
    ListSelectionModel selectionModel = table.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel .SINGLE_SELECTION);
    selectionModel.addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e){
    System.out.println(e);





    }
    });
    }
    public static void main(String[] args){
    new Modell();

    }
    public int getColumnCount() {
    return 0;
    }
    public int getRowCount() {
    return 0;
    }
    public Object getValueAt(int arg0, int arg1) {
    return null;
    }
    }

    And I dont know if I was clear enough with my problem so ill just repeat it. The main problem is that I dont really know how to update the tabledata when a cell is edited. I dont know where to use the fireTablexxx method. ;(

Similar Threads

  1. Updating Arrays
    By drymsza1234 in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 02:34 PM
  2. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 05:09 PM
  3. Help Updating Tree via Listeners
    By Seiki in forum SWT / JFace
    Replies: 1
    Last Post: 03-27-2009, 10:46 AM
  4. Updating Graphics
    By Greedful in forum Java 2D
    Replies: 2
    Last Post: 07-20-2007, 07:12 PM
  5. Updating into 2 tables in the DB
    By yuchuang in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •