我有JPanel
GridBagLayout
。该面板包含2行,第一行有一个JLabel,第二行有一个JScrollPane
,里面有JTable
。该表未填充100%的滚动窗格。即使我调整框架大小,滚动窗格也会调整大小,但内部的桌子总是有固定的宽度。
JTable table=new JTable(myModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll=new JScrollPane(table);
JPanel panel=new JPanel(new GridBagLayout());
// component add details skipped
以下是添加到面板时应用于滚动窗格的网格包约束。
GridBagConstraints gbc=new GridBagConstraints();
gbc.gridx=0; // first column
gbc.gridy=1; // second row
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.fill=GridBagConstraints.BOTH;
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.weightx=1.0;
gbc.weighty=1.0;
出了什么问题?滚动窗格或表格有问题吗?
答案 0 :(得分:7)
我认为只是(通知行永远不会通过使用任何LayoutManager来调整大小)
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
GBC不适合JComponents的LayoutManager实现Scrollable,对这些JComponents使用BorderLayout(ev GridLayout)
例如
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class JTableAndGBC {
private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
private JTable table;
private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
{"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
public JTableAndGBC() {
JPanel panel = new JPanel(new GridBagLayout());
table = new JTable(model);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(table, gbc);
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JTableAndGBC();
}
});
}
}
编辑1
JTable
,JScollPane
,JComboBox
无法返回合理的PreferredSize
,请参阅编辑中的代码,然后查看所有LayoutManagers
的内容,
小心注意table.setPreferredScrollableViewportSize(table.getPreferredSize());
,我建议测试Dimension是否超载所需坐标或屏幕分辨率或不过载:-),
否则缩小new Dimension(int, int)
代替table.getPreferredSize()
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class JTableAndGBC {
private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
private JTable table;
private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
{"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
public JTableAndGBC() {
JPanel panel = new JPanel(new GridBagLayout());
table = new JTable(model);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
JScrollPane pane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
panel.add(pane, gbc);
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JTableAndGBC();
}
});
}
}
编辑2
我强烈建议使用BorderLayout,GridLayout
而不是GBC(JTable
,JScollPane
,JComboBox
无法返回合理的PreferredSize
,需要覆盖GBC,brrrr,这不是为什么要打扰)
。
BorderLayout的代码
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class JTableAndGBC {
private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
private JTable table;
private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
{"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
public JTableAndGBC() {
JPanel panel = new JPanel(new BorderLayout()/*(new GridBagLayout()*/);
table = new JTable(model);
//GridBagConstraints gbc = new GridBagConstraints();
//gbc.weightx = 1.0;
//gbc.fill = GridBagConstraints.HORIZONTAL;
JScrollPane pane = new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
/*TableColumn firstColumn = table.getColumnModel().getColumn(0);
firstColumn.setMinWidth(150);
firstColumn.setMaxWidth(200);
TableColumn secondColumn = table.getColumnModel().getColumn(1);
secondColumn.setMinWidth(200);
secondColumn.setMaxWidth(250);
TableColumn thirdColumn = table.getColumnModel().getColumn(1);
thirdColumn.setMinWidth(50);
thirdColumn.setMaxWidth(100); */
table.setRowHeight(30);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
panel.add(pane/*, gbc*/);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JTableAndGBC();
}
});
}
}
答案 1 :(得分:3)
感谢您提供宝贵的建议。我找到了解决这个问题的确切方法。将表格调整为首选大小或查看端口大小,以较大者为准。无论您使用何种布局管理器,它都能正常工作!
解决方案是使用getScrollableTracksViewportWidth()
方法定义的表,如下所示:
JTable table=new JTable(myModel){
public boolean getScrollableTracksViewportWidth() {
return getPreferredSize().width < getParent().getWidth();
}
};
JScrollPane scroll=new JScrollPane(table);
答案 2 :(得分:1)
删除
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
线。它会导致固定的表格大小。