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

my JTable inside JScrollBar

另外,我对JPanel使用registering,并且JTable是记录已保存的验证,因此在registering之后将出现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();
        }

    }
}


这是我在JTable中调用main JFrame的地方

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管理视口,可选的垂直和水平滚动条以及可选的行和列标题视口。必须将可滚动客户端(如Jtable)放在ViewPort视图中,而您只需要调整scrollPane的大小,而不是客户端。

检查文档: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/

10-12 20:30