I have a problem with the JTable and scrollbars i tried several things now it's ok only if the frame is at full screen height see the screenshots below :
What i tried to achieve this :
Dimension dim = new Dimension(400,350);
tableProd.setSize(dim);
tableProd.setPreferredSize(dim);
tableProd.setMinimumSize(dim);
tableProd.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableProd.setModel(modeleDatas);
tableProd.setPreferredScrollableViewportSize(dim);
JScrollPane scrollTab=new JScrollPane(tableProd);
scrollTab.setPreferredSize(dim);
panelProd.add(scrollTab, gbc);
even extends JTable with this JSTable :
public class JSTable extends JTable {
/**
* Returns false to indicate that horizontal scrollbars are required
* to display the table while honoring perferred column widths. Returns
* true if the table can be displayed in viewport without horizontal
* scrollbars.
*
* @return true if an auto-resizing mode is enabled
* and the viewport width is larger than the table's
* preferred size, otherwise return false.
* @see Scrollable#getScrollableTracksViewportWidth
*/
public boolean getScrollableTracksViewportWidth() {
if (autoResizeMode != AUTO_RESIZE_OFF) {
if (getParent() instanceof JViewport) {
return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
}
}
return false;
}
}
UI code :
public class Fenetre extends JFrame {
private JPanel panels = new JPanel(new CardLayout());
public Fenetre(){
this.setTitle("Gestion Station Phone");
this.setSize(900, 800);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initMenu();
this.setJMenuBar(menuBar);
GridBagConstraints gbcPage = new GridBagConstraints();
cardBD.setLayout(new GridBagLayout());
titrePage.setFont(new Font("Serif",Font.BOLD,22));
gbcPage.gridx=0;
gbcPage.gridy=0;
gbcPage.gridwidth=3;
cardBD.add(titrePage,gbcPage);
GridBagConstraints gbc = new GridBagConstraints();
panelActions.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
panelActions.add(buttonAdd,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets=new Insets(10,0,0,0);
panelActions.add(buttonUpdate,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets=new Insets(10,0,0,0);
panelActions.add(buttonDel,gbc);
gbcPage.gridx=0;
gbcPage.gridy=1;
Border border = new LineBorder(Color.BLACK);
Border margin = new EmptyBorder(10,10,10,10);
panelActions.setBorder(new CompoundBorder(border,margin));
panelActions.setBackground(Color.white);
gbcPage.gridwidth=1;
gbcPage.insets=new Insets(10,0,0,0);
cardBD.add(panelActions,gbcPage);
panelInfosRow.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets=new Insets(0,0,0,0);
labelId.setText("ID");
panelInfosRow.add(labelId,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets=new Insets(2,0,0,0);
textId.setPreferredSize(new Dimension(80,20));
panelInfosRow.add(textId,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets=new Insets(15,0,0,0);
panelInfosRow.add(label1,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets=new Insets(2,0,0,0);
text1.setPreferredSize(new Dimension(120,20));
panelInfosRow.add(text1,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets=new Insets(2,0,0,0);
combo.setVisible(false);
panelInfosRow.add(combo,gbc);
.....
gbcPage.gridx=1;
gbcPage.gridy=1;
gbcPage.gridwidth=1;
gbcPage.insets=new Insets(10,20,0,0);
cardBD.add(panelInfosRow,gbcPage);
panelProd.setLayout(new GridBagLayout());
Dimension dim = new Dimension(400,350);
tableProd.setSize(dim);
tableProd.setPreferredSize(dim);
tableProd.setMinimumSize(dim);
tableProd.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableProd.setModel(modeleDatas);
tableProd.setPreferredScrollableViewportSize(dim);
gbc.gridx = 0;
gbc.gridy = 0;
panelProd.add(labelProd, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JScrollPane scrollTab=new JScrollPane(tableProd);
scrollTab.setPreferredSize(dim);
panelProd.add(scrollTab, gbc);
gbcPage.gridx=2;
gbcPage.gridy=1;
gbcPage.gridwidth=1;
cardBD.add(panelProd,gbcPage);
logo = new JLabel(new ImageIcon("images/logo_station_phone2.png"));
cardHome.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
cardHome.add(logo,gbc);
gbc.insets = new Insets(10,0,0,0);
gbc.gridx = 0;
gbc.gridy = 1;
cardHome.add(new JLabel("Choix point de vente "),gbc);
gbc.insets = new Insets(2,0,0,0);
gbc.gridx = 0;
gbc.gridy = 2;
MagasinDAO magasinBDD = new MagasinDAO(daoFactory);
Object[][] magasins = magasinBDD.trouverMagasins();
choix_magasin.addItem(new ObjectIdValue(-1, ""));
if(prefs.getLong("id_point",-1)==-1)
choix_magasin.setSelectedIndex(0);
for(int i= 0 ; i<magasins.length;i++){
choix_magasin.addItem(new ObjectIdValue((long) magasins[i][0], (String) magasins[i][1]));
if(prefs.getLong("id_point",-1)==(long) magasins[i][0])
choix_magasin.setSelectedIndex(i+1);
}
choix_magasin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
prefs.putLong("id_point", ((ObjectIdValue)choix_magasin.getSelectedItem()).getId());
}
});
cardHome.add(choix_magasin,gbc);
cardVente.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth=2;
cardVente.add(labelVente, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth=2;
gbc.insets = new Insets(8,0,0,0);
listModel = new DefaultTableModel();
liste_prod_vente.setModel(listModel);
cardVente.add(liste_prod_vente, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(8,0,0,0);
gbc.gridwidth=2;
cardVente.add(label_vente_client,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(2,0,0,0);
gbc.gridwidth=2;
cardVente.add(liste_client,gbc);
panels.add(cardHome,BorderLayout.CENTER);
panels.add(cardBD,BorderLayout.CENTER);
panels.add(cardVente, BorderLayout.CENTER);
cardHome.setVisible(true);
cardBD.setVisible(false);
cardBD.setVisible(false);
So, i don't know what is the problem so weird. I'm waiting for solutions thanks.
You need to properly configure the layout manager of the parent container that holds the JScrollPane
(which in turn holds your JTable
).
From line
panelProd.add(scrollTab, gbc);
in your first code snippet, I guess the parent container is panelProd
and its layout manager is a GridBagLayout
. Thus you need to properly configure the GridBagConstraints
instance (which I guess is gbc
) associated to your scroll pane. You probably need something like
gbc.fill = GridBagConstraint.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
panelProd.add(scrollTab, gbc);
Do not set preferred sizes for your scroll pane and your table in this case!!
However, without knowing the rest of the code that populates panelProd
it is impossible to tell for sure.
Edit to reflect additional source code in original question:
Replace the code from line
panelProd.setLayout(new GridBagLayout());
to line
cardBD.add(panelProd,gbcPage);
with
JPanel panelProd = new JPanel(new BorderLayout());
tableProd.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableProd.setModel(modeleDatas);
panelProd.add(labelProd, BorderLayout.NORTH);
JScrollPane scrollTab=new JScrollPane(tableProd);
panelProd.add(scrollTab, BorderLayout.CENTER);
int oldFill = gbcPage.fill;
double oldWeightX = gbcPage.weightx;
double oldWeightY = gbcPage.weighty;
gbcPage.fill = GridBagConstraints.BOTH;
gbcPage.gridx=2;
gbcPage.gridy=1;
gbcPage.gridwidth=1;
gbcPage.weightx=1;
gbcPage.weighty=1;
cardBD.add(panelProd,gbcPage);
gbc.fill = oldFill;
gbc.weightx = oldWeightX;
gbc.weighty = oldWeightY;
GridBagLayout
is not necessary for simple arrangements like your prodPanel
(BorderLayout
is sufficient and much simpler to use in this case).