【问题标题】:Updating jtextfield's from jtable从 jtable 更新 jtextfield
【发布时间】:2012-07-23 03:35:00
【问题描述】:

有人可以帮我解决这个问题吗?

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent e){
        if(e.getValueIsAdjusting()){
            ListSelectionModel model = table.getSelectionModel();  
            int lead = model.getLeadSelectionIndex(); 
            displayRowValues(lead);
        }
    }
    private void displayRowValues(int rowIndex){
        String country = "";   
        Object oCountry = table.getValueAt(rowIndex, 0);  
        country += oCountry.toString();
        countryTxt.setText(country );
    }
});

它应该在选择其中一个行中将jtable(表)中的单元格(表)中的数据发送到TextField(CountryTXT)中,但它仅在我点击行时时工作,而不是当我骑自行车带箭头时的表格钥匙。

【问题讨论】:

    标签: java swing jtable listener listselectionlistener


    【解决方案1】:

    【讨论】:

    • 很高兴我能帮上忙,尽管你应该按照@Reimeus 的建议去做,而不是完全评论它,只要在真实时返回,请参阅链接以获取解释
    • 这两种方法都在工作,但我还有另一个问题:删除所有行时,它给我带来了这个异常:java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    • 我用这个方法删除所有行:DefaultTableModel model=(DefaultTableModel)table.getModel(); int rc=model.getRowCount(); for(int i = 0;i
    【解决方案2】:

    问题出在这一行:

    if (e.getValueIsAdjusting()) { 
    

    将其替换为:

    if (e.getValueIsAdjusting()) return;
    

    顺便说一句,这是对多选事件的检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-04
      • 2014-12-04
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多