英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论