java Jtable JscrollPane 以全屏高度显示的JTable
java Jtable JscrollPane the JTable displaying at full screen height
我的 JTable 和滚动条有问题我尝试了几件事现在只有当框架处于全屏高度时才可以,请参见下面的屏幕截图:
我试图实现的目标:
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);
甚至用这个 JSTable 扩展了 JTable :
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代码:
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);
所以,我不知道是什么问题这么奇怪。我正在等待解决方案谢谢。
您需要正确配置 包含 JScrollPane
的父容器 的布局管理器(它又包含您的 JTable
)。
来自行
panelProd.add(scrollTab, gbc);
在您的第一个代码片段中,我猜父容器是 panelProd
,其布局管理器是 GridBagLayout
。因此,您需要正确配置与滚动窗格关联的 GridBagConstraints
实例(我猜是 gbc
)。你可能需要像
这样的东西
gbc.fill = GridBagConstraint.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
panelProd.add(scrollTab, gbc);
在这种情况下,不要为滚动窗格和 table 设置首选大小!!
但是,如果不知道填充 panelProd
的其余代码,则无法确定。
编辑以反映原始问题中的附加源代码:
替换行
中的代码
panelProd.setLayout(new GridBagLayout());
到行
cardBD.add(panelProd,gbcPage);
和
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
对于像 prodPanel
这样的简单安排来说不是必需的(BorderLayout
就足够了,在这种情况下使用起来更简单)。
我的 JTable 和滚动条有问题我尝试了几件事现在只有当框架处于全屏高度时才可以,请参见下面的屏幕截图:
我试图实现的目标:
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);
甚至用这个 JSTable 扩展了 JTable :
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代码:
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);
所以,我不知道是什么问题这么奇怪。我正在等待解决方案谢谢。
您需要正确配置 包含 JScrollPane
的父容器 的布局管理器(它又包含您的 JTable
)。
来自行
panelProd.add(scrollTab, gbc);
在您的第一个代码片段中,我猜父容器是 panelProd
,其布局管理器是 GridBagLayout
。因此,您需要正确配置与滚动窗格关联的 GridBagConstraints
实例(我猜是 gbc
)。你可能需要像
gbc.fill = GridBagConstraint.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
panelProd.add(scrollTab, gbc);
在这种情况下,不要为滚动窗格和 table 设置首选大小!!
但是,如果不知道填充 panelProd
的其余代码,则无法确定。
编辑以反映原始问题中的附加源代码:
替换行
中的代码panelProd.setLayout(new GridBagLayout());
到行
cardBD.add(panelProd,gbcPage);
和
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
对于像 prodPanel
这样的简单安排来说不是必需的(BorderLayout
就足够了,在这种情况下使用起来更简单)。