gpt4 book ai didi

java - 如何创建可滚动的 JTable

转载 作者:行者123 更新时间:2023-12-01 16:52:26 25 4
gpt4 key购买 nike

我真的不知道我做错了什么。我尝试过遵循其他人的代码,但我似乎无法理解如何向我的 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 来放置表格,但您并没有真正将滚动 Pane 添加到 GroupLayout 中。相反,您将其添加到 JFrame 中 - 这是您的第一个错误。

所以你应该先创建scrollpane,然后在其中添加table;然后将滚动 Pane 添加到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)。现在,让我们看两行。就在创建滚动 Pane 之后。

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

这些将为您提供您正在寻找的精美外观。即使没有任何内容可滚动,滚动条(禁用)也是如此。

关于java - 如何创建可滚动的 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37339279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com