0

在我的小项目中,我正在解析一些日志文件并在 JTable 上显示。在 Jtable 中有 2 列。

第二列是 for => 显示搜索语句 第一列是 for => 显示这句话在哪里(在哪个日志文件中?)

有我的结果屏幕的屏幕截图。

在此处输入图像描述

现在,我的问题是,我想把 JButtons 放在第一列。但是当我期待显示 Jbutton 时,我不能。

这是我的代码:

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ResultGui 
{
    public final static boolean RIGHT_TO_LEFT = false;
    private static JButton but;
    private static JTable table;
    public static void addComponentsToPane(Container contentPane,List<String> result, List<String>      logFiles) 
    {
       if (RIGHT_TO_LEFT)
       {
        contentPane.setComponentOrientation(
        ComponentOrientation.RIGHT_TO_LEFT);
       }
       contentPane.setLayout(new GridLayout(0,1));
       //list to array
       String[] data = result.toArray(new String[result.size()]);
       DefaultTableModel dm = new DefaultTableModel();
       Object [][] objectArray = new Object[logFiles.size()][result.size()];
       for(int i=0; i<logFiles.size(); i++)
       {
         but = new JButton("LogFile");
         but.setToolTipText(logFiles.get(i));;
         but.setOpaque(true);
         objectArray [i][0] = but;
         //objectArray [i][0] = logFiles.get(i);
         i++;
         objectArray [i][0] = "";
        }    
       for(int i=0; i<result.size(); i++)
       {
         objectArray[i][1] = result.get(i);
       } 
       dm.setDataVector(objectArray, new Object[] { "LOGFILES", "RESULTS" });
       //dm.addColumn("deneme", strArray2Vector(data));  
       table = new JTable(dm);
       TableColumn columnA = table.getColumn("LOGFILES");
       columnA.setMinWidth(30);
       columnA.setMaxWidth(100);
       JScrollPane scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       contentPane.add(scrollPane);
    }

    public static void createAndShowGUI(List<String> stringList, List<String> logFiles)   
    {
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Result Screen");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //Set up the content pane and components in GridLayout
      addComponentsToPane(frame.getContentPane(),stringList,logFiles);
      frame.setSize(1000,500);
      //frame.pack();
      frame.setVisible(true);
    }
}

我应该怎么做才能在我的表中成功创建 Jbutton ?任何示例、提示或建议都值得赞赏。

4

2 回答 2

1

我想把 JButtons 放在第一列

以您对任何其他列的方式为模型添加文本。然后,您可以使用Table Button Column来呈现和处理按钮点击。

于 2013-11-15T16:05:49.543 回答
-1

显示方式如下

table.setDefaultRenderer(JComponent.class, new MyTableCellRenderer());
table.setDefaultEditor(JComponent.class, new MYTableCellEditor());


class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
        return (JComponent)value;
    }
}

class PFTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    private static final long serialVersionUID = 1L;
    private JComponent item;

    @Override
    public Object getCellEditorValue() {
        return item;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
        item = (JComponent)value;
        item.setBackground(table.getSelectionBackground());
        return item;
    }       
}

最后

table.setModel(new DefaultTableModel() {    
    public boolean isCellEditable(int row, int column) {
            return column == 0;
    };
};

使用此 JTABLE 设置,您可以直接将 jbutton 作为单元格内容

于 2013-11-15T10:42:49.847 回答