There are three panels inside a JFrame.
1. Parent panel (Using GridBaglayout)
i. Table panel 1 containing JTable (Using normal Border layout) & JScrollPane
ii.Button panel 2 containing "Add Row" and "Delete Row" button. (Using flow layout)
Requirements:
1. Add row and delete row buttons should add and delete table rows respectively.
2. The panel 1 size should increase/decrease with addition and deletion of rows. For this I have used: table.setPreferredScrollableViewportSize(table.getPreferredSize());
3. Scroll bars should appear only when panel 1 + panel 2 fills up the parent panel
我不能继续做table.setPreferredScrollableViewportSize(table.getPreferredSize()),每次添加新行时,滚动条将永远不会按以下规则显示:
Vertical scroll bar appear only when the component size (in this context, table) becomes larger than the view port size.
需要帮助才能找到何时停止执行table.setPreferredScrollableViewportSize(table.getPreferredSize())的条件,或者是否有不同的方法来满足上述要求。
package practice;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TableExample3 {
private DefaultTableModel tableModel;
public void createAndShowGui() {
JFrame frame = new JFrame("Table size test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
JPanel tablepanel = new JPanel(new BorderLayout());
JTable table = new JTable(4, 4);
table.setFillsViewportHeight(false);
tableModel = (DefaultTableModel) table.getModel();
JScrollPane pane = new JScrollPane(table);
tablepanel.add(pane);
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 1;
panel.add(tablepanel, gc);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
JButton addButton = new JButton("Add");
JButton deleteButton = new JButton("Delete");
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
Object[] data = null;
addButton.addActionListener((evt) -> {
tableModel.addRow(data);
// if (has to be some condition here when tablepanel + buttonPanel
// fills up the panel) {
table.setPreferredScrollableViewportSize(table.getPreferredSize());
panel.revalidate();
panel.repaint();
});
deleteButton.addActionListener((evt) -> {
int rowCount = tableModel.getRowCount();
tableModel.removeRow(rowCount - 1);
pane.getViewport().setPreferredSize(table.getPreferredSize());
panel.revalidate();
panel.repaint();
});
gc.gridy = 1;
gc.weighty = 1;
gc.anchor = GridBagConstraints.NORTH;
panel.add(buttonPanel, gc);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public static void main(String... args) {
TableExample3 example = new TableExample3();
SwingUtilities.invokeLater(() -> example.createAndShowGui());
}
}
答案 0 :(得分:2)
如果希望视口达到合理的高度(与初始行数无关) 您可以从
更改初始化table.setPreferredScrollableViewportSize(table.getPreferredSize());
类似
Dimension preferredTableSize = table.getPreferredSize();
preferredTableSize.height = 100;
table.setPreferredScrollableViewportSize(preferredTableSize);
但最重要的是,在"添加"的动作处理程序中和"删除"不要做任何事情
setPreferredSize
,setPreferredViewportSize
,revalidate
,repaint
,
因为它弊大于利。
addButton.addActionListener((evt) -> {
tableModel.addRow(data);
// nothing more needed here
});
deleteButton.addActionListener((evt) -> {
int rowCount = tableModel.getRowCount();
tableModel.removeRow(rowCount - 1);
// nothing more needed here
});
然后滚动条会出现并根据需要消失。