Results 1 to 3 of 3
  1. #1
    murali18 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Red face Need help with JTable

    Hi,

    I am new to swing and I need help with Jtable. I have a code that displays records dynamically. I have set a max record attribute so that only those many rows are shown at any particular time (lets say 10). After that the new records should come from 1st row again. I am able to achieve that but I still have problem. When the 11th record comes, previous records (like 10th, 9th, 8th, 7th...) all vanish. I wanted to achieve a way of display where I the new records just overwrite from the top, but I just don't know how. Below is the code,


    ------>


    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Vector;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;

    import com.ge.med.terra.jami.DemoUtils;
    import com.ge.med.terra.jami.XpImageViewport;



    public class SmartPrepTextDemo extends JPanel {


    private JPanel msgPanel = new JPanel();
    private JLabel msgText = new JLabel("Text");
    private JTextField Elapsed = new JTextField("Elapsed Time=");
    // private JButton jb_start = new JButton("");
    static String formatString = "%02d:%02d";
    private JTable table;
    private DefaultTableModel defaultModel;
    // private int max_records = 0;
    private int set_count = 1;
    int sec_trigger;
    int d = 2;
    int counter = 0;
    // private JLabel TimeVal = new JLabel();
    // private JButton jb_stop = new JButton("");
    javax.swing.Timer timer;
    javax.swing.Timer timer2;
    SimpleDateFormat timef = new SimpleDateFormat("mm:ss");
    String formattedTime;

    long ref = System.currentTimeMillis();
    public SmartPrepTextDemo(){

    msgPanel.setLayout(new BorderLayout());

    this.setLayout(new BorderLayout());
    add(msgPanel, BorderLayout.CENTER);
    msgPanel.add(Elapsed, BorderLayout.SOUTH);


    Timer timer = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    long currentDate = System.currentTimeMillis();
    long elapsedSeconds = (currentDate - ref)/1000;
    int minutes = (int) (elapsedSeconds / 60);
    int seconds = (int)(elapsedSeconds % 60);
    formattedTime = String.format(formatString, minutes, seconds);
    Elapsed.setText("Elapsed Time = " + formattedTime);
    sec_trigger = seconds;
    if((sec_trigger % d) == 0){

    my_actionPerformed(e);
    counter++;
    }
    }
    });
    timer.start();

    table = new JTable();
    defaultModel = new DefaultTableModel(0,(getMyCount()+1));
    setModel(defaultModel);
    table.setShowHorizontalLines( false );
    table.setShowVerticalLines( false );

    table.setShowGrid( false );
    table.getAutoscrolls();

    Vector vHead = new Vector();
    vHead.addElement( "Time" );
    for(int i=1; i<=getMyCount(); i++){
    vHead.addElement( "My" + i );

    }
    ((DefaultTableModel)table.getModel()).addRow(vHead );

    /* jb_start.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    my_actionPerformed(e);
    }
    });*/

    msgPanel.add(table, BorderLayout.CENTER);

    }

    // Standard main stub.
    public static void main(String[] args) {
    JFrame frame = new JFrame("Smart Prep Demo");


    SmartPrepTextDemo rd = new SmartPrepTextDemo();

    frame.setBounds(100, 100,512,512);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setContentPane(rd);
    frame.setVisible(true);
    }

    protected void my_actionPerformed (ActionEvent e){



    Vector vBody = new Vector();

    vBody.add( executeTime() );
    for(int i=1;i<=getMyCount(); i++){

    vBody.add(getMyValue(i));
    }

    if(recordInView()){
    ((DefaultTableModel)table.getModel()).addRow(vBody );

    }
    else{

    ((DefaultTableModel)table.getModel()).setRowCount( set_count);
    ((DefaultTableModel)table.getModel()).addRow(vBody );


    }

    }

    private String executeTime(){

    timer2 = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    long currentDate = System.currentTimeMillis();
    long elapsedSeconds = (currentDate - ref)/1000;
    int minutes = (int) (elapsedSeconds / 60);
    int seconds = (int)(elapsedSeconds % 60);
    formattedTime = String.format(formatString, minutes, seconds);


    }
    });

    return formattedTime;


    }

    public void setModel(TableModel model)
    {
    table.setModel(model);
    }

    public boolean isLastCell()
    {
    int rows = table.getRowCount();
    int cols = table.getColumnCount();
    int selectedRow = table.getSelectedRow();
    System.out.println(rows);
    System.out.println(cols);
    int selectedCol = table.getSelectedColumn();

    if((rows == (selectedRow+1)) && (cols == (selectedCol+1)))
    return true;
    else
    return false;
    }

    public int getMyCount(){

    int i = 3;
    return i;
    }

    public String getMyValue(int i){
    switch (i) {

    case 1: return "-0.10";

    case 2: return "-0.20";

    case 3: return "-0.30";

    case 4: return "-0.40";

    case 5: return "-0.50";

    case 6: return "-0.60";

    default: return "-0.90";

    }

    }

    public boolean recordInView(){
    System.out.println("--->" + table.getRowCount());
    if (table.getRowCount() <= 10) return true;

    else return false;
    }
    }

    ------ >
    I don't expect a complete solution but any suggestion as to how I can proceed to achieve that, would be great!

    Many thanks,
    Murali

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Eranga is right. The short answer lies in your TableModel and RowSorter implementations.
    The Java Tutorial. Read it.

Similar Threads

  1. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 09:27 AM
  2. JTable
    By hedonist in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 12:30 AM
  3. Regarding the JTable
    By suresh.uvce in forum AWT / Swing
    Replies: 5
    Last Post: 02-11-2009, 07:46 AM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:57 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
  •