Results 1 to 3 of 3
  1. #1
    neha_negi is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Use of swingutilities.invokeLater

    Hello, everybody

    In my code a Timer thread is running and inside of this thread by using :
    table.setValueAt(value,row,col );
    update the table .

    When i,print the table value by using :
    System.out.println(table.getValueAt(row,col));
    it prints the table value in console .

    By using simple repaint method table not show updated table value.For this i used swingUtilities.invokeLater().
    But atfter using swingUtilities.invokeLater() table is not show the updated values.

    So please tell me how to use SwingUtilities in my code .

    /////////////////////////////////////////////
    there is two classes ::
    .................................................. ..................
    package Master;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.Dimension;
    import javax.swing.*;

    public class Amain extends JFrame
    {
    Dimension screenSize ;
    Alarms alarms = new Alarms();
    public Alarmstime iframe;
    public static JDesktopPane desktop = new JDesktopPane();
    public static JPanel pane = new JPanel();
    JPanel panel3=new JPanel();
    byte def[] ;
    int time;
    Timer timer;
    JButton b1;
    public Amain()
    {
    super("AMAIN ");
    pack();
    setSize(600,600);
    getContentPane().add(pane);
    pane.setLayout(new BorderLayout());
    pane.add(panel3,BorderLayout.CENTER);
    panel3.setLayout(new FlowLayout());
    b1=new JButton("Alarm Button");
    panel3.add(b1);
    b1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    panel3.setVisible(false);
    System.out.println("Expand All is pressed ok");
    doMouseClicked();
    }
    });
    AckofPacket(2);
    System.out.println("Ack packet call" );
    setVisible(true);
    }
    void doMouseClicked()
    {
    iframe = new Alarmstime();
    iframe.setLocation(0, 0);
    iframe.setSize(800,700);
    iframe.setTitle("test");
    desktop.add(iframe);
    }
    public static void main(String args[])throws Exception
    {
    System.out.println("main function call firstly");
    new Amain();
    }
    public void AckofPacket(int seconds)
    {
    time=3000;
    timer = new Timer();
    timer.scheduleAtFixedRate( new RemindTask(), seconds*time,time);
    }
    class RemindTask extends TimerTask
    {
    public void run()
    {
    alarms.Alarmtablevalues();
    }//run close
    }//remind class

    class Alarmstime extends JFrame
    {
    JTabbedPane jtp = new JTabbedPane();
    JPanel content = (JPanel)getContentPane();
    Alarms page7 =new Alarms();
    Alarmstime()
    {
    jtp.setPreferredSize(new Dimension(600,550));
    jtp.addTab("Alarms", page7);
    content.add(jtp);
    setVisible(true);
    }
    }
    }

    .................................................. .............................

    package Master;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Calendar;
    import javax.swing.event.*;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;

    public class Alarms extends JPanel
    {
    JPanel PanelAlarm = new JPanel();
    JTabbedPane jtp = new JTabbedPane();
    JPanel ActiveAlarms = new JPanel();
    JPanel AlarmTable =new JPanel();
    DefaultTableModel tableModel1;
    RefreshThread RefThre;
    JScrollPane jsp;
    JTable table;
    JTable table2;
    static int rownum=0;
    public static Amain aempon;
    String data1[][] = new String[100][7];
    String data2[][]=new String[100][8];
    String Cname1[] = new String[7];
    String Cname2[]= new String[8];
    JButton bC,stop;
    Calendar cal = Calendar.getInstance();

    Alarms()
    {
    jtp.setPreferredSize(new Dimension(780,530));
    jtp.addTab("Active Alarms", ActiveAlarms);
    add(jtp);
    setVisible(true);
    ActiveAlarms.setLayout(new BorderLayout());
    AlarmTable.setLayout(null);
    Cname1[0] ="Entity";
    Cname1[1] ="Label";
    Cname1[2] ="Alarm ID";
    Cname1[3] ="Stat ID";
    Cname1[4] ="Direction";
    Cname1[5] ="State";
    Cname1[6] ="Count";

    tableModel1 = new DefaultTableModel(data1,Cname1);
    table = new JTable(tableModel1);
    jsp = new JScrollPane(table);
    jsp.setPreferredSize(new Dimension(600,150));
    jsp.setLocation(10,10);
    jsp.setSize(700,320);
    AlarmTable.add(jsp);

    bC=new JButton("REFRESH");
    bC.setLocation(20,350);
    bC.setSize(120,30);
    AlarmTable.add(bC);
    bC.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    table.setValueAt("a0",10 ,0 );
    table.setValueAt("a1",10 ,1 );
    table.setValueAt("a2",10 ,2 );
    table.setValueAt("a3",10 ,3 );
    table.setValueAt("a4",10 ,4 );
    table.setValueAt("a5",10 ,5 );
    table.setValueAt("a6",10 ,6 ); AlarmTable.repaint();
    table.repaint();
    }
    }
    );
    ActiveAlarms.add(AlarmTable,BorderLayout.CENTER);
    }
    public void Alarmtablevalues()
    {
    System.out.println("in side of the Alarmtable function");
    if(rownum<5)
    { table.setValueAt("Link",rownum ,0 );
    table.setValueAt("Label",rownum,1 );
    table.setValueAt("Report",rownum ,2 );
    table.setValueAt("Nnna",rownum,3 );
    table.setValueAt("Nnna",rownum,4 );
    table.setValueAt("State",rownum,5 );
    table.setValueAt("Count",rownum ,6 );

    AlarmTable.repaint();
    table.repaint();
    System.out.println(" Print the values of table 1");
    System.out.println(table.getValueAt(rownum,0));
    System.out.println(table.getValueAt(rownum,1));
    System.out.println(table.getValueAt(rownum,2));
    System.out.println(table.getValueAt(rownum,3));
    System.out.println(table.getValueAt(rownum,4));
    System.out.println(table.getValueAt(rownum,5));
    }
    rownum++;
    AlarmTable.repaint();
    table.repaint();
    System.out.println("table colsn ="+ table.getColumnCount());
    /* 1)
    showUpdates();
    try
    {
    Thread.sleep(3000);
    }
    catch(Exception e)
    {}
    new Thread().start();
    */
    //////////////////////////////
    // 2)
    // RefThre = new RefreshThread(AlarmTable);
    RefThre = new RefreshThread(table);
    Thread t = new Thread(RefThre);
    t.start();
    }//func close

    /*
    void showUpdates()
    {
    try{
    SwingUtilities.invokeLater(new Runnable() {
    public void run()
    {
    AlarmTable.repaint();
    table.repaint();
    }
    });
    }
    catch(Exception ie)
    {}
    }
    */
    class RefreshThread implements Runnable
    {
    private JTable Alarm;
    public RefreshThread(JTable t1 )
    {
    this.Alarm = t1;
    }
    public void run() {
    while( true ) {
    SwingUtilities.invokeLater( new Runnable() {
    public void run() {
    Alarm.repaint();
    }
    });
    try {
    Thread.sleep(5000L); //5s
    } catch( InterruptedException interrupted ) {
    break;
    }
    }
    }
    }//class close
    }
    .................................................. .........................................

    I would use swingutilities.invokeLater by two types of methods which was show in program both are not update the table.
    Please tell me how ,i use SwingUtilities............
    Last edited by neha_negi; 09-18-2008 at 12:11 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You should simplify your code to the bare minimum required to still be compilable and still demonstrate demonstrate your problem without all the extra irrelevant stuff that only increases the size of your code as this deters others from reading it. If no one reads it, no one will help you. Also please use code tags so your code will retain its formatting. To do this, you the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also,
    1) You don't want to add a JFrame into a JDesktopPane. This doesn't make sense and causes an error to be thrown (I'm sure you've seen this error).
    2) If you use a JDesktopPane, or any component, to be visible, it must be added to the JFrame or other component that his shown in the JFrame.
    3) With Swing you shouldn't use a util Timer but instead should use a Swing Timer. With a Swing Timer you don't have to worry about calling invokeLater(...) as anything called in the Timer is called on the EDT.

    Edit: have gone through much of your code. You need to study the Swing Timer API and Sun tutorial. It will help you to fix a lot of errors in your code. Also, you'll probably want to add / change the JTable data through its table model.
    Last edited by Fubarable; 09-19-2008 at 05:02 AM.

Posting Permissions

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