虽然我已经阅读了所有相关的答案,但我仍然坚持如何在JTable中显示列标题的问题。我尝试添加JScrollPane,但它没有用。请帮忙。下面给出了代码:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
final JPanel panel_1 = new JPanel();
final JTable table = new JTable();
frame.getContentPane().add(panel, "name_762477085406274");
panel.setLayout(null);
JButton btnOk = new JButton("ok");
btnOk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
panel.setVisible(false);
panel_1.setVisible(true);
final Connection connect;
final Statement statement;
final ResultSet resultset;
try
{
Class.forName("com....");
connect = DriverManager.getConnection("jdbc:mysql...");
System.out.println("Connecting succesfully");
statement = connect.createStatement();
resultset = statement.executeQuery("select ProjName as 'Project Name', ProjID as 'Project ID' from projdetails");
//table.setModel(DBUtils.resultSetToTableModel(resultset));
ResultSetMetaData rsmetadata = resultset.getMetaData();
System.out.println(rsmetadata);
int columns = rsmetadata.getColumnCount();
System.out.println(columns);
DefaultTableModel dtm = new DefaultTableModel();
Vector columns_name = new Vector();
Vector data_rows = new Vector();
for ( int i =1; i<=columns;i++)
{
columns_name.addElement(rsmetadata.getColumnName(i));
System.out.println(columns_name);
}
dtm.setColumnIdentifiers(columns_name);
//System.out.println(resultset);
/*while(resultset.next())
{
Array lastname = resultset.getArray("ProjName");
//lastname =
System.out.println(lastname);
}*/
while ( resultset.next() )
{
data_rows = new Vector();
for(int j =1; j<=columns; j++)
{
data_rows.addElement(resultset.getString(j));
}
dtm.addRow(data_rows);
}
table.setModel(dtm);
/* final String lastName = resultset.getString("ProjName");
//final String lastName1 = resultset.getString("ProjID");
System.out.println(lastName);
//System.out.println(lastName1);
lbl_ShowAllProjects.setText(lastName);//+" "+lastName1);
System.out.println("\n\n\t\t");*/
}
catch(Exception e){
System.out.println("Cannot connect to database server");
e.printStackTrace();
}}
});
btnOk.setBounds(94, 117, 89, 23);
panel.add(btnOk);
frame.getContentPane().add(panel_1, "name_762480663629692");
panel_1.setLayout(null);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Project Name", "Project ID"
}
) {
boolean[] columnEditables = new boolean[] {
false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setResizable(false);
table.setBounds(25, 11, 399, 216);
panel_1.add(table);
//final JScrollPane scrollPane = new JScrollPane(table);
//JScrollPane scrollPane = new JScrollPane(table);
//panel_1.add(scrollPane);
//table.setFillsViewportHeight(true);
//frame.getContentPane().add(new JScrollPane(table));
// JScrollPane.setViewPortView(table);
}
答案 0 :(得分:2)
这可能是你的罪魁祸首:
table.setBounds(25, 11, 399, 216);
您通过人工放置和渲染人为地限制了JTable混乱的大小。所以,是的,确实将你的JTable放入JScrollPane,不,不要设置任何组件的界限。您应该避免在Java Swing程序中使用null
布局和setBounds(...)
,因为这会使非常不灵活的GUI,虽然在一个平台上看起来不错但在大多数其他平台或屏幕分辨率上看起来很糟糕并且很难更新和维护(正如你发现的那样)。
您可以在此处找到Swing教程的链接,包括布局管理器教程和其他Swing资源:Swing Info
此外,在更改容器的组件时,您需要拨打revalidate()
和repaint()
。更好的方法是使用CardLayout来交换组件。
例如,
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Foo {
private static final String PANEL_KEY = "panel";
private static final String PANEL_1_KEY = "panel 1";
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panel = new JPanel();
final JPanel panel_1 = new JPanel();
final JTable table = new JTable();
frame.getContentPane().add(panel, PANEL_KEY);
frame.getContentPane().add(panel_1, PANEL_1_KEY);
JButton btnOk = new JButton("ok");
panel.add(btnOk);
panel_1.setLayout(new BorderLayout());
final JScrollPane scrollPane = new JScrollPane(table);
panel_1.add(scrollPane);
table.setFillsViewportHeight(true);
frame.pack();
frame.setVisible(true);
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JPanel contentPane = (JPanel) frame.getContentPane();
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane, PANEL_1_KEY);
String[] colNames = { "Mon", "Tues", "Wed" };
DefaultTableModel dtm = new DefaultTableModel();
Vector<String> columns_name = new Vector<>();
for (String colName : colNames) {
columns_name.add(colName);
}
dtm.setColumnIdentifiers(columns_name);
table.setModel(dtm);
}
});
}
}