Creating row headers for JTable at run time

一曲冷凌霜 提交于 2019-12-08 02:28:57

问题


I have just started working with JTable. This is my table example. The add row button adds rows to the table. I want to create row headers for this table. How can I achieve this?

Can any one help me please?

The code for the sample table is:

package test;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.table.*;

import test.InsertRows.CellEditor;

public class SampleTable extends JFrame {

JTable table;
JPanel panel;
DefaultTableModel dataModel;

public SampleTable () {
    super("My Table Example");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    onInit();
} 

void onInit()
{
    JButton b=new JButton("Add Row");
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            insertNewRow();

        }


    });

    panel = new JPanel(new BorderLayout());

    String[] columnNames = {
            "Name",
            "OID",
            "Index",
            "Value",
    };
    Object[][] data = {
            {"sysLocation","1.3.6.1.2.1.1.6","0",""},
                {"sysContact","1.3.6.1.2.1.1.4","0",""},
                    {"sysDescr","1.3.6.1.2.1.1.1","0",""}

    };

    dataModel = new DefaultTableModel();
    for (int col = 0; col < columnNames.length; col++) {
        dataModel.addColumn(columnNames[col]);
    }
    for (int row = 0; row < 3; row++) {
        dataModel.addRow(data[row]);
    }

    table = new JTable(dataModel);
    table.setDefaultEditor(Object.class, new CellEditor(this, table));
    table.setPreferredScrollableViewportSize(new Dimension(600, 120));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    panel.add(scrollPane,BorderLayout.CENTER);
    panel.add(b,BorderLayout.SOUTH);
    panel.setOpaque(true); //content panes must be opaque
    setContentPane(panel);

    //Display the window.
    pack();
    setVisible(true);
}
public static void main(String[] args) {
    SampleTable myFrame = new SampleTable();
} 
private void insertNewRow()
{

    if(vaidCheck(table)){
        dataModel.insertRow(table.getRowCount(),
                new Object[] {"", "", "", ""}
                );
        dataModel.fireTableRowsInserted(
                dataModel.getRowCount(),
                dataModel.getRowCount()
        );
    }
    else{
        JOptionPane.showMessageDialog(null,"Field is empty");                                                                            
    }

}
public boolean vaidCheck(JTable table)
{
      if(table.getCellEditor()!=null){
          table.getCellEditor().stopCellEditing();
      }
      for(int row=0;row< table.getRowCount();row++){
          for (int col=0;col<table.getColumnCount();col++){
              String om=table.getValueAt(row,col).toString();
              if(om.trim().length()==0){
                  return false;
              }
          }
      }
      return true;
 }

 public class CellEditor extends DefaultCellEditor
  {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private JTable m_Table = null;
        public CellEditor(JFrame parentFrame, JTable table) { 
            super(new JTextField());
            super.setClickCountToStart(1);
            m_Table = table;
        }
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, final int row, int column)
        {
            if(column == 0){
                 Object[] objectArray = {"Nikhil","Nijil"};
                    JComboBox comboBox = new JComboBox(objectArray);
                    comboBox.setEditable(true);
                    comboBox.setSelectedItem(value);

                    ItemListener itemListener = new ItemListener() {
                        public void itemStateChanged(ItemEvent e) {
                            if(e.getStateChange() == ItemEvent.SELECTED) {
                                if(null != m_Table.getCellEditor()){ 
                                    m_Table.getCellEditor().stopCellEditing();
                                }

                                m_Table.setValueAt(e.getItem(), row, 0);
                            }
                        }
                    };
                    comboBox.addItemListener(itemListener);
                    return comboBox;
            }
            if(column == 2){

            }
            JTextField textField = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column);
            return textField;

        }
  }
} 


回答1:


I hope my interpretation of the question is correct. To display a row header you need to create a new cell renderer and set it on the required column. For example, here is a basic renderer that mocks a table header:

static class RowHeaderRenderer extends DefaultTableCellRenderer {
    public RowHeaderRenderer() {
        setHorizontalAlignment(JLabel.CENTER);
    }

    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();

            if (header != null) {
                setForeground(header.getForeground());
                setBackground(header.getBackground());
                setFont(header.getFont());
            }
        }

        if (isSelected) {
            setFont(getFont().deriveFont(Font.BOLD));
        }

        setValue(value);
        return this;
    }
}

To set it up you can do the following:

table.getColumnModel().getColumn(0).setCellRenderer(new RowHeaderRenderer());

Here is how it looks like based on the posted code:



来源:https://stackoverflow.com/questions/13170969/creating-row-headers-for-jtable-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!