Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8591809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:36:26+00:00 2026-06-11T23:36:26+00:00

I have setup a JTable with paging – which works very well, but I

  • 0

I have setup a JTable with paging – which works very well, but I have a problem with updating data to the table. table.repaint() is not working. Here is the code that I am using. Thanks in advance!

String[][] data = new String[100][4];

 String[] columnNames = new String[]{
         "IP", "PC_NAME", "ttl", "db"};

Constructor:

gui(){
    JTable table =  new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    JButton next = new JButton("next");
    JButton prev = new JButton("prev");
    next.addActionListener(this);
    prev.addActionListener(this);
    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(prev);
    buttonPanel.add(next);
    panel.add(buttonPanel, BorderLayout.SOUTH);
    panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
    panel.add(scrollPane, BorderLayout.CENTER);
    getContentPane().add(panel);}

ActionListener:

public void actionPerformed(ActionEvent e) {
 if (e.getActionCommand() == "next") {
     Rectangle rect = scrollPane.getVisibleRect();
     JScrollBar  bar = scrollPane.getVerticalScrollBar();
     int blockIncr = scrollPane.getViewport().getViewRect().height;
     bar.setValue(bar.getValue() + blockIncr);
     scrollPane.scrollRectToVisible(rect);
 }
 if (e.getActionCommand() == "prev") {
     Rectangle rect = scrollPane.getVisibleRect();
     JScrollBar  bar = scrollPane.getVerticalScrollBar();
     int blockIncr = scrollPane.getViewport().getViewRect().height;
     bar.setValue(bar.getValue() - blockIncr);
     scrollPane.scrollRectToVisible(rect);
 }}

Here is the function that store data into an array:

int i=0;
public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
{
    data[i][0]=IP;
    data[i][1]=PC_NAME;
    data[i][2]=ttl;
    data[i][3]=gw;
    i++;
    table.repaint();
    scrollPane.repaint();

}

Edit 1:

I tried with DefaultTableModel and still no luck. Here is code that I used for updating table.
Constructor code didn’t changed.
Declaration:

private static final long serialVersionUID = 1L;
 String[][] data = new String[100][4];

 String[] columnNames = new String[]{
         "IP", "PC_NAME", "ttl", "db"};



            DefaultTableModel model = new DefaultTableModel(data,columnNames);
    JTable table =  new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);

Here is function that updated table:

 public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
{

    System.out.println(IP);
    model.setValueAt(IP, i, 0);
    model.setValueAt(PC_NAME, i, 1);
    model.setValueAt(ttl, i, 2);
    model.setValueAt(gw, i, 3);
    i++;
    model.fireTableDataChanged();
    table.repaint();
    scrollPane.repaint();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:36:27+00:00Added an answer on June 11, 2026 at 11:36 pm

    Your problem is that your comparing Strings with ==. Use the equals(...) method instead.

    • == compares if two variables refer to the same object — something you don’t care about. Thus two String variables can hold the same word but not be deemed equal by this operation.
    • the equals(...) or equalsIgnoreCase(...) method checks if two String variables hold the same chars in the same order, and that you do care about.

    So not:

    if (e.getActionCommand() == "next") {
      //  ...
    }
    

    but rather:

    if ("next".equals(e.getActionCommand())) {
      //  ...
    }
    

    or if you don’t care about case:

    if ("next".equalsIgnoreCase(e.getActionCommand())) {
      //  ...
    }
    

    Edit 1
    Next, once your JTable has hold of the data, changing the data will likely have no effect on the JTable. So here you change the data array:

    int i=0;
    public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
    {
        data[i][0]=IP;
        data[i][1]=PC_NAME;
        data[i][2]=ttl;
        data[i][3]=gw;
        i++;
        table.repaint();
        scrollPane.repaint();
    
    }
    

    But this array has already been passed into the JTable, is now part of its model and has been changed into a Vector in all likelihood.

    To see a change in your JTable’s representation of the data, you must change the data held by the table’s model. I suggest that you use a DefaultTableModel to hold your data in the JTable and then in your method above (which should begin with a lower case letter), you change the data held by the model.

    Also, regardless if your if blocks work or not, don’t use == to compare Strings as this will bite you, if not now, soon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup url routing in my ASP.NET 4 project, which works perfectly offline.
I have created a simple GUI which includes a JTable. This table may be
I have setup airbrake in my app but it doesn't seem to log any
I have setup all the things for Windows Azure and Drupal. But, when I
I have setup the following header file to create a Stack which uses an
I have setup a simple Oracle external table test that I (alongside a DBA
I have following method in my class which extends JTable: protected void setTableCursor(Cursor cursor)
I have setup a settings bundle and can successfully pull data from that. I
I have a table with about 150 rows in it. These are the data
I have setup a model structure which allows different models to associate with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.