[英]How to create scrollable JTable
我真的不知道我在做什么錯。 我嘗試遵循其他人的代碼,但似乎無法理解如何向JTable添加滾動條
這是我到目前為止的內容:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JTextArea;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
public class TBB_SQLBuilder {
private JFrame frame;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TBB_SQLBuilder window = new TBB_SQLBuilder();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TBB_SQLBuilder() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 950, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea();
JButton button = new JButton("New button");
table = new JTable();
table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(table, GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button)))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
.addComponent(button))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(table, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
.addContainerGap(613, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
frame.add(new JScrollPane(table));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String getValue = textArea.getText();
String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/My.mdb;";
Connection conn;
try {
conn = DriverManager.getConnection(connectDB);
Statement st =conn.createStatement();
ResultSet rsHNum = st.executeQuery(getValue);
table.setModel(buildTableModel(rsHNum));
((DefaultTableModel)table.getModel()).fireTableDataChanged(); // show changes
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
public static DefaultTableModel buildTableModel(ResultSet rs1)
throws SQLException {
ResultSetMetaData metaData = rs1.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs1.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs1.getObject(columnIndex));
System.out.println(rs1.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
盡管您正在使用JScrollPane
放置表格,但實際上並沒有將滾動窗格添加到GroupLayout
。 相反,您將其添加到JFrame
-這是您的第一個錯誤。
因此,您應該首先創建滾動窗格,然后在其中添加表; 然后將滾動窗格添加到GroupLayout
,如下所示
JScrollPane scrollPane = new JScrollPane(table);
// Force the scrollbars to always be displayed
scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE)
因此,無論您在哪里執行addComponent(table
都應該使用addComponent(scrollPane
。現在,讓我們看兩行。在創建滾動窗格之后。
scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
這些將為您提供所需的精美外觀。 滾動條(禁用),即使沒有滾動內容。
只需創建JScrollPane
並將表添加到值中即可...請參見下面的示例。
public class MainView extends JFrame{
private JPanel mainPanel;
private JTable table;
private DefaultTableModel model_table;
private JScrollPane scroll_table;
public static void main(String[] args) {
MainView main = new MainView();
main.setVisible(true);
}
public MainView() {
mainPanel = new JPanel(null);
setSize(500, 500);
table = new JTable();
model_table = new DefaultTableModel();
model_table.addColumn("1");
model_table.addColumn("2");
model_table.addColumn("3");
model_table.addColumn("4");
table.setModel(model_table);
for(int i=0;i<10;i++){ // add value to table
Vector<String> r = new Vector<String>();
r.addElement("a");
r.addElement("b");
r.addElement("c");
r.addElement("d");
model_table.addRow(r);
}
scroll_table = new JScrollPane(table); // add table to scroll panel
scroll_table.setBounds(5, 10, 300, 150);
scroll_table.setVisible(true);
mainPanel.add(scroll_table);
this.add(mainPanel);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.