Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 过滤器后获取值jtable(组合框多列上的jtable)_Java_Swing_Combobox_Jtable - Fatal编程技术网

Java 过滤器后获取值jtable(组合框多列上的jtable)

Java 过滤器后获取值jtable(组合框多列上的jtable),java,swing,combobox,jtable,Java,Swing,Combobox,Jtable,您可以在combobox测试运行中复制搜索,在选择过滤器后,它仍然会获得原始值。 你可以查看我的代码并完成数据过滤后收到的数据帮助我,搜索了一个多星期但无法完成。希望你能帮忙。多谢各位 Form.java: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import ja

您可以在combobox测试运行中复制搜索,在选择过滤器后,它仍然会获得原始值。 你可以查看我的代码并完成数据过滤后收到的数据帮助我,搜索了一个多星期但无法完成。希望你能帮忙。多谢各位

Form.java:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
public class Form implements Runnable {
    private static JFrame frame;
    private static DetailedComboBox combo;
    private static JTextField textField;
    private static DetailedComboBox comboLop;
    private static JTextField textField2;
    /**
     * @wbp.parser.entryPoint
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub



        List<List<?>> tableData = new ArrayList<List<?>>();
        tableData.add(new ArrayList<String>(
                Arrays.asList("0","Nam", "Phan Nam", "1")));
        tableData.add(new ArrayList<String>(
                    Arrays.asList( "1","Long", "Dinh Hoang Long", "2")));
        tableData.add(new ArrayList<String>(
                Arrays.asList( "3","Luc", "Phan Cong Luc", "4")));
        tableData.add(new ArrayList<String>(
                Arrays.asList( "4","Hoang", "Tran Hoang", "5")));



        String[] columns = new String[]{"ID","Name", "Full Name", "Capital"};
        int[] widths = new int[]{0,80, 200, 100};

        combo = new DetailedComboBox(columns, widths, 0);
        combo.setEditable(true);
      //  comboNhanVien.setEditable(true);
        combo.setBounds(58, 50, 154, 23);
        combo.setTableData(tableData);
        combo.setSelectedIndex(-1);
        combo.setPopupAlignment(DetailedComboBox.Alignment.LEFT);
        combo.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            showDetails();
          }

        });
      /*  combo.getEditor().getEditorComponent().addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent arg0) {
                combo.showPopup();
                combo.hidePopup();
            }
            @Override
            public void focusLost(FocusEvent arg0) {
                //combo.hidePopup();
            }
        });*/
        combo.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                 System.out.print(e.getKeyCode());
                String value= combo.getEditor().getItem().toString();

                 if (value.trim().length() == 0 && e.getKeyCode() != 40) {
                     combo.hidePopup();
                    // System.out.print(e.getKeyCode());
                }else {
                    combo.showPopup();
                    //System.out.print("X: "+e.getKeyCode());
                }
            }             
        });

        List<List<?>> tableDataLop = new ArrayList<List<?>>();
        tableDataLop.add(new ArrayList<String>(
                Arrays.asList("0","Class A")));
        tableDataLop.add(new ArrayList<String>(
                 Arrays.asList("1","Class B")));

        String[] columnsLop = new String[]{"ID","Class"};
        int[] widthsLop = new int[]{0,100};

        comboLop = new DetailedComboBox(columnsLop, widthsLop, 0);
        comboLop.setEditable(true);
      //  comboNhanVien.setEditable(true);
        comboLop.setBounds(58, 50, 154, 23);
        comboLop.setTableData(tableDataLop);
        comboLop.setSelectedIndex(-1);
        comboLop.setPopupAlignment(DetailedComboBox.Alignment.LEFT);
        comboLop.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            showDetailsLop();
          }

        });
        comboLop.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                 System.out.print(e.getKeyCode());
                String value= comboLop.getEditor().getItem().toString();

                 if (value.trim().length() == 0 && e.getKeyCode() != 40) {
                     comboLop.hidePopup();
                    // System.out.print(e.getKeyCode());
                }else {
                    comboLop.showPopup();
                    //System.out.print("X: "+e.getKeyCode());
                }
            }             
        });
        comboLop.setEditable(true);
        comboLop.setBounds(58, 94, 154, 23);



        JPanel p = new JPanel();
        p.setLayout(null);
        p.add(combo);
        p.add(comboLop);
         frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(p, BorderLayout.CENTER);
            p.add(getTextField());
            p.add(getTextField2());
        //frame.getContentPane().setLayout(null);
        //frame.getContentPane().add(comboNhanVien);
            //frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    }
    /**
     * Launch the application.
     */

    /**
     * Create the application.
     */

     private static void showDetails()
      {
        List<? extends Object> rowData = combo.getSelectedRow();
        textField.setText(rowData.get(1).toString());
       // capital.setText(rowData.get(2).toString());

      }
     public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Form());
      }
    private JTextField getTextField() {
        if (textField == null) {
            textField = new JTextField();
            textField.setBounds(234, 52, 86, 20);
            textField.setColumns(10);
        }
        return textField;
    }
    private JTextField getTextField2() {
        if (textField2 == null) {
            textField2 = new JTextField();
            textField2.setColumns(10);
            textField2.setBounds(234, 96, 86, 20);
        }
        return textField2;
    }
     private static void showDetailsLop()
      {
        List<? extends Object> rowData = comboLop.getSelectedRow();
        textField2.setText(rowData.get(1).toString());
       // capital.setText(rowData.get(2).toString());
      }
}

您需要了解组合框、表和模型之间的模型/视图索引关系

您的组合框将始终包含所有条目,因此其索引等同于模型索引

该表可能已过滤或未过滤,因此您需要将其索引转换为模型索引,以便设置组合框索引

如果我了解您试图做什么,那么这就是我对您的
valueChanged()
方法所做的更改:

public void valueChanged(ListSelectionEvent event)
{
    int index = table.getSelectedRow();

    if (index != -1)
    {
        int row = table.convertRowIndexToModel(index);
        comboBox.setSelectedIndex(row);
    }
}

请提供更多详细信息,您希望获得什么样的输出?此应用程序的功能是什么?“您可以查看我的代码并完成数据过滤后收到的数据帮助我,搜索了一周多,但无法完成。”虽然我们可以为您完成代码,但这并不是问题所在。这是一个问答网站,其中的问题“你能完成我的代码吗?”因“太宽泛”而脱离主题。为了更快地获得更好的帮助,发布一个or&实际显示您尝试了什么,并解释尝试失败的原因。
p.setLayout(null)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。请改用布局管理器,或与布局填充和边框一起使用。
选择过滤器后,仍会获得原始值。
-过滤JTable中的数据不会从TableModel中删除数据。它只过滤JTable视图中显示的数据。如果您只需要表中的数据,请使用
table.getValueAt(…)
NOT table.getModel().getValueAt(…)。在获取正确的数据之前,请先筛选选定的行,但在获取错误的数据之后,再筛选它。您可能需要一些时间来运行“帮助我”上的代码。谢谢,谢谢。我已经按照您的说明做了,您的java初学者不应该理解这种语言。再次多谢各位
public void valueChanged(ListSelectionEvent event)
{
    int index = table.getSelectedRow();

    if (index != -1)
    {
        int row = table.convertRowIndexToModel(index);
        comboBox.setSelectedIndex(row);
    }
}