我设计了两个具有相同columnmodel的jtables,因此它们相应地调整大小,这两个jtables应该具有相同的列号,我希望它们以相同的方式运行,当我们调整列或更改滚动条时。现在我有问题,当我调整第二个表的大小时,行上的列不会改变它们的大小,滚动条也不会相应地表现。
我的代码就是那样
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;
public class CompFilteredTable extends JPanel{
private JTable tblFilter;
private JTable tblData;
private JScrollPane scrollPane;
private JScrollPane scrollPane_1;
public CompFilteredTable() {
setLayout(new BorderLayout(0, 0));
add(getScrollPane_1(), BorderLayout.NORTH);
add(getScrollPane(), BorderLayout.CENTER);
}
public JTable getTblFilter() {
if (tblFilter == null) {
tblFilter = new MyTable(new TestTableModel2());
tblFilter.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setTableHeader (null);
}
return tblFilter;
}
public JTable getTblData() {
if (tblData == null) {
tblData = new MyTable(new TestTableModel());
tblData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setColumnModel(tblData.getColumnModel());
}
return tblData;
}
private class MyTable extends JTable {
private MyTable (final TableModel model) {
super (model);
}
private boolean isColumnMarginChangeInProgress=false;
@Override
public void columnMarginChanged(final ChangeEvent e) {
if (isColumnMarginChangeInProgress) {
return;
}
isColumnMarginChangeInProgress = true;
if (isEditing()) {
removeEditor();
}
TableColumn resizingColumn = null;
if (tableHeader != null) {
resizingColumn = tableHeader.getResizingColumn();
}
if (resizingColumn != null) {
if (autoResizeMode == AUTO_RESIZE_OFF) {
resizingColumn.setPreferredWidth(resizingColumn.getWidth());
} else { // this else block is missing in jdk1.4 as compared to 1.3
doLayout();
repaint();
}
} else {
resizeAndRepaint();
}
isColumnMarginChangeInProgress = false;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
CompFilteredTable compFilteredTable = new CompFilteredTable();
frame.getContentPane().add(compFilteredTable);
frame.pack();
frame.setVisible(true);
}
private class TestTableModel extends DefaultTableModel {
@Override
public int getRowCount () {
return 10;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private class TestTableModel2 extends DefaultTableModel {
@Override
public int getRowCount () {
return 1;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane();
scrollPane.setViewportView(getTblData());
}
return scrollPane;
}
private JScrollPane getScrollPane_1() {
if (scrollPane_1 == null) {
scrollPane_1 = new JScrollPane();
scrollPane_1.setViewportView(getTblFilter());
}
return scrollPane_1;
}
}
在deepak贡献之后更新了代码(我不知道如何显示我的更新,因此我在下面添加)
我更新了代码,现在图片上有两个问题。没有看到第二个表的标题,表之间存在很大的差距。
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
public class CompFilteredTable extends JPanel{
private JTable tblFilter;
private JTable tblData;
private JScrollPane scrollPane;
private JPanel panel;
public CompFilteredTable() {
setLayout(new BorderLayout(0, 0));
add(getScrollPane(), BorderLayout.CENTER);
}
public JTable getTblFilter() {
if (tblFilter == null) {
tblFilter = new MyTable(new TestTableModel2());
tblFilter.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setTableHeader (null);
}
return tblFilter;
}
public JTable getTblData() {
if (tblData == null) {
tblData = new MyTable(new TestTableModel());
tblData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setColumnModel(tblData.getColumnModel());
}
return tblData;
}
private class MyTable extends JTable {
private MyTable (final TableModel model) {
super (model);
}
private boolean isColumnMarginChangeInProgress=false;
@Override
public void columnMarginChanged(final ChangeEvent e) {
if (isColumnMarginChangeInProgress) {
return;
}
isColumnMarginChangeInProgress = true;
if (isEditing()) {
removeEditor();
}
TableColumn resizingColumn = null;
if (tableHeader != null) {
resizingColumn = tableHeader.getResizingColumn();
}
if (resizingColumn != null) {
if (autoResizeMode == AUTO_RESIZE_OFF) {
resizingColumn.setPreferredWidth(resizingColumn.getWidth());
} else { // this else block is missing in jdk1.4 as compared to 1.3
doLayout();
}
repaint();
} else {
resizeAndRepaint();
}
isColumnMarginChangeInProgress = false;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
CompFilteredTable compFilteredTable = new CompFilteredTable();
frame.getContentPane().add(compFilteredTable);
frame.pack();
frame.setVisible(true);
}
private class TestTableModel extends DefaultTableModel {
@Override
public int getRowCount () {
return 10;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private class TestTableModel2 extends DefaultTableModel {
@Override
public int getRowCount () {
return 1;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane();
scrollPane.setViewportView(getPanel());
}
return scrollPane;
}
private JPanel getPanel() {
if (panel == null) {
panel = new JPanel();
panel.setLayout(new GridLayout(2, 1, 0, 0));
panel.add(getTblFilter());
panel.add(getTblData());
}
return panel;
}
}
输出屏幕
答案 0 :(得分:1)
使用TableColumn
事件上的setPreferredWidth
方法设置columnMarginChanged
的首选宽度将更新JTableHeader
组件。因此,表标题会按照观察的大小调整。
在设置所需属性后显式调用repaint()
将触发更改以应用于表的每个组件。
所以这里需要进行更改以使其按预期运行。
if (autoResizeMode == AUTO_RESIZE_OFF) {
resizingColumn.setPreferredWidth(resizingColumn.getWidth());
repaint();
} else { // this else block is missing in jdk1.4 as compared to 1.3
此外,repaint()
也可以移出条件块。
答案 1 :(得分:0)
问题1:看不到第二个表格的标题。
原因:表标题设置为null
,如发布的代码所示。
public JTable getTblFilter() {
if (tblFilter == null) {
tblFilter = new MyTable(new TestTableModel2());
tblFilter.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setTableHeader (null);
}
return tblFilter;
}
解决方案是不将表头设置为null。删除部分代码
tblFilter.setTableHeader (null);
应该有效。
问题2:表之间存在很大差距。
有不同的LayoutManagers可用于根据您的需要设计UI。使用2行1列的GridLayout应该有帮助。
public CompFilteredTable() {
setLayout(new GridLayout(2, 1));
add(getScrollPane_1());
add(getScrollPane());
}
答案 2 :(得分:0)
可以将单个JTable添加到单个JScrollPane中,以解决UI头中未显示的表头问题。
为了管理表之间的差距,GridBagLayout可以提供帮助。 GridBagLayout是最灵活的布局管理器类.GridBagLayout的对象垂直,水平或沿着基线对齐组件,而不需要相同大小的组件。
答案 3 :(得分:0)
我解决了所有问题,表之间有一点差距。我想使用它作为组件来根据上表中的值过滤表。这个组件有什么改进吗?也许过滤后的函数可以添加到这个组件中。
组件的输出屏幕:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class CompFilteredTable extends JPanel{
private JTable tblFilter;
private JTable tblData;
private JScrollPane scrollPane;
private JPanel panel;
private JScrollPane scrollPane_1;
private JScrollPane scrollPane_2;
public CompFilteredTable() {
setLayout(new BorderLayout(0, 0));
add(getScrollPane(), BorderLayout.CENTER);
}
public JTable getTblFilter() {
if (tblFilter == null) {
tblFilter = new FilteredTable(new TestTableModel2());
tblFilter.setPreferredScrollableViewportSize(new Dimension(0, 0));
tblFilter.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setTableHeader (null);
}
return tblFilter;
}
public JTable getTblData() {
if (tblData == null) {
tblData = new FilteredTable(new TestTableModel());
tblData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblFilter.setColumnModel(tblData.getColumnModel());
}
return tblData;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
CompFilteredTable compFilteredTable = new CompFilteredTable();
frame.getContentPane().add(compFilteredTable);
frame.pack();
frame.setVisible(true);
}
private class TestTableModel extends DefaultTableModel {
@Override
public int getRowCount () {
return 10;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private class TestTableModel2 extends DefaultTableModel {
@Override
public int getRowCount () {
return 1;
}
@Override
public int getColumnCount () {
return 10;
}
@Override
public String getColumnName (final int column) {
return "Col-"+column;
}
@Override
public Object getValueAt (final int row, final int column) {
return (row * column) + "";
}
}
private JScrollPane getScrollPane() {
if (scrollPane == null) {
scrollPane = new JScrollPane();
scrollPane.setViewportView(getPanel());
}
return scrollPane;
}
private JPanel getPanel() {
if (panel == null) {
panel = new JPanel();
panel.setLayout(new BorderLayout(0, 0));
panel.add(getScrollPane_2(), BorderLayout.NORTH);
panel.add(getScrollPane_1());
}
return panel;
}
private JScrollPane getScrollPane_1() {
if (scrollPane_1 == null) {
scrollPane_1 = new JScrollPane();
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setViewportView(getTblData());
Dimension d = tblData.getPreferredSize();
scrollPane_1.setPreferredSize(new Dimension(d.width,tblData.getRowHeight()*tblData.getRowCount()+1));
}
return scrollPane_1;
}
private JScrollPane getScrollPane_2() {
if (scrollPane_2 == null) {
scrollPane_2 = new JScrollPane();
scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane_2.setViewportView(getTblFilter());
Dimension d = tblFilter.getPreferredSize();
scrollPane_2.setPreferredSize(new Dimension(d.width,tblFilter.getRowHeight()*tblFilter.getRowCount()+1));
}
return scrollPane_2;
}
}
class FilteredTable extends JTable {
FilteredTable (final TableModel model) {
super (model);
}
private boolean isColumnMarginChangeInProgress=false;
@Override
public void columnMarginChanged(final ChangeEvent e) {
if (isColumnMarginChangeInProgress) {
return;
}
isColumnMarginChangeInProgress = true;
if (isEditing()) {
removeEditor();
}
TableColumn resizingColumn = null;
if (tableHeader != null) {
resizingColumn = tableHeader.getResizingColumn();
}
if (resizingColumn != null) {
if (autoResizeMode == AUTO_RESIZE_OFF) {
resizingColumn.setPreferredWidth(resizingColumn.getWidth());
} else { // this else block is missing in jdk1.4 as compared to 1.3
doLayout();
}
repaint();
} else {
resizeAndRepaint();
}
isColumnMarginChangeInProgress = false;
}
}