2014-07-10 29 views
1

我想第一次学习一些JTable,并且遇到了我想要实现的问题。JTable,为什么我的表格不按预期显示

最终,我希望我的JTable在我的边界布局的中间,列名显示在顶部,现在每个单元格填充单词“嘿”。我也尝试在该表中添加一个空白边框以阻止表格“拥抱”组件边缘。

这是我的电流输出:

enter image description here

正如你所看到的,我已经添加了边框显然没有显示,也没有表头。单元格被正确填写。

最终我想要实现:

enter image description here

这是所使用的代码:

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


    } 

} 

感谢。

回答

2

问题是BorderLayout的中心元素扩大。我会建议使用GridBagLayoutGridBagConstraint S或A 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

答案是使用JScrollPane的。需要

此代码来实现我想要的东西:

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); 
+1

很高兴见到你想通这个问题在你自己!如果你正在寻找一个更强大的解决方案(也许将来你想添加一个'EtchedBorder'到表格?),请参阅我的答案 –

相关问题