?
Solved

JTable problem...please help!!

Posted on 2001-07-22
4
Medium Priority
?
182 Views
Last Modified: 2012-05-04
Hi,

I want to create a java-based email system based on the EXMH mail system which is currently being used in the computer science department of my university.  Right now, I'm having trouble with one of the basic tasks of viewing the email message numbers using JTable.

I already have a JTree displaying the files and directories in the Mail folder.  There is a recursive method which allows one to eventually view the inbox dirctory and all the email message numbers in that.  What I have tried to do is have a Jtable to the right of the JTree which only displays the email message numbers (ie. 1, 2, 3 etc) in the first column and I eventually want to display the sender, subject and date of the messages.

I really hope you can help me because I'm having a lot of difficulty right now. The code so far is attached. Thanking you very much,

Sadie

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.Vector;
import java.io.File;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Em22
{
   
  private static String baseDirName = "/c:/jdk1.3/bin/ProjectSaad/MailFile/Mail";  
   
   
   
   
    public static void main(String args[]) {
        JFrame frame = new JFrame("Em20");
      
        // handle window close
      
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


      

      //To create west pane containing the directory
        JPanel panel1 = new JPanel();

      
      //Create top node of tree
      DefaultMutableTreeNode top = new DefaultMutableTreeNode("Mail");
      
      Subtree sb = new Subtree();
      
      sb.addSubTree(top, baseDirName);
            
        JTree jt = new JTree(top);
        DefaultTreeCellRenderer tcr =
            (DefaultTreeCellRenderer)jt.getCellRenderer();
        tcr.setTextSelectionColor(Color.red);

        JScrollPane jsp = new JScrollPane(jt);
      jsp.setPreferredSize(new Dimension(390, 272));



       
      
      // Create east table pane for information about emails.
      
        JPanel panel2 = new JPanel();
        MyTableModel myModel = new MyTableModel();
        JTable table = new JTable(myModel);
        table.setPreferredScrollableViewportSize(new Dimension(390, 250));
      
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       
      //Create the scroll pane and add the table to it.
        JScrollPane tablePane = new JScrollPane(table);




      // Create south west editor pane for displaying emails.
      
        JPanel panel3 = new JPanel();
      JEditorPane editorPane3 = new JEditorPane();
      JScrollPane editorScrollPane3 = new JScrollPane(editorPane3);
      
      editorScrollPane3.setPreferredSize(new Dimension(600, 250));
      editorScrollPane3.setMinimumSize(new Dimension(10, 10));
      panel3.add(editorScrollPane3);

      /*JPanel panel4 = new JPanel();
      JButton button = new JButton();
      JScrollPane buttonPane = new JScrollPane(button);
      
      buttonPane.setPreferredSize(new Dimension(15, 10));
      //buttonPane.setMinimumSize(new Dimension(10, 10));
      panel4.add(buttonPane); */



panel1.add(jsp);
      panel2.add(tablePane);
      //panel4.add(buttonPane);

        frame.getContentPane().add("West", panel1);
        frame.getContentPane().add("East", panel2);
      frame.getContentPane().add("South", panel3);
      //frame.getContentPane().add("South", panel4);
        frame.setLocation(350, 350);
        //frame.setBackground(Color.blue);
      frame.pack();
        frame.setVisible(true);


    }
}





//To create the tree structure to display the directory

class Subtree
{

 private static String slash = "/";


    //To create a tree structure displaying the Mail directory

    private static String baseDirName = "/c:/jdk1.3/bin/ProjectSaad/MailFile/Mail";
       
    public static void addSubTree (DefaultMutableTreeNode node, String dirname) {
          
          File f = new File(dirname);
          String s[] = f.list();
          
          System.out.println("addSubTree " + dirname);
          
          for (int i = 0; i < s.length; i++) {
                
                File tmp = new File(dirname + slash + s[i]);
                
                System.out.println("new file " + s[i]);
                
                if (tmp.isDirectory()) {
                      System.out.println("adding a new sub node");
                      DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(s[i]);
                      node.add(newNode);
                        addSubTree(newNode, dirname + slash + s[i]);
                  } else {
                        System.out.println("adding as file " + s[i]);
                    node.add(new DefaultMutableTreeNode(s[i]));
              }      
      }
    }
}






class TableData
{
   
    Vector  rows = new Vector;
    private static String slash = "/";
    private static String baseDirName = "/c:/jdk1.3/bin/ProjectSaad/MailFile/Mail";

    //To create a tree structure displaying the Mail directory

    public void addRowData (Vector rows, String dirname)
    {
      File f = new File(dirname);
      String s[] = f.list();
      
      
      for (int i = 0; i < s.length; i++) {
          
          File tmp = new File(dirname + slash + s[i]);
          Vector newRow = new Vector;
          
          while (tmp.isFile()) {
                                  
                  newRow.addElement(tmp);
                  
                        addRowData(newRow, dirname + slash + s[i]);
                  rows.addElement(newRow);
                      }
          // rows.addElement(newRow);
          }
          
      }
    }
}




//To create the table displaying information about the emails

class MyTableModel extends AbstractTableModel {
   
private boolean DEBUG = true;

    final String[] columnNames = {"Mail Number"};
    Object [] []data= {};



    TableData dataObject = new TableData();


      public void getData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();
          data = new Object [numRows][numCols];
          TableData dataObject = new TableData();

          for (int i=0; i < numRows; i++) {
            for (int j=0; j < numCols; j++) {
            dataObject.addRowData(data[i][j]);
            }
          }
      }

    /*

int numCols = getColumnCount();
          TableData dataObject = new TableData();


            for (int i=0; i < numRows; i++) {
                for (int j=0; j < numCols; j++) {
                   
                data[i][j]=dataObject;

            }
          }
      }
    */


   
        public int getColumnCount() {
            return columnNames.length;
        }
       
        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
   
        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 4) {
                return false;
            } else {
                return true;
            }
        }
   

   
        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
   
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of " 
                                   + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }
   

   
        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
   
}





0
Comment
Question by:saadia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Expert Comment

by:Nushi
ID: 6306275
your problem is here:
public void getData() {
          int numRows = getRowCount();
          int numCols = getColumnCount();
          data = new Object [numRows][numCols];
          TableData dataObject = new TableData();

          for (int i=0; i < numRows; i++) {
               for (int j=0; j < numCols; j++) {
                    dataObject.addRowData(data[i][j]);
                    }
               }
     
you missing a string in the dataObject.addRowData method

Nushi.
0
 
LVL 1

Expert Comment

by:poluru
ID: 6307186
First of all u fix the Number Of Colums for ur Table
in this case lets say numcols=1 (i.e. u r displaying only mail number)
then while adding the rowdata u find the number of files in the present folder then add numbers to tabledata from 1 to numfiles.
nowhere u r adding the data.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7669552
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points NOT refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7714493
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question