简体   繁体   中英

Put a JComboBox to JTable and make getting an element from list instead of editing the cell

I would like to make one cell in JTable to be a JComboBox object and be able to use it. It must be only one cell, not whole column. The table can contain the specific cell but not always, its placement is not static. My problem lies in putting JComboBox in JTable which can be used. I attached a code I was able to do so far. Table contains the JComboBox but when I click it there is no list and turns cell's editor on. I would like to make list show up. What should I do or what should be added/modified in code to gain my goal?

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class CustomCellRenderer {
    JTable table;
    TableColumn tcol;

    public static void main(String[] args) {
        new CustomCellRenderer();
    }

    public CustomCellRenderer() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        Object data[][] = { { "Vinod", "Computer", "3" },
                { "Rahul", "History", "2" }, { "Manoj", "Biology", "4" },
                { "Sanjay", "PSD", "5" } };
        String col[] = { "Name", "Course", "Year" };
        DefaultTableModel model = new DefaultTableModel(data, col);
        table = new JTable(model);
        tcol = table.getColumnModel().getColumn(0);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        tcol = table.getColumnModel().getColumn(1);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        tcol = table.getColumnModel().getColumn(2);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        JTableHeader header = table.getTableHeader();
        JScrollPane pane = new JScrollPane(table);
        panel.add(pane);
        frame.add(panel);
        frame.setSize(500, 150);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public class CustomTableCellRenderer extends DefaultTableCellRenderer implements ActionListener {
        public Component getTableCellRendererComponent(JTable table,
                Object obj, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component cell = super.getTableCellRendererComponent(table, obj,
                    isSelected, hasFocus, row, column);

            if (((String) obj).equals("History")) {
                JComboBox comboBox = new JComboBox();
                comboBox.addItem("History");
                comboBox.addItem("English");
                comboBox.addItem("Biology");
                comboBox.addItem("PE");
                comboBox.addItem("None of the above");
                comboBox.addActionListener(this);

                return comboBox;
            }
            return cell;
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println("Perform some action");
        }
    }
}

You have to implement a TableCellEditor not a TableCellRenderer and set it with JTable.setCellEditor(TableCellEditor) .

Look at this example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM