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. 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.

谁能指导我解决吗?

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       
}

我没有确切了解采用这种方法的情况。 在检查该行的盒子时,我想通过行文。

最佳回答

我不知道,“行动生活者”会很好地为JCheckBox提供一种JTable,因为检查箱是真的纽子,而是提供检查箱。 或许与表格模式一道发挥作用。 例如,你可以通过在桌子上展示的“强食”显示罢工。 例如,在我下面,我制作了一份习俗表,扩展了DefaultTableModel,并保留了带有诱杀剂的浏览器,接着是我制作成像器的物体,根据ool子改变其成像结果。

例如,

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;
   }
}

我很想说,有更好的解决办法,包括为你的囚室设立习俗使器,但上述守则提供了快速和 d脏的固定装置。

问题回答

这里,你如何能够创造“体力”:

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

适用字体的一个办法是使用。 表 Row Rendering办法。 探讨背景图示。 不要确定使者的背景,你就可以把“F”确定下来。

否则,你就需要为您的表格中各栏打下一个惯例使器,以使用适当的Fon。

在检查箱中添加一名听众,以添加/删除标签。 方框和标签的例子可能有助于:

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);
  }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签