表格不会显示标题和调整大小

时间:2018-05-03 10:45:03

标签: java swing jframe jtable jtableheader

尝试制作看起来像这样的框架: Class methods table

这是我到目前为止写的:

public class Dialog {

private JFrame frame;
private JPanel labelPanel;
private JPanel buttonPanel;
private JButton button1;
private JButton button2;
private JTable table;
private JLabel label;

private static final String methodHeader = "Method";
private static final String parametersHeader = "Parameters";
private static final String resultHeader = "Result";

public Dialog(List<MethodDescriptor> methodList) {
    gui(methodList);
}

public void gui(List<MethodDescriptor> methodList) {

    /* Initialisation */
    DefaultTableModel headerData = new DefaultTableModel();
    DefaultTableModel data = new DefaultTableModel();
    headerData.addColumn(methodHeader);
    headerData.addColumn(parametersHeader);
    headerData.addColumn(resultHeader);

    /* Add data to table */
    for (int i = 0; i < methodList.size(); i++) {
        int itterator = 1;
        String methodName = methodList.get(i).getMethodName() + "(";
        List<ParameterDescriptor> paramList = methodList.get(i).getParamsList();
        for (ParameterDescriptor param : paramList) {
            methodName += param.getParamType() + " " + param.getParamName();
            if (itterator++ != paramList.size()) {
                methodName += ", ";
            }
        }
        methodName += ")";
        headerData.addRow(new Object[] { methodName });

        Vector v = new Vector();

        data.addRow(v);
    }

    JTable table = new JTable(data) {
    @Override
               public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
           Component component = super.prepareRenderer(renderer, row, column);
           int rendererWidth = component.getPreferredSize().width;
           TableColumn tableColumn = getColumnModel().getColumn(column);
           tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
           return component;
        }
    };
    //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable rowHeader = new JTable(headerData) {

    @Override
       public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
           Component component = super.prepareRenderer(renderer, row, column);
           int rendererWidth = component.getPreferredSize().width;
           TableColumn tableColumn = getColumnModel().getColumn(column);
           tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
           return component;
        }
    };
    rowHeader.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    rowHeader.setIntercellSpacing(new Dimension(0, 0));
    Dimension d = rowHeader.getPreferredScrollableViewportSize();
    d.width = rowHeader.getPreferredSize().width;
    rowHeader.setPreferredScrollableViewportSize(d);
    rowHeader.setRowHeight(table.getRowHeight());

    JScrollPane scrollPane = new JScrollPane(table);

    scrollPane.setRowHeaderView(rowHeader);

    JTableHeader corner = rowHeader.getTableHeader();
    corner.setReorderingAllowed(false);
    corner.setResizingAllowed(false);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);


    // rowHeader.setDefaultRenderer(Object.class, new RowHeaderRenderer());

    frame = new JFrame();
    frame.setLayout(new BorderLayout());

    // table = new JTable(mColumns, mColumnNames);

    buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    labelPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));

    button1 = new JButton("Cancel");
    button2 = new JButton("Ok");
    label = new JLabel(
            "Please add parameters and expected values for listed methods you would like to generate Unit tests for");

    /* Adding components to panel */
    buttonPanel.add(button1);
    buttonPanel.add(button2);
    labelPanel.add(label);

    /* Adding panels to frame */
    frame.add(labelPanel, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.add(buttonPanel, BorderLayout.SOUTH);

    frame.setTitle("Unit test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();

    frame.setVisible(true);

}

好的,这个methodList,是一个包含1.方法名称2的列表。方法参数列表。问题是我无法显示标题字符串,并且我的表格已固定在某个大小上,不知道它的大小以及如何修改它。方法列表并不总是相同。这取决于选择的课程。因此我需要我的表自我调整大小,如果有更多的方法或方法+参数的名称更长,以适应单元格。 所以现在我有两个问题: 1.标题不会出现 2.细胞不能正确调整大小 这就是我得到的:Table so far

我已经添加了ScrollPane,所以它应该可以工作,但事实并非如此。 我在stackoverflow上查看了其他解决方案,但没有任何帮助。

1 个答案:

答案 0 :(得分:0)

试图改进您的代码。至少它现在显示一个表:

io.vertx.reactivex