如何使这个表格可以滚动?

huangapple go评论102阅读模式
英文:

How can I make this table scrollable?

问题

这里有一个表格,应该显示 Integer.MAX_VALUE 行,并具有滚动功能,但尽管我添加了滚动窗格,但没有滚动窗格。有人能解释需要更改/添加什么吗?

import java.awt.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

class DynamicTableModel extends AbstractTableModel {
    final static String[] headers = {"Seconds Since Epoch", "Formatted Timestamp"};

    private static String getPrettyStringFromEpochSeconds(int seconds) {
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm:ss a EEEE, MMMM d, yyyy", Locale.ENGLISH);
        String formattedDate = dateTime.format(formatter);
        return formattedDate;
    }

    @Override
    public int getRowCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public int getColumnCount() {
        return headers.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return (columnIndex == 0) ? rowIndex : getPrettyStringFromEpochSeconds(rowIndex);
    }
}

public class GoodTable extends JPanel {

    JTable jt;

    public GoodTable() {

        DynamicTableModel model = new DynamicTableModel();

        JTable table = new JTable(model);

        //table.setPreferredScrollableViewportSize(new Dimension(500,300));
        table.setFillsViewportHeight(true);

        JScrollPane js = new JScrollPane(table);
        js.setVisible(true);
        add(js);

    }

    public static void main(String[] args) {

        JFrame jf = new JFrame();
        GoodTable tab = new GoodTable();
        jf.setTitle("Table");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jf.setSize(screenSize.width, screenSize.height);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(tab);

    }

}
英文:

I have this table here that should be displaying Integer.MAX_VALUE many rows with scrolling functionality, but there is no scrollpane even though I added one. Can someone shed light on what needs to be changed/added?

import java.awt.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
class DynamicTableModel extends AbstractTableModel {
final static String[] headers = {"Seconds Since Epoch", "Formatted Timestamp"};
private static String getPrettyStringFromEpochSeconds(int seconds) {
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm:ss a EEEE, MMMM d, yyyy", Locale.ENGLISH);
String formattedDate = dateTime.format(formatter);
return formattedDate;
}
@Override
public int getRowCount() {
return Integer.MAX_VALUE;
}
@Override
public int getColumnCount() {
return headers.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return (columnIndex == 0) ? rowIndex : getPrettyStringFromEpochSeconds(rowIndex);
}
}
public class GoodTable extends JPanel{
JTable jt;
public GoodTable(){
DynamicTableModel model = new DynamicTableModel();
JTable table = new JTable(model);
//table.setPreferredScrollableViewportSize(new Dimension(500,300));
table.setFillsViewportHeight(true);
JScrollPane js=new JScrollPane(table);
js.setVisible(true);
add(js);
}
public static void main(String[] args) {
JFrame jf=new JFrame();
GoodTable tab= new GoodTable();
jf.setTitle("Table");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jf.setSize(screenSize.width, screenSize.height);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(tab);
}
}

答案1

得分: 0

使用Integer.MAX_VALUE时存在限制。

当我使用以下代码时,滚动工作正常:

public int getRowCount() {
    //return Integer.MAX_VALUE / 15; // 不起作用
    return Integer.MAX_VALUE / 16;
}

我使用了16,因为这是表格中每行的高度。

因此,最大的首选大小将是行数 * 16,因为首选高度也受整数值的限制。

英文:

There is a limitation when using Integer.MAX_VALUE.

Scrolling works when I use:

public int getRowCount() {
//return Integer.MAX_VALUE / 15; // doesn't work
return Integer.MAX_VALUE / 16;
}

I used 16 since that is the height of each row in the table.

So the maximum preferred size would be rows * 16, since the preferred height is also limited by an Integer value.

huangapple
  • 本文由 发表于 2020年8月7日 01:14:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63288653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定