2016-05-20 5 views
1

これはこの質問の続きですJava resizing individual components of JPanel今回はフレームにはテーブルとJTextAreaしかありません。テーブルとJTextAreaの間に大きな空の領域が表示されます。これはmyPanel.javaです。JTableポリシー。表の下のgridBagLayoutパネルの空白スペース。

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextArea; 

public class myPanel extends JPanel { 
    public myPanel() { // Creating a vertical Box layout with five sections, placing jpanels there 

     //First panel with a table 

     String[] columnNames = {"First", "Second", "Third", "Fourth"}; 
     Object[][] data = {{"11", "12", "13", "Forteen"},{"21", "22", "23", "Twenty four"}}; 
     JScrollPane scrollPane1 = new JScrollPane(new JTable(data, columnNames)); 

     //Second panel with a text area 

     JTextArea ecnArea = new JTextArea(10, 20);   
     ecnArea.setText(""); 
     ecnArea.setName("Note"); 
     //ecnArea.setLineWrap(true); 
     //ecnArea.setWrapStyleWord(true); 
     JScrollPane myScrollBar = new JScrollPane(ecnArea); 

     //Placing everything in a container 
     this.setLayout(new GridBagLayout()); 

     GridBagConstraints c = new GridBagConstraints(); 
     c.anchor =GridBagConstraints.CENTER; 
     c.fill = GridBagConstraints.BOTH; 
     c.weightx = 0.5; 
     c.weighty=0.5; 
     c.gridx = 0; 
     c.gridy = 0; 
     this.add(scrollPane1, c);   
     c.gridy++; 
     this.add(myScrollBar, c); 
    } 
} 

なぜそれが表示されますか?どのようにそれを削除するには?

答えて

2

あなたはこれらの行を追加する必要があります:

JTable table = new JTable(data, columns); 
table.setPreferredScrollableViewportSize(new Dimension(500,60)); 
JScrollPane scrollPanel = new JScrollPane(table); 
//scrollPanel.getViewport().setBackground(color.WHITE; //optional, but looks nicer 

あなたは、同様の寸法や色をインポートする必要があります。

関連する問題