I have 2 JTable
s, both placed in their own FastScrollPane
s, and them scroll pane placed into JPanel
and then they both are placed into JPanel
. I put different objects as the parameters in .setPreferredScrollableViewportSize(dimensionPrameters)
, however, both tables tend to have the same size.
How I can make them fully independent on each other?
The code:
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);
In the image the effect I'm trying to avoid is encircled.
In order to get minmal reproducible example, please check the below code:
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);
}
});
}
}
and the image:
note
Dimension dimension = new Dimension(600, 20);
Dimension dimensionPayments = new Dimension(600, 60);
applied to both different tables
[1]: https://i.sstatic.net/X3BFk.png
No idea why the code implements TableModel. For a simple example it is one statement to create a TableModel. Just use
table.setModel( new DefaultTabelModel(1, 5) );
Also, no idea why you create a panel to hold each label. You stated in your comment you need a panel because the table has a button on the right. Well, the label doesn't have a button so there is no need for the panel. Keep the code simple. It is too hard to keep track of all then unnecessary panels that you have created.
however, both tables tend to have the same size.
This is your basic code:
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(0,1));
....
allPanel.add(firstPanel);
...
allPanel.add(secondPanel);
...
allPanel.add(thirdPanel);
...
allPanel.add(forthPanel);
The allPanel uses a GridLayout. The GridLayout will make all components the same size. So the size of each component will be the size of the largest table.
This is also why you have space between the labels and the tables.
Don't use a GridLayout. Instead use a vertical BoxLayout or a GridBagLayout.