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

  • Home
  • SEARCH
  • 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 8923343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:05:31+00:00 2026-06-15T07:05:31+00:00

I’ve followed some previous advice on this site about displaying database information in tables,

  • 0

I’ve followed some previous advice on this site about displaying database information in tables, however mine still won’t work. I have a generated Netbeans Dialog with a table named mainTable, I am setting the Model to a ResultSet that has been taken from the MySql.

EDIT the entire class:

import swing.SwingUtilities;
import javax.java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

swing.table.DefaultTableModel;

public class AddressBookImpl extends AddressBookGui implements ActionListener {
final static AddressBookImpl impl = new AddressBookImpl();
DefaultTableModel defaultTableModel = new DefaultTableModel();
AddressBookGui gui = new AddressBookGui();



public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() { impl.startGUI(); }});
}

public void startGUI(){
    gui.main(null);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setResizable(true);
    this.setTitle("Address Book");
    listeners();
    refreshTable();
}

public DefaultTableModel refreshTable() {
    try{

        DatabaseImpl dbimpl = new DatabaseImpl();
        dbimpl.refreshDatabase();
        ResultSet refreshResult = dbimpl.refreshResult;
        ResultSetMetaData meta = refreshResult.getMetaData();
        int numberOfColumns = meta.getColumnCount();

        while (refreshResult.next()) 
        {
            Object[] rowData = new Object[numberOfColumns];

            for (int i = 0; i < rowData.length; ++i)
            {
                rowData[i] = refreshResult.getObject(i + 1);
            }
            defaultTableModel.addRow(rowData);
        }
        this.defaultTableModel = defaultTableModel;
    } catch (Exception e) {
        e.printStackTrace();
    }

    gui.mainTable.setModel(defaultTableModel);
    return defaultTableModel;
}


public void listeners() {
    quitFileMenuBar.addActionListener(this);
    addButton.addActionListener(this);
    refreshButton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equalsIgnoreCase("Quit")) 
    {
        this.dispose();
    }
    if (e.getActionCommand().equalsIgnoreCase("Add"))
    {
        // Add a new address to the DB
    }
    if (e.getActionCommand().equalsIgnoreCase("About"))
    {

    }
    if (e.getActionCommand().equalsIgnoreCase("Refresh"))
    {
        refreshTable();
        System.out.println("Refreshing........");
    }
}
}

And the DatabaseImpl class:

protected ResultSet refreshDatabase() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url, user, password);
        System.out.println("Connection is created to " + url);

        Statement statement = con.createStatement();
        String selectAllQuery = "Select * from " + table + ";";

        ResultSet refreshResult = statement.executeQuery(selectAllQuery);
//          con.close();
        this.refreshResult = refreshResult;
        return refreshResult;

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return refreshResult;
}

However this just shows a blank area where the table should be, any help?

The GUI class is a bog standard netbeans generated class.

  • 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-15T07:05:33+00:00Added an answer on June 15, 2026 at 7:05 am

    It looks like you’re passing the TableModel into the wrong JTable, into one that is not being held by the currently visualized AddressBookGui instance. This won’t work (as you’re finding out) since setting the JTable of a newly created AddressBookGui instance will have absolutely no effect on the visualized object as while they are instances of the same class, they are independent of each other. The solution is to pass this model into the JTable that is held by the actually visualized AddressBookGui instance. Note a bad solution is to use static fields. Just don’t do this.

    Edit
    I think that your problem is with a convoluted misuse of inheritance and again of setting the model of a non-visualized JTable. I suspect that your line of code gui.main(null) calls a static main method that creates and displays an AddressBookGui instance, and that this guy holds your JFrame. If so, then again this AddressBookGui instance that it is displaying is not the same as that held by the gui variable.

    I suggest that you not have AddressBookImpl extend AddressBookGui, but rather have it simply hold a instance of AddressBookGui, and that you not call AddressBookGui’s main method but rather have AddressBookImpl be responsible for displaying its AddressBookGui instance.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.