2017-02-26 60 views
0
There are three panels inside a JFrame. 

    1. Parent panel (Using GridBaglayout) 
     i. Table panel 1 containing JTable (Using normal Border layout) & JScrollPane 
     ii.Button panel 2 containing "Add Row" and "Delete Row" button. (Using flow layout) 

    Requirements: 

    1. Add row and delete row buttons should add and delete table rows respectively. 
    2. The panel 1 size should increase/decrease with addition and deletion of rows. For this I have used: table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

    3. Scroll bars should appear only when panel 1 + panel 2 fills up the parent panel 

我不能繼續做table.setPreferredScrollableViewportSize(table.getPreferredSize()),每次一個新行添加別的,滾動條將不會出現按如下規則:顯示JTable中滾動時的JPanel大小爲全

Vertical scroll bar appear only when the component size (in this context, table) becomes larger than the view port size. 

需要幫助找到何時停止執行的條件table.setPreferredScrollableViewportSize(table.getPreferredSize()),或者是有不同的方式,爲上述要求。

package practice; 

    import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.FlowLayout; 
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 

    import javax.swing.BorderFactory; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    import javax.swing.JTable; 
    import javax.swing.SwingUtilities; 
    import javax.swing.table.DefaultTableModel; 

    public class TableExample3 { 

     private DefaultTableModel tableModel; 

     public void createAndShowGui() { 
      JFrame frame = new JFrame("Table size test"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JPanel panel = new JPanel(new GridBagLayout()); 

      JPanel tablepanel = new JPanel(new BorderLayout()); 
      JTable table = new JTable(4, 4); 
      table.setFillsViewportHeight(false); 
      tableModel = (DefaultTableModel) table.getModel(); 
      JScrollPane pane = new JScrollPane(table); 
      tablepanel.add(pane); 

      GridBagConstraints gc = new GridBagConstraints(); 
      gc.fill = GridBagConstraints.HORIZONTAL; 
      gc.gridx = 0; 
      gc.gridy = 0; 
      gc.weightx = 1; 
      panel.add(tablepanel, gc); 

      JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
      buttonPanel.setBorder(BorderFactory.createEtchedBorder()); 
      JButton addButton = new JButton("Add"); 
      JButton deleteButton = new JButton("Delete"); 
      buttonPanel.add(addButton); 
      buttonPanel.add(deleteButton); 

      table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

      Object[] data = null; 
      addButton.addActionListener((evt) -> { 

       tableModel.addRow(data); 

       // if (has to be some condition here when tablepanel + buttonPanel 
       // fills up the panel) { 

       table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

       panel.revalidate(); 
       panel.repaint(); 
      }); 

      deleteButton.addActionListener((evt) -> { 
       int rowCount = tableModel.getRowCount(); 
       tableModel.removeRow(rowCount - 1); 
       pane.getViewport().setPreferredSize(table.getPreferredSize()); 
       panel.revalidate(); 
       panel.repaint(); 

      }); 

      gc.gridy = 1; 
      gc.weighty = 1; 
      gc.anchor = GridBagConstraints.NORTH; 

      panel.add(buttonPanel, gc); 

      frame.getContentPane().add(panel); 
      frame.setVisible(true); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

     } 

     public static void main(String... args) { 
      TableExample3 example = new TableExample3(); 
      SwingUtilities.invokeLater(() -> example.createAndShowGui()); 
     } 
    } 

回答

2

如果你希望你的視口得到合理的高度(獨立行的初始數量) 你可以從

table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

改變你初始化像

Dimension preferredTableSize = table.getPreferredSize(); 
preferredTableSize.height = 100; 
table.setPreferredScrollableViewportSize(preferredTableSize); 

但最重要的是,在「添加」和「刪除」的操作處理程序中,不要執行任何操作,如 setPreferredSize,setPreferredViewportSize,revalidate,repaint, ,因爲它確實比弊大於利。

addButton.addActionListener((evt) -> { 
    tableModel.addRow(data); 
    // nothing more needed here 
}); 

deleteButton.addActionListener((evt) -> { 
    int rowCount = tableModel.getRowCount(); 
    tableModel.removeRow(rowCount - 1); 
    // nothing more needed here 
}); 

然後滾動條會根據需要出現和消失。

+0

添加/刪除按鈕應位於表格的最後一行之後。如果我這樣做, – user2596386

+0

添加/刪除按鈕應位於表格的最後一行之後。如果我像上面說的那樣設置了表視圖端口大小,那些按鈕在最後一行之後會顯得很遠。另外,只要添加新行,表格面板的尺寸應該增大以適應該行,直到整個面板填滿爲止(即通過增加tablepanel的大小並減小包含按鈕面板的較低網格單元的大小)。現在當添加下一行時,由於沒有空間,應該會出現一個滾動條。 – user2596386

+0

@ user2596386然後,最好忽略我的答案的前半部分 –