2017-10-10 228 views
1

我試圖將JTable放入JScrollPane之內。 JScrollPaneJPanel裏面,這是JTabbedPane的一部分。將JTable添加到JTabbedPane中的JScrollPane

下面是代碼:

JScrollPane scrollPane = new JScrollPane(); 
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);    

    JPanel pnlMaxMinAvg1 = new JPanel(); 
    scrollPane.setViewportView(pnlMaxMinAvg1); 
    tabbedPane.addTab("MaxMinAvg1", null, pnlMaxMinAvg1, null); 
    pnlMaxMinAvg1.setLayout(new BorderLayout(0,0)); 

table = new JTable(); 
     table.setModel(new DefaultTableModel(
      new Object[][] {{"V_L1_N_RMS_AVG", null} 
}, 
      new String[] { 
       "Variable", "Value" 
      } 
     )); 
     table.setBounds(178, 11, 200,300); 

scrollPane.add(table); 

當我運行此,不顯示table。如果我設置:

pnlMaxMinAvg1.add(table); 

然後table可見。

爲什麼?

回答

1

您的JPanel,pnlMaxMinAvg1,在您使其成爲scrollPane的視口視圖時爲空。將table添加到pnlMaxMinAvg1會爲滾動窗格顯示一些非空的內容。此外,請避免setBounds()並讓佈局完成工作。在下面的例子中,滾動窗格的視圖被顯式設置爲表格,就像你的片段一樣,但是你可以在滾動窗格的構造函數中指定它。

image

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 

/** 
* @see https://stackoverflow.com/q/46669145/230513 
*/ 
public class TableTest { 

    private void display() { 
     JFrame f = new JFrame("TableTest"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JTable table = new JTable(); 
     table.setModel(new DefaultTableModel(
      new Object[][]{{"V_L1_N_RMS_AVG", null}}, 
      new String[]{"Variable", "Value"} 
     )); 
     JScrollPane jsp = new JScrollPane(); 
     jsp.setViewportView(table); 
     JTabbedPane jtp = new JTabbedPane(); 
     jtp.add("TableTest", jsp); 
     f.add(jtp); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new TableTest()::display); 
    } 
} 

在此變化中,我已經添加N行數據和重寫的Scrollable方法,getPreferredScrollableViewportSize(),如圖here。調整封閉窗口的大小以查看它如何影響滾動條。

image2

import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 

/** 
* @see https://stackoverflow.com/q/46669145/230513 
*/ 
public class TableTest { 

    private static final int N = 16; 

    private void display() { 
     JFrame f = new JFrame("TableTest"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     DefaultTableModel model = new DefaultTableModel(
      null, new String[]{"Variable", "Value"} 
     ); 
     JTable table = new JTable(model) { 

      @Override 
      public Dimension getPreferredScrollableViewportSize() { 
       return new Dimension(256, getRowHeight() * N/2); 
      } 
     }; 
     for (int i = 0; i < N; i++) { 
      model.addRow(new Object[]{"V_L1_N_RMS_AVG" + i, String.valueOf(i)}); 
     } 
     JScrollPane jsp = new JScrollPane(); 
     jsp.setViewportView(table); 
     JTabbedPane jtp = new JTabbedPane(); 
     jtp.add("TableTest", jsp); 
     f.add(jtp); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new TableTest()::display); 
    } 
}