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 9292087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:47:46+00:00 2026-06-18T20:47:46+00:00

This program is used to read data from database. In the database,there are three

  • 0

This program is used to read data from database. In the database,there are three tables pki17, pki18, pkn18. For viewing different tables,JComboBox is used and it works by changing the TableModel of the table. Also by using tableChanged method I made table editable. When a cell value in the table is changed the corresponding value in the database has to change to.

When I use tableChanged and actionPerformed methods together, value in database doesn’t get changed when I’m editing a cell in the swing table. When I remove actionPerformed method, then I can update database by editing table cells.

I need to have both abilities, to choose a table from the database by using JComboBox and update database values by editing values in the swing table.

I think the problem exists because TableModel of the table is changed in both methods. But I don’t know how to solve it.

public class TableCombobox extends JPanel implements ActionListener, TableModelListener  {
static JTable table;
static JComboBox box;
static MyTableModel model;
static Connection con = null;
static Statement stmt = null;

public TableCombobox() throws SQLException {
    super(new BorderLayout());
    table = new JTable(new MyTableModel("pki18"));
    table.setPreferredScrollableViewportSize(new Dimension(500, 400));
    table.setFillsViewportHeight(true);
    table.getModel().addTableModelListener(this);

    JScrollPane scrollPane = new JScrollPane(table);

    JPanel menuPanel = new JPanel();
    menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
    menuPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1,
            Color.black));

    String[] dalykas = { "Chose groop", "pki17", "pki18", "pkn18" };
    box = new JComboBox(dalykas);
    box.setMaximumSize(new Dimension(150, 25));
    box.setAlignmentX(Component.LEFT_ALIGNMENT);
    box.addActionListener(this);
    box.setSelectedIndex(2);

    menuPanel.add(box);

    JPanel cards = new JPanel(new CardLayout());
    cards.add(scrollPane, "view");
    add(menuPanel, BorderLayout.LINE_START);
    add(cards, BorderLayout.CENTER);
}
public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int col = e.getColumn();
    model = (MyTableModel) e.getSource();
    String stulpPav = model.getColumnName(col);
    Object data = model.getValueAt(row, col);
    Object studId = model.getValueAt(row, 0);
    System.out.println("tableChanded works");
    try {
        new ImportData(stulpPav, data, studId);
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}

public void actionPerformed(ActionEvent event) {        
        JComboBox cb = (JComboBox) event.getSource();
        String pav = (String) cb.getSelectedItem();
        if (pav != "Chose groop") {
            try {
                model = new MyTableModel(pav);
                table.setModel(model);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

private static void GUI() throws SQLException {
    JFrame frame = new JFrame("E-gradebook");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new TableCombobox());
    frame.pack();
    frame.setSize(800, 400);
    frame.setVisible(true);
}

public static void main(String[] args) throws SQLException {
    try {

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/pki18",
                "root", "");
        GUI();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } finally {
        if (stmt != null)
            stmt.close();
    }
}

static Connection getConnection() {
    return con;
}
}


public class ImportData {
static Connection con = TableCombobox.getConnection();

public ImportData(String a, Object b, Object c)
        throws ClassNotFoundException, SQLException {
    Statement stmt = null;
    try {

        String stulpPav = a;
        String duom = b.toString();
        String studId = c.toString();
        System.out.println(duom);
        con.setAutoCommit(false);
        stmt = con.createStatement();
        stmt.addBatch("update pki18 set " + stulpPav + " = " + duom
                + " where studento_id = " + studId + ";");
        stmt.executeBatch();
        con.commit();
    } catch (BatchUpdateException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (stmt != null)
            stmt.close();
        con.setAutoCommit(true);
        System.out.println("Data was imported to database");
    }
}   
}


public class MyTableModel extends AbstractTableModel{
static int rowCount;
static Object data [][];
static String columnNames [];

public  MyTableModel(String grupName) throws SQLException{
    String query ="select Studento_id, vardas_pavarde, 1_semestras,"+
            " 2_semestras, egzaminas, bendras_balas "+
            "from pki18." + grupName;

    ResultSet rs ;
    Connection con = TableCombobox.getConnection();

    Statement stmt = null;
    stmt = con.createStatement();
    rs = stmt.executeQuery(query);

    rs.last();
    rowCount = rs.getRow();
    data = new Object[rowCount][6];
    rs = stmt.executeQuery(query);
    for (int iEil = 0; iEil < rowCount; iEil++){
        rs.next();                  
        data[iEil][0] = rs.getLong("Studento_id");
        data[iEil][1] = rs.getString("Vardas_Pavarde");
        data[iEil][2]  = rs.getByte("1_semestras");
        data[iEil][3] = rs.getByte("2_semestras");
        data[iEil][4]  = rs.getByte("Egzaminas");
        data[iEil][5] = rs.getByte("bendras_balas");                    
    }

     String[] columnName  = {"Asmens_kodas","Vardas_Pavarde","1_Semestras"
            ,"2_Semestras","Egzaminas","Bendras_Balas"};
     columnNames = columnName;
}   
public int getColumnCount(){
    return columnNames.length;
}
public int getRowCount(){
    return data.length;
}
public String getColumnName(int col){
    return columnNames[col];
}
public Object getValueAt(int row, int col){
    return data[row][col];
}
public Class getColumnClass(int col){
    return getValueAt(0, col).getClass();
}
public boolean isCellEditable(int row, int col){
    return true;
}
public void setValueAt(Object value, int row, int col){
    data[row][col] = value;
    fireTableCellUpdated(row, col);
}
}
  • 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-18T20:47:47+00:00Added an answer on June 18, 2026 at 8:47 pm

    In the constructor, you add the table model listener to the current model only:

    table.getModel().addTableModelListener(this);
    

    In the action event, however, you replace the table model:

    model = new MyTableModel(pav);
    table.setModel(model);
    

    As a consequence, the new table model won’t have the listener, and you won’t receive notifications any more. Have the actionPerformed method add the listener as well, and your problem should be fixed.

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

Sidebar

Related Questions

For this program i have only used field separators from data files in shell
I used the following piece of code to read data from files as part
I'm building a program that has several functions that need to read data from
I have a C++ program that will read in data from a binary file
Basically I've written this program to check for strings. I've used socket method for
this program is for class to display the database in Listview.i want to add
This program I'm doing is about a social network, which means there are users
Are there best practices out there for loading data into a database, to be
I wanted to know how to read data from an unknown source of input,
Sorry, if this sounds ignorant. Is it possible to SECURELY upload data from 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.