我想在点击JList每一行时,行中的文字变成一个JTextField并且可以编辑。现在做到了可以加入一个JTextField,但是无法让它响应用户的操作。
JList personList = new JList(new Object[] { "a", "b", "c" });personItem pi = new personItem();
personList.setCellRenderer(pi);class personItem extends JPanel implements ListCellRenderer { private FlowLayout fmgr_l = new FlowLayout(FlowLayout.LEFT); public JLabel showBox = new JLabel(); public JTextField valueBox = new JTextField(); public personItem(String i, String v) {
super();
super.setLayout(fmgr_l);
valueBox.setText(v);
super.add(showBox);
super.add(valueBox);
valueBox.setVisible(false);
showBox.setVisible(true);
} public Component getListCellRendererComponent(JList list, Object value,
int row, boolean isSelected, boolean hasFocus) { showBox.setText(value.toString()); if (isSelected) {
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
valueBox.setVisible(true);
showBox.setVisible(false); } else {
setForeground(list.getForeground());
setBackground(list.getBackground());
valueBox.setVisible(false);
showBox.setVisible(true); }
return this;
}
}
JList personList = new JList(new Object[] { "a", "b", "c" });personItem pi = new personItem();
personList.setCellRenderer(pi);class personItem extends JPanel implements ListCellRenderer { private FlowLayout fmgr_l = new FlowLayout(FlowLayout.LEFT); public JLabel showBox = new JLabel(); public JTextField valueBox = new JTextField(); public personItem(String i, String v) {
super();
super.setLayout(fmgr_l);
valueBox.setText(v);
super.add(showBox);
super.add(valueBox);
valueBox.setVisible(false);
showBox.setVisible(true);
} public Component getListCellRendererComponent(JList list, Object value,
int row, boolean isSelected, boolean hasFocus) { showBox.setText(value.toString()); if (isSelected) {
setForeground(list.getSelectionForeground());
setBackground(list.getSelectionBackground());
valueBox.setVisible(true);
showBox.setVisible(false); } else {
setForeground(list.getForeground());
setBackground(list.getBackground());
valueBox.setVisible(false);
showBox.setVisible(true); }
return this;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货