Ich weiß wirklich nicht, was ich falsch mache. Ich habe versucht, Code folgende anderer Leute, aber ich kann nicht scheinen zu verstehen, wie eine Bildlaufleiste hinzufügen, um meine JTableWie scrollbar zu erstellen JTable
Hier ist, was ich bisher:
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);
}
Also, was ist Ihre Frage? Ich frage nur, weil dein Code gut aussieht. Es fügt * JScrollPane * zum * JFrame * hinzu und dann wird * JTable * zu * JScrollPane * hinzugefügt. – Alexander
Für bessere Hilfe, früher, ein [MCVE] oder [kurze, unabhängige, korrekte Beispiel] (http://www.sscce.org/). –
Ich habe mehr Code hinzugefügt. Ich weiß wirklich nicht, wo das Problem liegt, also musste ich alle Layout-Sachen hinzufügen, falls das das Problem war. Hoffentlich wird nicht von einem Code-Dump beschuldigt werden. –