我有 2 个
JTable
,都放置在自己的 FastScrollPane
中,并将它们的滚动窗格放置到 JPanel
中,然后将它们都放置到 JPanel
中。我在.setPreferredScrollableViewportSize(dimensionPrameters)
中放置了不同的对象作为参数,但是,两个表往往具有相同的大小。
我怎样才能让他们完全独立?
代码:
Dimension dimensionPayments = new Dimension(600, 60);
table1.setPreferredScrollableViewportSize(table1.getPreferredSize());
table2.setPreferredScrollableViewportSize(dimensionPayments);
FastScrollPane fastScrollPane1 = new FastScrollPane(table1);
fastScrollPane1.setAlignmentX(JComponent.LEFT_ALIGNMENT);
FastScrollPane fastScrollPane2 = new FastScrollPane(table2);
fastScrollPane2.setAlignmentX(JComponent.LEFT_ALIGNMENT);
firstPanel.add(fastScrollPane1);
firstPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
secondPanel.add(fastScrollPane2);
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
allPanel.add(firstPanel);
allPanel.add(secondPanel);
在图像中,我试图避免的效果被圈出。
为了获得最小的可重现示例,请检查以下代码:
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
public class CfgTrafficProvidersBoxTest extends JPanel {
private static final long serialVersionUID = 1500904348023955658L;
private JTable changeTrafficProviderTable;
private JTable changeTrafficProviderPaymentsTable;
private final String PRICE_FORMAT = "#.####";
public JButton addBtn;
public JButton addPaymentBtn;
public JButton saveBtn;
public JLabel addTrafficProviderServiceLabel;
public JLabel addTrafficProviderPaymentLabel;
private long editId = -1;
CfgTrafficProvidersBoxTest() {
createGUIElements();
}
public void createGUIElements() {
JPanel allAndSavePanel = new JPanel();
allAndSavePanel.setLayout(new BoxLayout(allAndSavePanel, BoxLayout.Y_AXIS));
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(0,1));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(0,1));
firstPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
addTrafficProviderServiceLabel = new JLabel("Add Service");
addTrafficProviderPaymentLabel = new JLabel("Add Payment");
firstPanel.add(addTrafficProviderServiceLabel);
allPanel.add(firstPanel);
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
changeTrafficProviderTable = new JTable();
changeTrafficProviderTable.setModel(new TableModel() {
@Override
public int getRowCount() {
return 0;
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public String getColumnName(int columnIndex) {
return "name"+columnIndex;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return "aa";
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
});
JScrollPane fastScrollPane = new JScrollPane(changeTrafficProviderTable);
fastScrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT);
secondPanel.add(fastScrollPane);
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
Dimension dimension = new Dimension(600, 20);
Dimension dimensionPayments = new Dimension(600, 60);
changeTrafficProviderTable.setPreferredScrollableViewportSize(dimension);
addBtn = new JButton("X");
addBtn.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
secondPanel.add(addBtn);
allPanel.add(secondPanel);
saveBtn = new JButton("SAVE");
changeTrafficProviderTable.setCellSelectionEnabled(false);
changeTrafficProviderTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
JPanel thirdPanel = new JPanel();
thirdPanel.setLayout(new GridLayout(0,1));
thirdPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
thirdPanel.add(addTrafficProviderPaymentLabel);
allPanel.add(thirdPanel);
JPanel forthPanel = new JPanel();
forthPanel.setLayout(new BoxLayout(forthPanel, BoxLayout.X_AXIS));
changeTrafficProviderPaymentsTable = new JTable();
changeTrafficProviderPaymentsTable.setPreferredScrollableViewportSize(dimensionPayments);
changeTrafficProviderPaymentsTable.setModel(new TableModel() {
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public String getColumnName(int columnIndex) {
return "name"+columnIndex;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return "aa";
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
});
JScrollPane fastScrollPanePayments = new JScrollPane(changeTrafficProviderPaymentsTable);
fastScrollPanePayments.setAlignmentX(JComponent.LEFT_ALIGNMENT);
fastScrollPanePayments.setVerticalScrollBar(fastScrollPane.getVerticalScrollBar());
forthPanel.add(fastScrollPanePayments);
addPaymentBtn = new JButton("XX");
addPaymentBtn.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
forthPanel.add(addPaymentBtn);
allPanel.add(forthPanel);
allAndSavePanel.add(allPanel);
saveBtn.setAlignmentX(Component.LEFT_ALIGNMENT);
Box b = Box.createHorizontalBox();
b.add(saveBtn);
b.add(Box.createHorizontalGlue());
allAndSavePanel.add(b);
this.add(allAndSavePanel);
}
public static void main(final String[] args) {
final JFrame frame = new JFrame();
frame.getContentPane().add(new CfgTrafficProvidersBoxTest());
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
和图像:
注意
Dimension dimension = new Dimension(600, 20);
Dimension dimensionPayments = new Dimension(600, 60);
适用于两个不同的表
[1]: https://i.stack.imgur.com/X3BFk.png
不知道为什么代码要实现TableModel。举一个简单的例子,只需一条语句即可创建一个 TableModel。只需使用
table.setModel( new DefaultTabelModel(1, 5) );
另外,不知道为什么要创建一个面板来保存每个标签。您在评论中指出您需要一个面板,因为表格右侧有一个按钮。好吧,标签没有按钮,所以不需要面板。保持代码简单。跟踪您创建的所有不必要的面板太困难了。
但是,两张桌子的尺寸往往相同。
这是你的基本代码:
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(0,1));
....
allPanel.add(firstPanel);
...
allPanel.add(secondPanel);
...
allPanel.add(thirdPanel);
...
allPanel.add(forthPanel);
allPanel 使用 GridLayout。 GridLayout 将使所有组件具有相同的大小。所以每个组件的大小将是最大表的大小。
这也是标签和表格之间有空间的原因。
不要使用网格布局。而是使用垂直 BoxLayout 或 GridBagLayout。