2014-07-10 65 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'到表格?),請參閱我的答案 –

相關問題