I am planning to create a table where everytime i add a row, a checkbox inside the table will also be created, will that be possible? And i am using window builder. If it is possible, can you give me some instructions how to do it? I did my research about it but none comes close to my question.
Thank you :)

This Oracle tutorial shows how to put a combo box ina JTable - it's exactly the same for a check box.

Thanks, ill check this.

  • agree read Oracle tutorial for working code example

  • note (probably typos or fast hands by @JamesCherrill) JCheckBox haven't something with JComboBox, logics is quite/little bit different, for JCheckBox just Boolean value stored in XxxTableModel to represents JCheckBox as Renderer/Editor in JTables view

OK, "exactly the same" was a bit of an exaggeration, but the overall approach is the same.

Thank guys, im trying to play with the code now.. ill tell you when i get it.. :)

hello, im back with a question, i tried to play with the codes, like what i have said we use window builder thats why i dont know how o create this things manually but im trying..

the column "e" should contain the checkbox, when i click it, i see the checkbox but it changes from true or false when i click it multiple times.. why is that?

That's what check boxes do. They toggle btween true and false when you click them.

yes. but i want it to be a checkbox when the user sees it and not a word "true" or "false" , what should i do??
but the idea you gave me about adding it into the table, it helped me, thanks!! my only problem now is showing it as a checkbox and now a word of true or false.

Sorry, my misunderstanding. Let's start again from the very beginning.
A JTable will display a column of boolean values as check boxes by default. Provided it knows tha the column contains Booleans. If you just pass a Object[][] or Vector to the JTable constructor it doesn't know the column types. So you need to provide a custom table model that defines your "e" column as Boolean. This part of the tutorial explains...

my only problem now is showing it as a checkbox and now a word of true or false

  • getColumnClass

  • isCellEditable for JTable based on AbstractTableModel

thanks guys.. but.. I understand that @JamesCherrill gave me the code wxample for getting the value of the checked box.. and a little later when i progress i will be needing that so thanks but this is the problem that i am encountering now...

please look at the pictures..

the first picture shows the first thing that the user will see.
the second picture shows what the user will see when he/she clicks it
the third pic show what happens after it was clicked.

those are my problems. I should see a checkbox even before i do anything.
i hope i have explained it.. im sorry.. english is not my language :(

Without seeing your code it's impossible to say what's wrong. Did you implement getColumnClass in yout table model?

Oh yes my code, im sorry ill post it. I have done the things that the tutorial you gave me instructed. I had extended the AbstractModel

to better explain it, ill just post the code.

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(56, 130, 479, 249);
        inventoryframe.getContentPane().add(scrollPane);

        table = new JTable();
        table.setShowVerticalLines(false);
        table.setShowHorizontalLines(false);
        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        table.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                        "t", "e"
                }
                ));
        scrollPane.setViewportView(table);

        textField = new JTextField();
        scrollPane.setColumnHeaderView(textField);
        textField.setColumns(10);
        inventoryframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        checkbox = new JCheckBox("borrow");
        checkbox.setHorizontalAlignment(SwingConstants.CENTER);
        checkbox.setBounds(360, 63, 97, 23);

        TableColumn sportColumn = table.getColumnModel().getColumn(1);
        sportColumn.setCellEditor(new DefaultCellEditor(checkbox));


        doIt();
    }
    public void doIt(){
        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        dtm.getDataVector().removeAllElements();


        ReadItemFromDB myReader = new ReadItemFromDB();
        List<Item> newItemList = myReader.showItems();


        for (Item myNewItems : newItemList) {
            Object[] rowData = new Object[1];


            rowData[0] =myNewItems.getItemID();



            dtm.addRow(rowData);
        }

        table.updateUI();


    }

    public int getColumnCount() {

        return 0;
    }

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getValueAt(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return null;
    }

as you can see, i have not edited yet the methods, because i dont still understand the process.. can you please explain it to me? Thank you.

OK. That code is going off in all kinds of wrong directions. I need you to go back and re-readthe link I gave earlier
This part of the tutorial
You need to create a simple table model, just like the one in that tutrorial, and implement a getColumnClass metjod in it, like the example a bit further down that tutorial (code in bold), that returns the appropriate class (eg String, Integer, Boolean) for each column. Returning Boolean will make that column use check boxes automatically.

wait.. please correct me if my understanding it wrong so here is my table:

table = new JTable();
        table.setShowVerticalLines(false);
        table.setShowHorizontalLines(false);
        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        table.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                        "t", "e"
                }
                ));
        scrollPane.setViewportView(table);

then u said i should implement the getColumnClass:

DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    public void doIt(){
        dtm.getDataVector().removeAllElements();
        dtm.getColumnClass(0);
        ReadItemFromDB myReader = new ReadItemFromDB();
        List<Item> newItemList = myReader.showItems();


        for (Item myNewItems : newItemList) {
            Object[] rowData = new Object[1];


            rowData[0] =myNewItems.getItemID();



            dtm.addRow(rowData);
        }





        table.updateUI();


    }



    public Boolean getColumnClass(){
        dtm.getValueAt(0, 1).getClass();
        return null;

    }

}

But, when i did those things, now when i click the button that will open the window for this table, It wont open :(

I really don't know how to make this any clearer than the Java and training experts who wrote the tutorial. Your code doesn't even start to resemble the tutorial code I linked to. Did you read it? I honestly don't know to make it any clearer than that. Sorry.

hmmn :( will this make a difference?

private void initialize() {
        inventoryframe = new JFrame();
        inventoryframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
        inventoryframe.getContentPane().setBackground(new Color(153, 204, 102));
        inventoryframe.getContentPane().setForeground(new Color(255, 255, 255));
        inventoryframe.getContentPane().setPreferredSize(new Dimension(1365, 747));
        inventoryframe.pack();
        inventoryframe.getContentPane().setLayout(null);

        JLabel lblInventory = new JLabel("Inventory Management");
        lblInventory.setBounds(56, 32, 234, 27);
        lblInventory.setFont(new Font("Tahoma", Font.PLAIN, 22));
        inventoryframe.getContentPane().add(lblInventory);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(56, 130, 479, 249);
        inventoryframe.getContentPane().add(scrollPane);

        table = new JTable();
        table.setShowVerticalLines(false);
        table.setShowHorizontalLines(false);
        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        doIt();
        table.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                        "t", "e"
                }
                ));


        scrollPane.setViewportView(table);


        checkbox = new JCheckBox("borrow");
        checkbox.setHorizontalAlignment(SwingConstants.CENTER);
        checkbox.setBounds(360, 63, 97, 23);

        TableColumn sportColumn = table.getColumnModel().getColumn(1);
        sportColumn.setCellEditor(new DefaultCellEditor(checkbox));



    }

    public void doIt(){
        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        getColumnClass(dtm);
        dtm.getDataVector().removeAllElements();

        ReadItemFromDB myReader = new ReadItemFromDB();
        List<Item> newItemList = myReader.showItems();

        for (Item myNewItems : newItemList) {
            Object[] rowData = new Object[1];


            rowData[0] =myNewItems.getItemID();



            dtm.addRow(rowData);
        }

        table.updateUI();


    }

    public Class getColumnClass(DefaultTableModel dtm) {
        return dtm.getValueAt(0, 1).getClass();
    }



}

OK, that's getting a lot closer. Main problem is that the getColumnClass method needs to be in the table model class, and takes an int as its parameter.
Here's an example that just illustrates the stuff you need (for the requirements that you have posted so far).
First, you need a table model that's just like the DefaultTableModel, except that it returns sensible values for getColumnClass, eg

    class BetterDefaultTableModel extends DefaultTableModel {

        private Class[] columnTypes;

        public BetterDefaultTableModel(Object[][] data, Object[] columnNames,
                Class[] columnTypes) {
            super(data, columnNames);
            this.columnTypes = columnTypes;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return columnTypes[columnIndex];
        }
    }

This just adds an array of column classes to the default, and implements a constructor just like the one you are currently using, except that you also pass in an array of column classes. That array is then used to give the correct return value for getColumnClass.

And here's an example of using it:

        JTable table = new JTable();
        Object[][] data = {{"hello", true}, {"goodbye", false}};
        String[] columnNames = {"a String", "a Boolean"};
        Class[] columnTypes = {String.class, Boolean.class};
        table.setModel(new BetterDefaultTableModel(
                data, columnNames, columnTypes));

Please spend a few minutes reading and understanding that code, then try using it in your environment. Assuming it does what you need, you can then use what it taught you to write your own program.

wihhhhh! i think im getting there but i still have a little error looK:

Look at the first pic:
That's my goal. To show the check box.!! :) :)

but, take a look at the sec. pic:
Ive only seen the label of the checkbox (which is "borrow") when I click the checkbox.

why is that?

Without seeing the relevant code I can only make wild guesses!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.