我第一次尝试学习一些JTable,我遇到的问题是我想要实现的目标。
最终我希望我的JTable位于边框布局的中间,列名显示在顶部,目前每个单元格都填充了单词“hey”。我还尝试在此表中添加一个空边框,以阻止表“拥抱”组件的边缘。
这是我目前的输出:
如您所见,我添加的边框显然没有显示,表格标题也没有显示。虽然细胞填充正确。
最终我想实现:
这是使用的代码:
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTable;
public class Leaderboard extends JPanel{
private final String[] columnNames = {"Name", "Score", "Length", "Time"};
public String[][] data = new String[10][4];
public JTable table;
public Leaderboard(){
this.setLayout(new BorderLayout());
for(int i = 0; i < 10; i++){
for(int j = 0; j < 4; j++){
data[i][j] = "hey";
}
}
table = new JTable(data, columnNames);
table.setBorder(BorderFactory.createEmptyBorder(150, 100, 40 , 40));
table.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(table);
this.add(table.getTableHeader(), BorderLayout.CENTER);
this.add(table, BorderLayout.CENTER);
this.add(table, BorderLayout.CENTER);
}
}
感谢。
答案 0 :(得分:2)
问题是BorderLayout
的中心元素扩展了。我建议在以下答案中使用GridBagLayout
与GridBagConstraint
或Box
类似:
Alex B answered "How do I add a margin outside the border of a component in Swing?"
这是一个有效的解决方案(gist):
import javax.swing.*;
import java.awt.*;
/**
* CenterBorderedJTable.java
*
* @author Marco
* @since 2014-07-10
*/
public class CenterBorderedJTable {
private static final String[] tableColumns = {"First Name", "Last Name", "Hobby", "Age", "Vegetarian?" };
private static final Object[][] tableData = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
/**
* Creates a GridBagConstraints with the specified vertical and horizontal margin.
* @param verticalMargin The vertical margin specifies how many pixels the top and bottom margins will be
* @param horizontalMargin The horizontal margin specifies how many pixels the left and right margins will be
* @return A GridBagConstraints with the specified vertical and horizontal margin.
*/
private static GridBagConstraints createGridBagConstaints(int verticalMargin, int horizontalMargin) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(verticalMargin, horizontalMargin, verticalMargin, horizontalMargin);
return constraints;
}
/**
* This boilerplate is from the Java tutorials:
* @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html">Creating the Demo Application</a>
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* This boilerplate is from the Java tutorials:
* @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html">Creating the Demo Application</a>
*/
private static void createAndShowGUI() {
JFrame f = new JFrame("Center Bordered JTable");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // exit JVM when window is closed
// GridBagLayout needed since we use GridBagConstraints to constrain the table
f.setLayout(new GridBagLayout());
// Create JTable with our sample data
JTable table = new JTable(tableData, tableColumns);
JScrollPane scrollPane = new JScrollPane(table); // Allow for scrolling if data is too long
scrollPane.setBorder(BorderFactory.createLineBorder(Color.CYAN)); // CYAN for easy visibility
table.setFillsViewportHeight(true);
// Add the scrollPane with the specified constraints to the window
f.add(scrollPane, createGridBagConstaints(50, 25));
// Pack and show the user!
f.pack();
f.setVisible(true);
}
}
答案 1 :(得分:-1)
答案是使用JScrollPane
&#39>。
需要此代码才能实现我想要的目标:
table = new JTable(data, columnNames);
//stops a user editing it
table.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBorder(BorderFactory.createEmptyBorder(150, 100, 40 , 40));
this.add(scrollPane,BorderLayout.CENTER);