gpt4 book ai didi

java - 调整 JScrollBar 内 JTable 的大小

转载 作者:行者123 更新时间:2023-12-01 09:31:03 26 4
gpt4 key购买 nike

有人可以告诉我如何调整 JScrollBar 内的 JTable 的大小吗?

my JTable inside JScrollBar

另外,我有用于注册JPanel,而JTable是在注册<之后保存记录的验证 JTable 将出现。但是,如果我将大小设置为 JTable 甚至 JScrollPane 的大小,则我的 JTable 中将不会出现任何内容。

这是我的 JTable 的代码:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public void setColumns(){
ViewTablePanel.tblModel=new DefaultTableModel();
ViewTablePanel.tblModel.setColumnIdentifiers(column);
}

public class ViewTablePanel extends JPanel{
static JTable tblVoters;
static DefaultTableModel tblModel;

ViewTablePanel(){
setBorder(BorderFactory.createTitledBorder("Voter's Information Table"));

data=new Database();
setColumns();
data.retrieveRecords();

ViewTablePanel.tblVoters=new JTable(ViewTablePanel.tblModel);


add(new JScrollPane(ViewTablePanel.tblVoters));
}
}
<小时/>
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
public class Database {
static String [] column={"VOTERS NUMBER",
"LAST NAME", "FIRST NAME",
"MIDDLE NAME", "GENDER",
"STATUS", "WEIGHT",
"HEIGHT", "BIRTH MONTH",
"BIRTH DAY", "BIRTH YEAR",
"BIRTH CITY/MUN.", "BIRTH PROVINCE",
"PROVINCE", "CITY/MUN", "BARANGAY",
"HOUSE NO./ST.",
"# MONTHS IN CITY RESIDENCE",
"# YEARS IN CITY RESIDENCE",
"# YEARS IN PHIL. RESIDENCE",
"OCCUPATION", "TIN NUMBER",
"CITIZENSHIP", "CITIZEN SINCE",
"FIRST-MOTHER", "MIDDLE-MOTHER",
"LAST-MOTHER", "FIRST-FATHER",
"MIDDLE-FATHER", "LAST-FATHER"};
static Vector rows;
static File fileNew;
static FileWriter fileWrite;
static FileReader fileRead;
static Scanner read;

public Database() {
fileNew=new File("Voters.txt");
fileWrite=null;
fileRead=null;
read=null;
}
public void retrieveRecords(){
String holdStr="";
try {
fileRead=new FileReader(fileNew);
read=new Scanner(fileRead);

while(read.hasNext()){
holdStr+=read.nextLine()+"\n";
}
read.close();

StringTokenizer strToken=new StringTokenizer(holdStr, "/");

while(strToken.hasMoreElements()){
rows=new Vector();
for(int i=0; i<column.length; i++){
rows.add(strToken.nextElement());
}
ViewTablePanel.tblModel.addRow(rows);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

}
}

这里是我调用要在主JFrame中调用的JTable的地方

import javax.swing.*;
import java.awt.*;

public class ViewPanel extends JPanel{

ViewPanel(){
setName("panelView");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(880,70));

add(new JScrollPane(new ViewTablePanel()), BorderLayout.CENTER);
add(new ViewButtonsPanel(), BorderLayout.SOUTH);
}
}

请帮忙和任何建议,非常感谢:)

最佳答案

JScrollPane 管理视口(viewport)、可选的垂直和水平滚动条以及可选的行和列标题视口(viewport)。可滚动客户端(如 Jtable)必须放置在 ViewPort View 中,并且您只需调整滚动 Pane 而不是客户端的大小。

检查文档:JScrollPane doc

public class ViewTablePanel extends JPanel{
static JTable tblVoters;
static DefaultTableModel tblModel;

ViewTablePanel(){
setBorder(BorderFactory.createTitledBorder("Voter's Information Table"));

data=new Database();
setColumns();
data.retrieveRecords();

ViewTablePanel.tblVoters=new JTable(ViewTablePanel.tblModel);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(x,y,width,height);
add(scrollPane);
scrollPane.setViewportView(ViewTablePanel.tblVoters);
}
}

希望对你有帮助

关于java - 调整 JScrollBar 内 JTable 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397101/

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