簡體   English   中英

在特定的JTable單元格中添加的JComboBox無法呈現

[英]JComboBox added in specific JTable cell does not render

我正在開發一個帶有秋千的游戲大廳。 我有一個JTable,所有不同的玩家都登錄了房間,我只想在一個單元格中添加一個JComboBox。 我的問題是comboBox無法正確呈現。

在此處輸入圖片說明

我知道關於該主題還有很多其他主題,但是我找不到同樣問題的人。

JComboBox box = new JComboBox();
box.addItem("Warrior");
/* Adds few other items (strings)*/
this.box.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        /* sends message to server to change character when the combobox's chosen element is changed*/
    }
});
TableUserModel model = new TableUserModel(localUser,this.box); //Specifying the local user as I don't want a JComboBox in the others user's rows.
JTable table = new JTable(this.model);
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(this.box));

表模型類

public class TableUserModel extends AbstractTableModel{

    private String[] columnNames = {"Username","Class","TeamColor","Action"};
    private Object[][] data = {{null,null,null,null}};
    private User localUser;
    private JComboBox box;

    public TableUserModel(User u,JComboBox box) {
        this.localUser = u;
        this.box = box;
    }

    @Override
    public int getColumnCount() {
        return this.columnNames.length;
    }

    @Override
    public int getRowCount() {
        return this.data.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        return this.data[row][col];
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Class getColumnClass(int column) {
        for (int row = 0; row < getRowCount(); row++) {
            Object o = getValueAt(row, column);
            if (o != null) {
                return o.getClass();
            }
        }
        return Object.class;
    }

    //The following method updates my data array when the informations are refreshed from the server
    public void refreshUsers(ArrayList<User> users) {
        int elementNumber = 0;
        //clears the data[][] array
        this.data = new Object[][];
        for (User usr : users) {
            this.data[elementNumber][0] = usr.getUsername();
            /*if it's the GriffinBabe's (local user) row */
                this.data[elementNumber][1] = this.box; //HERE!!! I add the JComboBox into the specific cell
            /*else adds a simple string information (for users other than localplayer) */
            this.data[elementNumber][2] = usr.getTeamColor();
            this.data[elementNumber][3] = null;
            elementNumber++;
        }
    }

用戶類別

只是一個包含一些信息的類,問題肯定不在這里

我只想在一個單元格中添加一個JComboBox。

這與TableModel無關。 顯示編輯器的是視圖(即表),因此您需要自定義表。

一種實現方法是重寫JTablegetCellEditor(...)方法。 例如:

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JPanel
{
    List<String[]> editorData = new ArrayList<String[]>(3);

    public TableComboBoxByRow()
    {
        setLayout( new BorderLayout() );

        // Create the editorData to be used for each row

        editorData.add( new String[]{ "Red", "Blue", "Green" } );
        editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
        editorData.add( new String[]{ "Apple", "Orange", "Banana" } );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                {
                    JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
                    return new DefaultCellEditor( comboBox1 );
                }
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table Combo Box by Row");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableComboBoxByRow() );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM