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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:04:24+00:00 2026-05-16T10:04:24+00:00

This is the first time for me to post here, so sorry if I

  • 0

This is the first time for me to post here, so sorry if I made some mistake.

I am working on a JTable which column data have to verify some parameters, for example:

Column 3 values > 30
Column 4 values > 10
Column 5 values > 4

Also the first 2 columns are filled “automatically”, putting 0s in the rest of the columns.

If that data is correct, in the Column 5 I would show an image of a tick, otherwise, I would show an image of a warning.

For verifying this I use the following code

    ImageIcon accept = new javax.swing.ImageIcon(getClass().getResource("/resources/accept.png"));
    ImageIcon deny = new javax.swing.ImageIcon(getClass().getResource("/resources/exclamation.png"));

    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        double d1 = Double.valueOf(jTable.getValueAt(row, 2).toString());
        double d2 = Double.valueOf(jT.getValueAt(row, 3).toString());
        double d3 = Double.valueOf(jT.getValueAt(row, 4).toString());

        if(d1>MAX_A||d2>MAX_B||d3>MAX_C){
            jTable.setValueAt(deny, row, 5);
        }
        else{
            jTable.setValueAt(accept, row, 5);
        }
    }

The problem of this code is that returns a Stack Overflow, and I don’t know how to handle this.

Is there any other way to implement some verifier on a table that implies multiple cells?

Thanks in advance.

  • 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-05-16T10:04:25+00:00Added an answer on May 16, 2026 at 10:04 am

    The problem of this code is that
    returns a Stack Overflow, and I don’t
    know how to handle this.

    The problem is that your code sets a value in the model listener so another tableChanged event is generated. Your code should be something like:

    if (e.getColumn() != 5)
       // do your code
    

    I don’t see a problem using a TableModelListener to dynamically set the value of a column based on data in another column. Here is a simple example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class TableProcessing extends JPanel implements TableModelListener
    {
        public TableProcessing()
        {
            String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
            Object[][] data =
            {
                {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
                {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
                {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
                {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
            };
    
            DefaultTableModel model = new DefaultTableModel(data, columnNames)
            {
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                @Override
                public Class getColumnClass(int column)
                {
                    return getValueAt(0, column).getClass();
                }
    
                //  The Cost is not editable
                @Override
                public boolean isCellEditable(int row, int column)
                {
                    return (column == 3) ? false : true;
                }
            };
            model.addTableModelListener( this );
    
            JTable table = new JTable( model );
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JScrollPane scrollPane = new JScrollPane( table );
            add( scrollPane );
    
            String[] items = { "Bread", "Milk", "Tea", "Coffee" };
            JComboBox<String> editor = new JComboBox<String>( items );
    
            DefaultCellEditor dce = new DefaultCellEditor( editor );
            table.getColumnModel().getColumn(0).setCellEditor(dce);
        }
    
        /*
         *  The cost is recalculated whenever the quantity or price is changed
         */
        public void tableChanged(TableModelEvent e)
        {
            if (e.getType() == TableModelEvent.UPDATE)
            {
                int row = e.getFirstRow();
                int column = e.getColumn();
    
                if (column == 1 || column == 2)
                {
                    TableModel model = (TableModel)e.getSource();
                    int quantity = ((Integer)model.getValueAt(row, 1)).intValue();
                    double price = ((Double)model.getValueAt(row, 2)).doubleValue();
                    Double value = new Double(quantity * price);
                    model.setValueAt(value, row, 3);
                }
            }
        }
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("Table Model Listener");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TableProcessing());
            frame.pack();
            frame.setLocationByPlatform( true );
            frame.setVisible( true );
        }
    
        public static void main(String[] args) throws Exception
        {
            EventQueue.invokeLater( () -> createAndShowGUI() );
    /*
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });
    */
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.