Table 不会显示 header 并调整大小
Table won't show header and resize
尝试制作如下所示的框架:
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. 方法参数列表的列表。问题是我无法让 header 字符串出现,而且我的 table 固定在某个大小上,不知道它是什么大小以及如何修改它。方法列表并不总是相同的。这取决于选择什么class。因此,如果有更多方法或方法名称 + 参数更长,我需要 table 自行调整大小以适应单元格。
所以现在我有两个问题:
1. Headers不会出现
2. 单元格无法正确调整大小
这是我得到的:Table so far
我已经添加了 ScrollPane,所以它应该可以工作,但没有。
我查看了 Whosebug 上的其他解决方案,但没有任何帮助。
已尝试改进您的代码。至少它现在显示 Table:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.beans.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
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<Method> methodList) {
gui(methodList);
}
public void gui(List<Method> methodList) {
/* Initialisation */
DefaultTableModel data = new DefaultTableModel();
data.addColumn(methodHeader);
data.addColumn(parametersHeader);
data.addColumn(resultHeader);
/* Add data to table */
for (int i = 0; i < methodList.size(); i++) {
Vector v = new Vector();
Method method = methodList.get(i);
v.add(method.getName());
String params = "";
for (Parameter param : method.getParameters()) {
params = param + ", " + param.getType() + " " + param.getName();
}
v.add(params);
v.add(method.getReturnType());
data.addRow(v);
}
JTable table = new JTable(data);
JScrollPane scrollPane = new JScrollPane(table);
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);
}
public static void main(String[] args) throws IntrospectionException {
new Dialog(Arrays.asList(Dialog.class.getMethods()));
}
}
尝试制作如下所示的框架: 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. 方法参数列表的列表。问题是我无法让 header 字符串出现,而且我的 table 固定在某个大小上,不知道它是什么大小以及如何修改它。方法列表并不总是相同的。这取决于选择什么class。因此,如果有更多方法或方法名称 + 参数更长,我需要 table 自行调整大小以适应单元格。 所以现在我有两个问题: 1. Headers不会出现 2. 单元格无法正确调整大小 这是我得到的:Table so far
我已经添加了 ScrollPane,所以它应该可以工作,但没有。 我查看了 Whosebug 上的其他解决方案,但没有任何帮助。
已尝试改进您的代码。至少它现在显示 Table:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.beans.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
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<Method> methodList) {
gui(methodList);
}
public void gui(List<Method> methodList) {
/* Initialisation */
DefaultTableModel data = new DefaultTableModel();
data.addColumn(methodHeader);
data.addColumn(parametersHeader);
data.addColumn(resultHeader);
/* Add data to table */
for (int i = 0; i < methodList.size(); i++) {
Vector v = new Vector();
Method method = methodList.get(i);
v.add(method.getName());
String params = "";
for (Parameter param : method.getParameters()) {
params = param + ", " + param.getType() + " " + param.getName();
}
v.add(params);
v.add(method.getReturnType());
data.addRow(v);
}
JTable table = new JTable(data);
JScrollPane scrollPane = new JScrollPane(table);
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);
}
public static void main(String[] args) throws IntrospectionException {
new Dialog(Arrays.asList(Dialog.class.getMethods()));
}
}