无法在JTable模型单元格中看到我的按钮文本

时间:2018-02-05 11:57:46

标签: java jtable jbutton

我不知道我的按钮文字部分出了什么问题,但它没有显示按钮文字删除。

我正在显示一个文本文件中的表,该文件在最后一列有一个按钮,是一个删除按钮,我按照本教程

http://camposha.info/source/java-jtable-button-column/

他所做的是使用已有数据的现有列,所以我尝试将数据更改为删除,但是表格没有显示文本,尽管它显示了可点击的按钮。

从文件中读取文本到代码表的代码:

class myStoreHouse extends JPanel {
    private JPanel buttonPanel2 = new JPanel();
    private JButton homeButton = new JButton("Back");
     FileReader file;
     String wholeLine;
     private JTable table;
     private DefaultTableModel model;
     JScrollPane scroll;
     String[][] data = new String[50][50];
     String[] column = {"Item Name", "Category", "Date of Expiry", ""};
     int noOfRows = 0;

    public myStoreHouse(){
        try {
            file = new FileReader("expiryDateEntry.txt");
            BufferedReader br = new BufferedReader(file);
            System.out.println("hi");
            wholeLine ="";
            while((wholeLine = br.readLine())!= null){
                int count = 0;
                String delQuotes = "";
                System.out.println(wholeLine);
                String regex = "([^\"]\\S*|\".+?\")\\s*";
                Matcher m = Pattern.compile(regex).matcher(wholeLine);
                    while(m.find()){
                        if(m.group(1) != null){
                            delQuotes = m.group(1).replace("\"", "");
                            if(count == 0){
                                data[noOfRows][count ] = delQuotes;
                                System.out.println(delQuotes);
                            }
                            if(count == 1){
                                data[noOfRows][count ] = delQuotes;
                            }
                            if(count == 2){
                                data[noOfRows][count ] = delQuotes;
                            }
                            if(count == 3){
                                data[noOfRows][count] = "Delete";
                            }
                        }
                        if(count < column.length) count++;
                    }

                noOfRows++; 
            }
            model = new DefaultTableModel(data,column);
            table = new JTable(model);
            table.setFillsViewportHeight(true);
            table.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
            table.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor(new JTextField()));
            scroll = new JScrollPane(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.setLayout(new BorderLayout());
        this.add(scroll, BorderLayout.CENTER);
        this.add(getButton(), BorderLayout.SOUTH);
    }

ButtonRenderer

  class ButtonRenderer extends JButton implements  TableCellRenderer
     {

       //CONSTRUCTOR
       public ButtonRenderer() {
         setOpaque(true);
       }
       @Override
       public Component getTableCellRendererComponent(JTable table, Object obj,
           boolean selected, boolean focused, int row, int col) {

         //SET PASSED OBJECT AS BUTTON TEXT
         setText((obj==null) ? "":obj.toString());
         return this;
       }

     }

ButtonEditor

     class ButtonEditor extends DefaultCellEditor
     {
       protected JButton btn;
        private String lbl;
        private Boolean clicked;

        public ButtonEditor(JTextField txt) {
         super(txt);

         btn=new JButton("");
         btn.setOpaque(true);

         //WHEN BUTTON IS CLICKED
         btn.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent e) {

             fireEditingStopped();
           }
         });
       }


        @Override
       public Component getTableCellEditorComponent(JTable table, Object obj,
           boolean selected, int row, int col) {

           //SET TEXT TO BUTTON,SET CLICKED TO TRUE,THEN RETURN THE BTN OBJECT
          lbl=(obj==null) ? "":obj.toString();
          btn.setText(lbl);
          clicked=true;
          return btn;
       }

       //IF BUTTON CELL VALUE CHNAGES,IF CLICKED THAT IS
        @Override
       public Object getCellEditorValue() {

            if(clicked ==  true)
            {
                JOptionPane.showMessageDialog(btn, "Are you sure?");
            }
            clicked=false;
            return new String(lbl);
       }

        @Override
       public boolean stopCellEditing() {
            clicked=false;
            return super.stopCellEditing();
       }

        @Override
       protected void fireEditingStopped() {
            super.fireEditingStopped();
       }
     }
}

任何帮助表示赞赏!谢谢。

1 个答案:

答案 0 :(得分:0)

由于代码未达到count = 3而改变了条件,因为m.find()只有3个匹配,因此它永远不会触及if(count == 3)。我需要做的就是在count == 2时在条件中添加另一行data[noOfRows][count+1] = "Delete";

while(m.find()){
    if(m.group(1) != null){
        delQuotes = m.group(1).replace("\"", "");
    if(count == 0){
        data[noOfRows][count ] = delQuotes;
        System.out.println(delQuotes);
    }
    if(count == 1){
        data[noOfRows][count ] = delQuotes;
    }
    if(count == 2){
        data[noOfRows][count] = delQuotes;
        data[noOfRows][count+1] = "Delete";
    }
    }
    if(count < column.length) count++;
}