简体   繁体   English

如何删除JTable中所选行(在第一列中有复选框)的文本?

[英]How to strikethrough text of a selected row(having checkbox in first column) of JTable?

I have a Jtable with a checkbox in first column. 我有一个Jtable,第一列有一个复选框。 I want to strikethrough text of a row when the checkbox is selected. 我想在选中复选框时删除行的文本。 (eg same as we do in microsoft outlook when our task is complete.) I have tried using AttributeString, but not able to do it. (例如,当我们的任务完成时,就像我们在microsoft outlook中一样。)我尝试过使用AttributeString,但是无法做到。

Can anyone please guide me to solve it? 有人可以指导我解决吗?

String strStrike; 
AttributedString as; 

public void setTextStrikeThrough() {
    for(int r=0;r< taskcells.length;r++) {
        if (ttable.getValueAt(r,0).equals(Boolean.TRUE)) { 
            for(int c=2;c<7;c++) {
                strStrike+=taskcells[r][c-1]; 
            }//end inner for as=new
            AttributedString(strStrike); 
            as.addAttribute(TextAttribute.STRIKETHROUGH,
                TextAttribute.STRIKETHROUGH_ON);
            as.getIterator(); 
        }//end if 
    }//end for       
}

I am not getting exactly where to call this method. 我没准确到哪里调用这个方法。 I want to strikethrough text of a row when checkbox of that row has been checked. 我想在检查该行的复选框时删除行的文本。

I don't know that an ActionListener will work well for a JCheckBox in a JTable since the check box isn't a real button but rather a rendering of a checkbox. 我不知道ActionListener可以很好地用于JTable中的JCheckBox,因为复选框不是真正的按钮,而是复选框的渲染。 Perhaps playing with the table model will help. 也许玩桌面模型会有所帮助。 For instance you can use HTML to display a strike through of Strings displayed in table cells. 例如,您可以使用HTML来显示表格单元格中显示的字符串。 For instance below I create a custom TableModel that extends DefaultTableModel and holds rows with a Boolean object followed by objects of a TextWrapper class that I've created that changes its toString result depending on a boolean. 例如,下面我创建一个自定义TableModel,它扩展DefaultTableModel并保存带有Boolean对象的行,后跟我创建的TextWrapper类的对象,这些对象根据布尔值更改其toString结果。

eg, 例如,

import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class StrikeThroughRow {
   public static final Object[][] DATA = {{Boolean.TRUE, "Monday", "fe"},
      {Boolean.FALSE, "Tuesday", "fi"}, {Boolean.TRUE, "Wednesday", "fo"},
      {Boolean.FALSE, "Thursday", "fum"}, {Boolean.TRUE, "Friday", "foo"}};

   public StrikeThroughRow() {

   }

   private static void createAndShowUI() {
      JTable table = new JTable(new StrikeThroughModel(DATA));
      JScrollPane scrollpane = new JScrollPane(table);

      JFrame frame = new JFrame("StrikeThroughRow");
      frame.getContentPane().add(scrollpane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

class StrikeThroughModel extends DefaultTableModel {
   public StrikeThroughModel(Object[][] data) {
      super(new String[]{"Check", "Work Day", "Giant Speak"}, 0);
      for (int i = 0; i < data.length; i++) {
         Vector<Object> rowVect = new Vector<Object>();
         rowVect.add(data[i][0]);
         if (data[i].length > 1) {
            for (int j = 1; j < data[i].length; j++) {
               rowVect.add(new TextWrapper(data[i][j].toString(), (Boolean)data[i][0]));
            }
         }
         addRow(rowVect);
      }
   }

   @Override
   public Class<?> getColumnClass(int columnIndex) {
      if (columnIndex == 0) {
         return Boolean.class;
      }
      return super.getColumnClass(columnIndex);
   }

   @Override
   public void setValueAt(Object value, int row, int column) {
      if (column == 0) {
         for (int i = 1; i < getColumnCount(); i++) {
            TextWrapper textWrapper = (TextWrapper) getValueAt(row, i);
            textWrapper.setStrikeThrough((Boolean) value);
            fireTableCellUpdated(row, i);
         }
      }
      super.setValueAt(value, row, column);
   }
}

class TextWrapper {
   private String text;
   private boolean strikeThrough = false;

   public TextWrapper(String text) {
      this.text = text;
   }

   public TextWrapper(String text, boolean strikeThrough) {
      this(text);
      this.strikeThrough = strikeThrough;
   }

   @Override
   public String toString() {
      if (strikeThrough) {
         return "<html><strike>" + text + "</html></strike>"; 
      }
      return text;
   }

   public void setStrikeThrough(boolean strikeThrough) {
      this.strikeThrough = strikeThrough;
   }
}

I'm betting that there are better solutions including creating a custom renderer for your cells, but the code above offers a quick and dirty fix. 我认为有更好的解决方案,包括为您的单元格创建自定义渲染器,但上面的代码提供了快速而肮脏的修复。

Here is how you can create a "strike through font": 以下是如何创建“通过字体浏览”:

Map attributes = component.getFont().getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
component.setFont( new Font(attributes) );

One way to apply the font is to use the Table Row Rendering approach. 应用字体的一种方法是使用表行渲染方法。 Take a look at the background color example. 看一下背景颜色示例。 Instead of setting the background of the renderer you can set the Font. 您可以设置字体,而不是设置渲染器的背景。

Otherwise you would need to create a custom renderer for the columns in your table to use the appropriate Font. 否则,您需要为表中的列创建自定义渲染器以使用相应的Font。

Add a listener to the checkbox which will add/remove the from the label. 在复选框中添加一个监听器,用于添加/删除标签。 Here is an example of box and label maybe helpful: 以下是框和标签的示例可能有用:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class check {
  public static void main(String args[]) {

    JFrame frame = new JFrame("for bsm");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox box = new JCheckBox("check me");

    final JLabel label = new JLabel("<html>text</html>");
    label.setFont(new Font("helvetica", Font.PLAIN, 12));
    label.setForeground(new Color(50, 50, 25));

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
        if(abstractButton.getModel().isSelected())
            label.setText(label.getText().replace("<html>", "<html><strike>").replace("</html>", "</strike></html>"));
        else
            label.setText(label.getText().replace("<html><strike>", "<html>").replace("</strike></html>", "</html>"));
      }
    };

    box.addActionListener(actionListener);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout(10, 10));
    panel.add(label, BorderLayout.NORTH);
    panel.add(box, BorderLayout.SOUTH);
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM