小弟初做swing开发急求大神,jtable中如何在单元格中添加一个带有事件的jlabel 120
展开全部
问题拆分来看,其实就是一个渲染和表格单元格选中事件,以下代码仅做参考:
final JComboBox comboBox = new JComboBox();
groups = new ArrayList<String>();
comboBox.setEditable(true);
TableComboBoxEditor editor = new TableComboBoxEditor(comboBox);
table.getColumnModel().getColumn(2).setCellEditor(editor);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == 2) {
int lastRow = e.getLastRow();
Object value = table.getValueAt(lastRow, 2);
if (value != null && !value.equals("") && !groups.contains(value.toString())) {
String item = value.toString();
groups.add(item);
comboBox.addItem(item);
comboBox.setSelectedItem(item);
}
}
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocus();
}
});
final JComboBox comboBox = new JComboBox();
groups = new ArrayList<String>();
comboBox.setEditable(true);
TableComboBoxEditor editor = new TableComboBoxEditor(comboBox);
table.getColumnModel().getColumn(2).setCellEditor(editor);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == 2) {
int lastRow = e.getLastRow();
Object value = table.getValueAt(lastRow, 2);
if (value != null && !value.equals("") && !groups.contains(value.toString())) {
String item = value.toString();
groups.add(item);
comboBox.addItem(item);
comboBox.setSelectedItem(item);
}
}
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocus();
}
});
展开全部
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
final class ClickTableDemo extends JFrame {
public String getTitle() {return "ClickTableDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
private JTable table;
//构造表格演示的模型
private class MyModel extends AbstractTableModel{
public int getRowCount() {return 5;}
public int getColumnCount() {return 5;}
public Object getValueAt(int row, int col) {return "row="+row+",column="+col;}
}
ClickTableDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
table=new JTable(new MyModel());
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int c=table.getSelectedColumn();
int r=table.getSelectedRow();
if(c!=-1&&r!=-1)
//这里演示了鼠标事件
System.out.println("行:" + r + ",列:"+c);
}
});
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
final Container container = getContentPane();
container.add(new JScrollPane(table),BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new ClickTableDemo();
}
}
);
}
}
追问
先谢过大哥,这功能和我的需求很相似可是jtable中并没有添加jlabel控件?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
mouse pressed 吗
更多追问追答
追问
不是,jtable中的单元格中添加一个jlabel再为这个jlabel添加一个点击事件。大哥谢了。
追答
你到底是在做什么呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询