2012-01-04 54 views
0

我想調整在TabPanel視圖中scrollPanels中的兩個JTable。我設法將幀的大小設置爲12000,5000,但Jtable/scrollPanel不玩球。調整一個Jtable來填充JFrame,並仍然滾動

我發現一些代碼來設置表的是工作,但不是填充的JFrame它只是在表的底部增加了一個滾動條列的尺寸/ scrollPanel

誰能拿出一個如何讓表格填充框架並仍然滾動?

的表文件是:

public class ViewInTablePET extends JPanel { 
private String[][] rowData = new String[0][]; 
private String columnNames[] = {"Shop", "Type", "price", "dateAcquired", "notes"}; 
private DefaultTableModel tableMod = new DefaultTableModel(rowData, columnNames); 
private JTable table = new JTable(tableMod); 

public ViewInTablePET() { 

    String bob; 
    String[] dan; 

    for (Integer i = 0; i < AppModel.getArrayListPet().size(); i++) { 
     bob = AppModel.getArrayListPet().get(i).toString(); 
     dan = AppModel.getStringAsArray(bob, "\t"); 
     tableMod.insertRow(i, new Object[]{dan[0], dan[1], dan[2], dan[3], dan[4]}); 
    } 
    JScrollPane scrollPane = new JScrollPane(table); 
    this.add(scrollPane, BorderLayout.CENTER);  
} 
} 

FRAM的文件是:

public class ViewInTabbs extends JFrame { 

JLabel Average = new JLabel("Click a table row to get the average price"); 

public ViewInTabbs() { 

    JTabbedPane tabs =    new JTabbedPane(); 
    ViewInTablePetShope petShop = new ViewInTablePetShope(); 
    ViewInTablePET pet =   new ViewInTablePET(); 

    tabs.add("Pet Shope", (JPanel) petShop); 
    tabs.add("Pets in the shop", (JPanel) pet); 
    this.setTitle("Tabb View of Data"); 
    this.add(tabs, BorderLayout.CENTER); 
    this.setSize(12000, 5000); 
    this.setVisible(true); 
    this.add(Average, BorderLayout.SOUTH); 
} 
public void setAverage(String ave) { 
    this.Average.setText(ave); 
} 
} 

回答

2

稀釋..二讀:罪魁禍首是ViewInTablePET的LayoutManager的缺省爲FlowLayout。更改爲BorderLayout的

public ViewInTablePET() { 
    super(new BorderLayout()); 
    // your code 
    ... 
} 
2

在你​​類,你與約束BorderLayout.CENTER添加JScrollPane,但我沒有看到該類中setLayout(new BorderLayout())電話。由於JPanel默認情況下具有FlowLayout,因此不會產生預期的效果。但是,當您將佈局設置爲BorderLayout時,它應該可以工作