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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:03:47+00:00 2026-06-07T21:03:47+00:00

This class is to create jtable with 6 columns, the last one containing boolean

  • 0

This class is to create jtable with 6 columns, the last one containing boolean checkboxes. I specified it in getColumnClass. I can’t uncheck/check these boxes, they are uneditable. I’m suspecting that methods setValueAt and getValueAt in class MyTableModel aren’t correct, but can’t get my head around it. Please help. Here are 3 classes I’m showing you: the gui class with main method, the MyTableModel which extends AbstractTableModel and the cell renderer class ColorRenderer.

import java.awt.Color;
import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

public class table extends JFrame {

    JTable table = new JTable();
    MyTableModel model = new MyTableModel(getDummyData());
    JScrollPane scroll;
    List<Integer> highlightedCell;

    public table() throws FileNotFoundException, IOException, InvalidFormatException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public Vector getDummyData() throws IOException, InvalidFormatException {
        highlightedCell = new ArrayList<Integer>();
        String nnnname;
        Vector vector;
        Vector vectorOfVector = new Vector();
        excelToList e = new excelToList("input.xls");
        List<Person> names = e.createWorkbook();
        int rowNum = 0;
        for (Person p : names) {
            nnnname = p.getName();
            vector = new Vector();
            vector.add(nnnname);
            vector.add(p.getDone());
            vector.add(p.getWaiting());
            vector.add(p.getORDERED());
            vector.add(p.getKPR1());
            vector.add(new Boolean(false));
            vectorOfVector.add(vector);
            rowNum++;
            if ((nnnname.matches("\\A\\p{ASCII}*\\z"))) {
                highlightedCell.add(rowNum - 1);
            }
        }
        return vectorOfVector;

    }

    public void createTable() throws FileNotFoundException, IOException, InvalidFormatException {


        table.setModel(model);
        table.setFillsViewportHeight(true);
        TableCellRenderer original = table.getDefaultRenderer(Object.class);
        table.setDefaultRenderer(Object.class,
                new ColorRenderer(highlightedCell, original));
        scroll = new JScrollPane(table);
        add(scroll);
        setSize(1000, 1000);
    }
    public static void main(String args[]) throws FileNotFoundException, IOException,     InvalidFormatException {
        table t = new table();
        t.createTable();
    }
}

class MyTableModel extends AbstractTableModel {

    private String[] columnName = {"NAME", "DONE", "WAITING",
        "OVERALL", "PERCENTAGE 1", "PERCENTAGE 2"};
    private Vector dataVector;

    public MyTableModel(Vector vectorOfVector) throws IOException, InvalidFormatException {
        dataVector = vectorOfVector;

    }

    public int getRowCount() {
        return dataVector.size();
    }

    public int getColumnCount() {
        return columnName.length;
    }
    Vector v;

    public Object getValueAt(int i, int i1) {
        v = (Vector) dataVector.elementAt(i);
        return v.elementAt(i1);
    }

    @Override
    public Class getColumnClass(int c) {
        switch (c) {
            case 5:
                return Boolean.class;
            default:
                return String.class;
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col < 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        Vector rowVector = (Vector) dataVector.get(row);
        rowVector.set(col, value);

        fireTableCellUpdated(row, col);
    }
}


class ColorRenderer implements TableCellRenderer {

    List<Integer> highlightedCoordinates;
    private TableCellRenderer cellRenderer;

    public ColorRenderer(List<Integer> list, TableCellRenderer cellRenderer) {
        if (cellRenderer == this || cellRenderer == null) {
            throw new IllegalArgumentException();
        }
        this.cellRenderer = cellRenderer;

        highlightedCoordinates = list;

    }

    public Component getTableCellRendererComponent(JTable jtable, Object color,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Component c;
        c = cellRenderer.getTableCellRendererComponent(jtable, color, hasFocus, hasFocus, row, row);


        if (column == 0 && highlightedCoordinates.contains(row)) {
            c.setBackground(Color.GREEN);
        } else {
            c.setBackground(Color.yellow);
        }

        return c;
    }
}
  • 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-07T21:03:48+00:00Added an answer on June 7, 2026 at 9:03 pm
    public boolean isCellEditable(int row, int col) {
        if (col < 1) {
            return true;
        } else {
            return false;
        }
    }
    

    If my understanding is correct you are trying to set only the 0th col as editable. Add the col index of checkbox to return as true.

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

Sidebar

Related Questions

I need to create a class to handle audio AudioTrack. In this class must
In C++, I create this class: public ref class UConfig { public: UConfig(short nr);
I have this class that have a function to load other classes and create
I have a class and this class has a delegate protocol. I create an
How do i create aliases in c# Take this scenario class CommandMessages { string
I want to extend RuntimeException to create this specific exception: class CompileLinkException extends RuntimeException
i used to create an instance of a singleton class like this: $Singleton =
UPDATE: This is the working example. First we create a class to hold weekday,
For this assignment I had to create my own string class. I initially wrote
I'm trying to create a table class, who's rows and columns may expand or

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.