我想添加JTable
到JPanel
其布局中null
. JPanel
包含其他组件.我必须添加JTable
适当的位置.
我有一个JTable,我在其中设置列大小如下:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(400);
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作,但是当表格最大化时,我会在最后一列的右边找到空白区域.是否可以在调整大小时将最后一列调整到窗口末尾?
我AUTO_RESIZE_LAST_COLUMN
在docs中找到了属性,但它不起作用.
编辑:JTable
是在JScrollPane
其首选大小设置.
这是我的jTable
private JTable getJTable() {
String[] colName = { "Name", "Email", "Contact No. 1", "Contact No. 2",
"Group", "" };
if (jTable == null) {
jTable = new JTable() {
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
}
DefaultTableModel contactTableModel = (DefaultTableModel) jTable
.getModel();
contactTableModel.setColumnIdentifiers(colName);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return jTable;
}
Run Code Online (Sandbox Code Playgroud)
我将调用此方法从数据库中检索数据并将其放入表模型中
public void setUpTableData() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
ArrayList<Contact> list = new ArrayList<Contact>();
if (!con.equals(""))
list = sql.getContactListsByGroup(con);
else
list = sql.getContactLists();
for (int i …
Run Code Online (Sandbox Code Playgroud) 我有以下代码来实例化一个JTable:该表提供了正确数量的行和列,但没有列上标题的标志.
public Panel1()
{
int nmbrRows;
setLayout(null);
setBackground(Color.magenta);
Vector colHdrs;
//create column headers
colHdrs = new Vector(10);
colHdrs.addElement(new String("Ticker"));
// more statements like the above to establish all col. titles
nmbrRows = 25;
DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
tblModel.setColumnIdentifiers(colHdrs);
scrTbl = new JTable(tblModel);
scrTbl.setBounds(25, 50, 950, 600);
scrTbl.setBackground(Color.gray);
scrTbl.setRowHeight(23);
add(scrTbl);
//rest of constructor
...
}
Run Code Online (Sandbox Code Playgroud)
将此与其他制表代码进行比较,我没有看到任何缺失的步骤,但必须缺少某些内容.
我宣布我JTable
的:
data_table = new JTable(info, header) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
但我已经看到,在运行时可以用鼠标拖动列.我怎么能禁用它?
启动应用程序时,不会选择任何行.但我想表明第一行已被选中.
这该怎么做?我需要设置一行的颜色JTable
吗?
更新:我尝试了table.setRowSelectionInterval(0,0).我知道它应该工作,但有一条错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Row index out of range
at javax.swing.JTable.boundRow(Unknown Source)
at javax.swing.JTable.setRowSelectionInterval(Unknown Source)
at cpn_gui.OptPanel.createForm(OptPanel.java:124)
at cpn_gui.OptPanel.<init>(OptPanel.java:50)
at cpn_gui.Login$1.actionPerformed(Login.java:62)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown …
Run Code Online (Sandbox Code Playgroud) JScrollPane.
根据我的应用程序中发生的事件,我在运行时将一个位于行内的JTable 添加到表中.我想让一个新的行添加到表格时,scoll窗格滚动到表格的底部.
对于JLists有[ensureIndexIsVisible][1]()
强制列表中的特定索引可见.我正在寻找相同的东西,但对于JTable.看起来我可能不得不手动移动滚动窗格上的滚动视图,但我认为必须有一个更简单的方法.
我设计了一个GUI,其中我使用了一个JTable,我必须使2个列不可见.我该怎么办?