Results 1 to 7 of 7
  1. #1
    javaevgny is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default JTable refresh button.doClick()

    Hi,

    I have a JTable with a refresh button. When I click the refresh button, the JTable is updated, so this works fine. Now I want to trigger automatically the refresh button with the button.doClick() method (just for testing), which I have placed in the constructor, so that the JTable would be updated each time a new instance of the JTable would be called.

    The actionPerformed() method looks like this:


    public void actionPerformed(ActionEvent source) {
    if (source.getSource() == button) {
    // do something
    } else if (source.getSource() == button2) {
    System.out.println("dateSampleX " + dateSample);
    antallTimer = 0;
    fh = new FileHandling2(x, y, antallTimer, user, studio, dateSample);
    fh.createFileName();
    fh.createFile();
    fh.writeToNewFile();
    fh.readFromFileToArray();
    fh.updateArray();
    fh.writeArrayToFile();
    fh.testPrintUpdatedFile();
    data = fh.readFromFileToArray();
    populateTable();
    }
    }

    The button2.doClick() method works fine in that way that all commands in the actionPerformed() method are executed. But what doesn't work is that the JTable is not refreshed when I use the button2.doClick() method. On the other hand, when I click on the refresh button (button2), then the JTable is refreshed. So what have I missed?
    Last edited by javaevgny; 01-11-2011 at 10:52 AM.

  2. #2
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Need to see the code.

  3. #3
    javaevgny is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Here's the code of the whole class. It's just a part of a package. I hope this helps to figure out the problem.


    Java Code:
    package com;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.table.*;
    import java.util.*;
    import javax.swing.event.*;
    import java.awt.Robot;
    
    
    public class tableExample5 extends JPanel implements ActionListener, TableModelListener {
    
        private JTable table;
        private JScrollPane scrollPane;
        private JTextArea output;
        private JTextField input;
        private JButton button, button2;
        private JPanel panel;
        private JLabel label;
        private Vector rows, columns;
        private DefaultTableModel tabModel;
    
        public int antallRader, antallKolonner;
        public int rader, kolonner, antallTimer;
        public String user, studio;
        public FileHandling2 fh;
        public String[][] data;
        public String day, d, clock;
        public int x, y;
        public boolean write;
        
        static String dateSample;    
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
        
        // Consturctor - get the chosen date
        public tableExample5(String s) {
            super();
            dateSample = s;
            antallRader = 16;
            antallKolonner = 8;
            user = "Erik";
            studio = "LK";
    
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            fh = new FileHandling2(antallRader, antallKolonner, antallTimer, user, studio, dateSample);
            data = fh.readFromFileToArray();
    
            fh.createFileName();
            fh.createFile();
            fh.writeToNewFile();
            fh.readFromFileToArray();
            fh.updateArray();
            fh.writeArrayToFile();
            fh.testPrintUpdatedFile();
    
            rows = new Vector();
            columns = new Vector();
            String[] columnNames = {
                "",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"
            };
            addColumns(columnNames);
            addRow();
    
            tabModel = new DefaultTableModel();
            tabModel.setDataVector(rows, columns);
    
            table = new JTable(tabModel);
            table.setPreferredScrollableViewportSize(new Dimension(900, 256));
            table.setRowSelectionAllowed(false);
            table.getSelectionModel().addListSelectionListener(new RowListener());
            table.getColumnModel().getSelectionModel().
                addListSelectionListener(new ColumnListener());
            table.getModel().addTableModelListener(this);
    
            add(new JScrollPane(table));
    
            output = new JTextArea(2, 40);
            output.setEditable(false);
            add(new JScrollPane(output));
            output.append("Click on a table cell\n");
    
            input = new JTextField(2);
            input.setText("0");
    
            panel = new JPanel();
            label = new JLabel("Number of hours:");
            button = new JButton("Book time");
            button2 = new JButton("Refresh table");
            button.addActionListener(this);
            button2.addActionListener(this);
            add(panel);
            panel.add(label);
            panel.add(input);
            panel.add(button);
            panel.add(button2);
            table.getParent().setBackground(Color.white);
            populateTable();
            simulateClick();
           //button2.doClick();
        }
    
        public void simulateClick() {
            try {
                Robot robot = new Robot();
    
                // Simulate a mouse click
                robot.mouseMove(550, 730);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    
        public void addColumns(String[] colName) {
            for (int i = 0; i < colName.length; i++) {
                columns.addElement((String) colName[i]);
            }
        }
    
        public void addRow() {
            for (int j = 0; j < antallRader; j++) {
            Vector r = new Vector();
                for (int i = 0; i < antallKolonner; i++) {
                    r.addElement((String) "");
                }
            rows.addElement(r);
            }
        }
    
        public void populateTable() {
            for (int j = 0; j < antallRader; j++) {
                for (int i = 0; i < antallKolonner; i++) {
                    table.setValueAt(data[j][i], j, i);
                    //System.out.print(data[j][i] + " ");
                }
                //System.out.println();
            }
        }
    
        public void tableChanged(TableModelEvent e) {
            switch (e.getType()) {
                case TableModelEvent.UPDATE:
                    break;
            }
        }
    
        public void actionPerformed(ActionEvent source) {
            if (source.getSource() == button) {
                antallTimer = Integer.parseInt(input.getText());
                System.out.println("Antall timer: " + antallTimer);
                fh = new FileHandling2(x, y, antallTimer, user, studio, dateSample);
                fh.createFileName();
                fh.createFile();
                fh.writeToNewFile();
                fh.readFromFileToArray();
                fh.updateArray();
                fh.writeArrayToFile();
                fh.testPrintUpdatedFile();
                data = fh.readFromFileToArray();
                populateTable();
                input.setText("0");
            } else if (source.getSource() == button2) {
                System.out.println("dateSampleX " + dateSample);
                antallTimer = 0;
                fh = new FileHandling2(x, y, antallTimer, user, studio, dateSample);
                fh.createFileName();
                fh.createFile();
                fh.writeToNewFile();
                fh.readFromFileToArray();
                fh.updateArray();
                fh.writeArrayToFile();
                fh.testPrintUpdatedFile();
                data = fh.readFromFileToArray();
                populateTable();
            }
        }
        
        public void outputSelection() {
            x = table.getSelectionModel().getLeadSelectionIndex();
            y = table.getColumnModel().getSelectionModel().
                    getLeadSelectionIndex();
    
            if      (y==1) day = "monday";
            else if (y==2) day = "tuesday";
            else if (y==3) day = "wednesday";
            else if (y==4) day = "thursday";
            else if (y==5) day = "friday";
            else if (y==6) day = "saturday";
            else if (y==7) day = "sunday";
    
            clock = table.getValueAt(x, 0).toString();
            d     = table.getValueAt(0, y).toString();
            if ((write==true) && (x!=0 && y!=0)) {
            output.append(
                    "How many hours do you want to book on " + day + ", " + d +
                    " from " + clock.substring(0, 5) + "?\n");
            }
            write = false;
            fh.readFromFileToArray();
        }
    
        private class RowListener implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent event) {
                if (event.getValueIsAdjusting()) {
                    write = true;
                    return;
                }
                outputSelection();
            }
        }
    
        private class ColumnListener implements ListSelectionListener {
            public void valueChanged(ListSelectionEvent event) {
                if (event.getValueIsAdjusting()) {
                    write = true;
                    return;
                }
                outputSelection();
            }
        }
    
        public static void createAndShowGUI() {
            //Disable boldface controls.
            UIManager.put("swing.boldMetal", Boolean.TRUE);
    
            //Create and set up the window.
            JFrame frame = new JFrame("Bookinglist");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            tableExample5 newContentPane = new tableExample5(dateSample);
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
      
    }
    Last edited by javaevgny; 01-12-2011 at 11:29 AM. Reason: Moderator Edit: Code tags added

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

    Default

    I've added code tags to your code to help make it readable. To see how to do this yourself, please read the link in my signature links below.

    I wonder if your problems are because you're calling doClick in the constructor before any components have been rendered.

    Edit: Also, we don't know what FileHandling2 does. I recommend that you try to simplify your problem to a minimal GUI that reproduces the problem, an SSCCE as this exercise will often show you the problem, and if not, gives us a small workable GUI that we can test and modify. Please see my other signature link about how to create one of these.
    Last edited by Fubarable; 01-11-2011 at 10:28 PM.

  5. #5
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I agree with Fubarable. We can't see what is happening as it is.
    Have you tried making refresh() a seperate method that is called from both the listener and the constructor?

  6. #6
    arafat877 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default

    Hi !
    I have developed a client/server application, witch the client is a swing based frame witch contains jtable, if client A adds/updates.removes a row, the other users must see the changes without clicking a button to refresh the jtable, I read some articles on the net, in witch they uses SwingWorker, java.swing.Timer, but I can't use this solutions because I don't undertand it .

    king regards

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

Similar Threads

  1. Browser's back button refresh the pages
    By hasansheikh18 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 05-31-2011, 04:44 PM
  2. Refresh Button Resubmitting Data - How To Prevent This?
    By Tirka in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-09-2009, 09:22 AM
  3. Refresh Button?
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-06-2009, 06:44 PM
  4. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM

Posting Permissions

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