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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:29:56+00:00 2026-06-15T06:29:56+00:00

I need some help for my problem. I have a table with e.g. a

  • 0

I need some help for my problem. I have a table with e.g. a double column and a string column. If the value in the double column is negativ, the string should be “negativ”. And the other way if the value is positiv, the string should be “positiv”.
The problem is now if I edit the double value in the jTable, the string should also be updated.

Update to my question, the actual code look like this:
But it doesn’t work, because the string in the second column wont be updated after I edit the first column value. It only works when I start the program the first time.

import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;

public class ExampleRemoveAddRows extends JFrame {

    private Object[] columnNames = {"Double", "positiv / negativ"};
    private Object[][] data = {
        {new Double(10.0), "positiv"},
        {new Double(-10.0), "negativ"},
        {new Double(20.0), "positiv"},
        {new Double(-30.0), "negativ"}
    };
    private JTable table;
    private DefaultTableModel model;

    public ExampleRemoveAddRows() {
        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
            @Override
            public Object getValueAt(int row, int column) {  
                if (column == 1) {
                    double number = Double.parseDouble(this.getValueAt(row, 0).toString());
                    System.out.println(number);
                    System.out.println("good");
                    System.out.println((number < 0) ? "negativ" : "positiv");
                    return "C: "+ this.getValueAt(row, 0);//((number < 0) ? "negativ" : "positiv");
                } else {
                    return super.getValueAt(row, column);
                }
            }  
        };
        table = new JTable(model);        
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ExampleRemoveAddRows frame = new ExampleRemoveAddRows();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Thanks for your help.

Sam

  • 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-15T06:29:57+00:00Added an answer on June 15, 2026 at 6:29 am

    I’ve revised your sscce to show the alternate approach suggested here. Note the alternate ways to get a Double constant. I’ve also re-factored the String constrants.

    Addendum: In helpful comments, @kleopatra observes that querying the model directly will always produce the correct result, but a TableModelListener will only see changes to column 0, not column 1. The simple expedient is to make column 1 non-editable, as its value depends completely on column 0.

    @Override
    public boolean isCellEditable(int row, int col) {
        return col == 0;
    }
    

    The first example below uses DefaultTableModel:

    import javax.swing.*;
    import javax.swing.table.*;
    
    /** @see https://stackoverflow.com/a/13628183/230513 */
    public class ExampleRemoveAddRows extends JFrame {
    
        public static final String NEGATIVE = "negativ";
        public static final String POSITIVE = "positiv";
        private Object[] columnNames = {"Double", POSITIVE + " / " + NEGATIVE};
        private Object[][] data = {
            {10d, null},
            {-10.0, null},
            {Double.valueOf(30), null},
            {Double.valueOf("-30"), null}
        };
        private JTable table;
        private DefaultTableModel model;
    
        public ExampleRemoveAddRows() {
            model = new DefaultTableModel(data, columnNames) {
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
    
                @Override
                public boolean isCellEditable(int row, int col) {
                    return col == 0;
                }
    
                @Override
                public Object getValueAt(int row, int col) {
                    if (col == 1) {
                        double number = (Double) this.getValueAt(row, 0);
                        return (number < 0) ? NEGATIVE : POSITIVE;
                    } else {
                        return super.getValueAt(row, col);
                    }
                }
    
                @Override
                public void setValueAt(Object aValue, int row, int col) {
                    super.setValueAt(aValue, row, col);
                    fireTableCellUpdated(row, 1); // may have changed
                }
            };
            table = new JTable(model);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ExampleRemoveAddRows frame = new ExampleRemoveAddRows();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    This variation extends AbstractTableModel:

    import java.awt.EventQueue;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.table.*;
    
    /**
    * @see https://stackoverflow.com/a/13628183/230513
    */
    public class ExampleRemoveAddRows extends JFrame {
    
        public static final String NEGATIVE = "negativ";
        public static final String POSITIVE = "positiv";
    
        public ExampleRemoveAddRows() {
            DoubleModel model = new DoubleModel();
            model.add(10.1);
            model.add(-10.2);
            model.add(Double.valueOf(30.1));
            model.add(Double.valueOf("-30.2"));
            JTable table = new JTable(model);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ExampleRemoveAddRows frame = new ExampleRemoveAddRows();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        private class DoubleModel extends AbstractTableModel {
    
            List<Double> data = new ArrayList<Double>();
    
            public void add(Double d) {
                data.add(d);
            }
    
            @Override
            public int getRowCount() {
                return data.size();
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public String getColumnName(int col) {
                if (col == 0) {
                    return "Double";
                } else {
                    return POSITIVE + " / " + NEGATIVE;
                }
            }
    
            @Override
            public Class<?> getColumnClass(int col) {
                if (col == 0) {
                    return Double.class;
                } else {
                    return String.class;
                }
            }
    
            @Override
            public boolean isCellEditable(int row, int col) {
                return col == 0;
            }
    
            @Override
            public Object getValueAt(int row, int col) {
                if (col == 0) {
                    return data.get(row);
                } else {
                    double number = (Double) this.getValueAt(row, 0);
                    return (number < 0) ? NEGATIVE : POSITIVE;
                }
            }
    
            @Override
            public void setValueAt(Object aValue, int row, int col) {
                if (col == 0) {
                    data.set(row, (Double) aValue);
                    fireTableRowsUpdated(row, row);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help in some wired problem. I have a sqlite table items as following
I need some help in solving this problem. We have a large amount of
I need some help on this problem. It is about ASP.NET MVC3. I have
I have this strange problem I need some help with. This menu code goes
I need some help on how to proceed with this problem : I have
I have a table which need new column. The newly introduced column need some
OK guys, I need some help... I have a table cell which I need
Need some help with this problem in implementing with XSLT, I had already implemented
I need some help with a small php problem. But i dont know how
I need some help in getting this right, problem Write a function which takes

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.