3 Replies - 119 Views - Last Post: 07 December 2017 - 06:37 AM Rate Topic: -----

#1 kaisbe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-December 17

Java2sAutoComboBox is not showing up on a JTable cell

Posted 07 December 2017 - 05:57 AM

I am trying to apply a custom ComboBox component, from java2s called "Java2sAutoComboBox", onto cells belonging to a specific column within a class extending from JTable called SupplyTable.

In order to accomplish so, I tested a sample from https://stackoverflo...0901944/6039980 and tried to follow the same logic, but by integrating the Java2sAutoComboBox component into the JTable class used in my application ..

I wrote the test program that summarises the behavior of the JTable used in my app:

import javax.swing.JTable;
import javax.swing.DefaultCellEditor;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;




public class Test extends JFrame {

Test() {

this.setLayout(new BorderLayout());

SupplyTableModel supplyTableModel = new SupplyTableModel();
SupplyTable supplyTable = new SupplyTable(supplyTableModel);

ArrayList<String> goods = new ArrayList<String>();

goods.add("First");
goods.add("Second");
goods.add("Third");

supplyTable.setGoods(goods);

JButton jButton = new JButton("Add Supply");

jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
        supplyTableModel.addRow();
    System.out.println("pressed");
}

});

this.add(jButton, BorderLayout.NORTH);

JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(supplyTable);
this.add(jScrollPane, BorderLayout.CENTER);
this.setSize(500, 500);
this.setLocationRelativeTo(null);
}

public static void main(String[] args) {
Test test = new Test();
test.setVisible(true);
}
}


SupplyTable:

import javax.swing.JTable;
import javax.swing.DefaultCellEditor;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SupplyTable extends JTable {

    Java2sAutoComboBox       java2sAutoComboBoxGoods;
    TableColumn              goodsTableColumn;
    ArrayList<String>        goodsDict;
    DefaultTableCellRenderer renderer;
    SupplyTableModel         supplyTableModel;

    SupplyTable(SupplyTableModel supplyTableModel) {

        super(supplyTableModel);

        this.supplyTableModel = supplyTableModel;
        goodsDict = new ArrayList<String>();

                renderer = new DefaultTableCellRenderer();
                java2sAutoComboBoxGoods = new Java2sAutoComboBox(goodsDict);
                java2sAutoComboBoxGoods.setMaximumRowCount(3);
        goodsTableColumn = this.getColumnModel().getColumn(0);
                goodsTableColumn.setCellEditor(new DefaultCellEditor(java2sAutoComboBoxGoods));
                goodsTableColumn.setCellRenderer(renderer);
    }

    public void setGoods(ArrayList<String> goods) {
                    this.goodsDict.clear();
                    this.goodsDict.add(".. <ADD> ..");
                    this.goodsDict.add("");
                for (String item: goods)
                        this.goodsDict.add(item);
            java2sAutoComboBoxGoods.setDataList(goodsDict);
            Util.updateComboDict((javax.swing.JComboBox)java2sAutoComboBoxGoods, goodsDict);
                    java2sAutoComboBoxGoods.setSelectedIndex(1);
            supplyTableModel.setGoods(goods);
    }
}


SupplyTableModel

import javax.swing.JTable;
import javax.swing.DefaultCellEditor;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SupplyTableModel extends AbstractTableModel {

    ArrayList<Supply> supplies;
    String[]          columnNames = {"", "Quantity", "Delivered?", "Note"};
    ArrayList<String> goods;
    int               orderDealType;

    public SupplyTableModel() {
        supplies = new ArrayList<Supply>();
    }
    public void setGoods(ArrayList<String> goods) {

        this.goods = goods;

    }

        public int getRowCount() {

                return supplies.size();
        }

        public int getColumnCount() {

                return columnNames.length;
        }

        public Object getValueAt(int row, int column) {

        switch (column) {
            case 0:
                return "";
            case 1:
                return (supplies.get(row)).quantity;
            case 2:
                return (supplies.get(row)).isDelivered;
            case 3:
                return (supplies.get(row)).note;
        }
                return null;
        }

        public String getColumnName(int c) {

                return columnNames[c];
        }

        public boolean isCellEditable(int r, int c) {

                return true;
        }

        public void setValueAt(Object value, int row, int column) {

        if (supplies.size() > row) {

            Supply old = supplies.get(row);

            switch (column) {
                            case 0:
                        if (!((String)value).trim().isEmpty())
                                    old.toBeDelivered = 0;
                    break;
                            case 1:
                    if (old.quantity != ((Integer)value).intValue())
                                    old.quantity = ((Integer)value).intValue();
                    break;
                            case 2:
                    if (old.isDelivered != ((Boolean)value).booleanValue())
                                    old.isDelivered = ((Boolean)value).booleanValue();
                    break;
                            case 3:
                    if (!old.note.equals((String)value))
                                    old.note = (String)value;
                    break;
            }

            supplies.set(row, old);

            fireTableRowsUpdated(row, row);
        }
        }

    public ArrayList<Supply> getRows() {
        return supplies;
    }

    public void addRow() {

        addRow(new Supply(-1, 0, 0, "", false, -1));
    }

    public void addRow(Supply row) {

        int insertedIndex = supplies.size()-1;

        supplies.add(row);
        fireTableRowsInserted(insertedIndex, insertedIndex);
    }

    public ArrayList<Supply> getSupplies() {
        return supplies;
    }
}


Supply

public class Supply {

    int     ID;
    int     toBeDelivered;
    int     quantity;
    String  note;
    boolean isDelivered;
    int     orderID;

    public Supply(int ID, int toBeDelivered, int quantity, String note, boolean isDelivered, int orderID) {
        this.ID = ID;
        this.toBeDelivered = toBeDelivered;
        this.quantity = quantity;
        this.note = note;
        this.isDelivered = isDelivered;
        this.orderID = orderID;
    }
}


Util

import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.Component;
import java.awt.Container;

import java.util.ArrayList;

public class Util {



    public static void updateComboDict(javax.swing.JComboBox jComboBox, String[] dict) {
                javax.swing.DefaultComboBoxModel comboModel = (javax.swing.DefaultComboBoxModel)jComboBox.getModel();
                comboModel.removeAllElements();
                for (String item : dict)
                    comboModel.addElement(item);
                jComboBox.setModel(comboModel);
        }

    public static void updateComboDict(javax.swing.JComboBox jComboBox, ArrayList<String> dict) {
        String[] dictArray = new String[dict.size()];
        int    i           = 0;
        for (String item : dict)
            dictArray[i++] = item;
        updateComboDict(jComboBox, dictArray);
    }
}


However, unlike what happens in the case of the code of 'TableRenderDemo' written by @mKorbel, the ComboBox doesn't show up whenever I double click and type any value ..

I'm really stuck on determining what led to such behavior.

Please help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: Java2sAutoComboBox is not showing up on a JTable cell

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Java2sAutoComboBox is not showing up on a JTable cell

Posted 07 December 2017 - 06:23 AM

I don't see any custom imports. If it's a custom component, why are you not importing it and using it?
Was This Post Helpful? 0
  • +
  • -

#3 kaisbe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-December 17

Re: Java2sAutoComboBox is not showing up on a JTable cell

Posted 07 December 2017 - 06:29 AM

View Postg00se, on 07 December 2017 - 06:23 AM, said:

I don't see any custom imports. If it's a custom component, why are you not importing it and using it?

Hi,

It gets imported implicitly after compiling the class from http://www.java2s.co...eteComboBox.htm on the same directory as the test app. No import is required.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Java2sAutoComboBox is not showing up on a JTable cell

Posted 07 December 2017 - 06:37 AM

Oh OK, didn't notice sorry. I fear this could be somewhat time-consuming to debug so i'm afraid at least micro-payment would be necessary for me to help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1