我在Jpanel中有一个JComboBox,我把它放在JTable的单元格中。 JPanel实现了TableCellRenderer。 ComboBox没有响应,当我尝试选择任何选项时没有任何反应。有任何想法吗???我使用NetBeans拖放功能设计了面板。
/*table class that contains the panel*/
public class TabUI extends JTable{
public LeftPaneUI lp;
public TabUI(){
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(new Object[][] {{ } }, new Object[] {"","" });
this.setModel(dm);
lp = new LeftPaneUI();
this.getColumnModel().getColumn(0).setCellRenderer( lp );
this.setRowHeight( 95 );
this.getColumnModel().getColumn(0).setPreferredWidth(160);
this.getColumnModel().getColumn(1).setPreferredWidth(405);
}
}
/*the aforementioned panel*/
public class LeftPaneUI extends javax.swing.JPanel implements TableCellRenderer {
/**
* Creates new form LeftPaneUI
*/
public LeftPaneUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
index_No = new javax.swing.JLabel();
type = new javax.swing.JLabel();
color = new javax.swing.JLabel();
index_val = new javax.swing.JLabel();
color_val = new javax.swing.JLabel();
entities = new javax.swing.JButton();
type_val = new javax.swing.JComboBox();
index_No.setText("Index No.");
type.setText("Type");
color.setText("Color");
index_val.setText("random");
color_val.setForeground(new java.awt.Color(235, 24, 24));
color_val.setText("red");
entities.setText("Word Entities");
type_val.setEditable(true);
type_val.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Generic", "Ubiquitous", "Event Driven", "Unwanted", "State Driven", "Optional" }));
type_val.setRequestFocusEnabled(true);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(color)
.addGap(33, 33, 33)
.addComponent(color_val))
.addGroup(layout.createSequentialGroup()
.addComponent(index_No)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(index_val))
.addGroup(layout.createSequentialGroup()
.addComponent(type)
.addGap(18, 18, 18)
.addComponent(type_val, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(entities)
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(index_No)
.addComponent(index_val))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(type)
.addComponent(type_val, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(color_val)
.addComponent(color, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(entities, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(3, 3, 3))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel color;
private javax.swing.JLabel color_val;
private javax.swing.JButton entities;
private javax.swing.JLabel index_No;
private javax.swing.JLabel index_val;
private javax.swing.JLabel type;
public javax.swing.JComboBox type_val;
// End of variables declaration
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return this;
}
}
/*adding the table when an option is selected*/
private void new_FileMenuActionPerformed(java.awt.event.ActionEvent evt)
{
TabUI tu = new TabUI();
jPanel2.add(tu);
}