gpt4 book ai didi

java - 在 JTable 中设置列​​的大小

转载 作者:行者123 更新时间:2023-12-02 06:42:13 24 4
gpt4 key购买 nike

我有扩展 JTable 的类,我有类实现 TableModel DefaultActionTableModel

public class RingTable extends JTable {

private static final long serialVersionUID = -2828916773705542169L;
private RingTableModel model;

public RingTable () {
}

public RingTable (List<String> columns) { // NO_UCD
model = new RingTableModel(columns);
setModel(model);
}

public RingTable (List<String> columns, List<TableRing> tableRings) {
model = new RingTableModel(columns, tableRings);
setModel(model);
}

/**
* Dodaje novu vrstu u tabelu sa zadatim indeksom
* @param i
*/
public void addRow(int i) {
model.addRow(i);
}


public boolean addRow(TableRing tableRing) { // NO_UCD
return model.addRow(tableRing);
}

/**
* Uklanja vrstu iz tabele koja ima indeks kao prosledjeni parametar
* @param i
* @return
*/
private boolean removeRow(int i) {
model.removeRow(i);
return true;
}

/**
* Uklanja sleektovane vrste iz tabele
*/
public void removeSelectedRows() {
int[] indices = getSelectedRows();
Arrays.sort(indices);
removeRowSelectionInterval(0, getRowCount()-1);
for (int i = indices.length-1; i >= 0; i--)
removeRow(indices[i]);
}

/**
* Uklanja sve vrste iz tabele
*/
public void removeAllRows() { // NO_UCD
while (model.getRowCount() > 0) {
removeRow(0);
}
}

}

 public class RingTableModel implements TableModel, DefaultActionTableModel {

private List<String> columns; // = new LinkedList<String>();
private List<TableRing> tableRings;


/**
*
*/
public RingTableModel() {
}

/**
*
*/
public RingTableModel(List<String> columns) {
this(columns, new LinkedList<TableRing>());
}

/**
*
*/
public RingTableModel(List<String> columns, List<TableRing> tableRings) {
this.columns = columns;
this.tableRings = tableRings;
}



/* (non-Javadoc)
* @see javax.swing.table.TableModel#addTableModelListener(javax.swing.event.TableModelListener)
*/
public void addTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnClass(int)
*/
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: // Index
return Integer.class;
case 1: // Name
return String.class;
case 2: // Show Dampers
case 3: // Show Inputs
case 4: // Show Outputs
return Boolean.class;
case 5: // Host
return String.class;
default:
return Boolean.class;
}
}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnCount()
*/
public int getColumnCount() {
return (columns!=null)?columns.size():0;
}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnName(int)
*/
public String getColumnName(int columnIndex) {
if (columns == null)
return null;
if (columns.size() <= columnIndex)
return null;
if (columnIndex < 0)
return null;
return columns.get(columnIndex);
}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getRowCount()
*/
public int getRowCount() {
return (tableRings == null)?0:tableRings.size();
}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#getValueAt(int, int)
*/
public Object getValueAt(int rowIndex, int columnIndex) {
if (tableRings == null)
return null;
if (rowIndex < 0 || rowIndex >= tableRings.size())
return null;
if (columnIndex < 0 || columnIndex >= columns.size())
return null;
TableRing tableRing = tableRings.get(rowIndex);
switch (columnIndex) {
case 0:
return rowIndex+1;
case 1:
return tableRing.getName();
case 2:
return tableRing.getShowDampers();
case 3:
return tableRing.getShowInputs();
case 4:
return tableRing.getShowOutputs();
case 5:
return tableRing.getHost();
default:
// TODO HARDCODE! broj kolona
// return tableRing.getTableAlarms().contains(RootData.getRootData().getEditableRingView().getTableAlarms().get(columnIndex-6));
if (tableRing.getTableAlarms().containsKey(columnIndex-RingTablePanel.COLUMN_NUMBER + 1)) {
return tableRing.getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER + 1);
}
return false;
}
}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#isCellEditable(int, int)
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return false;
case 1:
case 2:
case 3:
case 4:
case 5:
return true;
default:
return true;
}

}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#removeTableModelListener(javax.swing.event.TableModelListener)
*/
public void removeTableModelListener(TableModelListener l) {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
*/
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (tableRings == null)
return ;
if (aValue == null) {
return;
}
if (rowIndex < 0 || rowIndex >= tableRings.size())
return ;
if (columnIndex < 0 || columnIndex >= columns.size())
return ;
TableRing tableRing = tableRings.get(rowIndex);
switch (columnIndex) {
case 0:
break;
case 1:
tableRing.setName((String)aValue);
break;
case 2:
tableRing.setShowDampers((Boolean)aValue);
break;
case 3:
tableRing.setShowInputs((Boolean)aValue);
break;
case 4:
tableRing.setShowOutputs((Boolean)aValue);
break;
case 5:
tableRing.setHost((String)aValue);
break;
default:
// HARDCODE! broj kolona
// TableAlarm tableAlarm = RootData.getRootData().getEditableRingView().getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER);
// if (tableAlarm != null && tableRing.getTableAlarms().containsKey(tableAlarm.getId())) {
// tableRing.getTableAlarms().remove(tableAlarm);
// } else if (tableAlarm != null && tableRing.getTableAlarms().size() < RootData.getRootData().getMaxNoOfAlarms()){
// tableRing.getTableAlarms().put(tableAlarm.getId(), true);
// } else {
// Actions.showErrorDialog(RootData.getRootData().getResource("Error.TooManyAlarms", "Too many alarms selected in ring! Maximum is:") + " " + RootData.getRootData().getMaxNoOfAlarms(), RootData.getRootData().getResource("Error.Default", "Error"));
// }
// return ;
if (tableRing.getTableAlarms().containsKey(columnIndex-RingTablePanel.COLUMN_NUMBER + 1)) {
tableRing.getTableAlarms().put(columnIndex-RingTablePanel.COLUMN_NUMBER + 1, !tableRing.getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER + 1));
} else {
tableRing.getTableAlarms().put(columnIndex-RingTablePanel.COLUMN_NUMBER + 1, true);
}

return;
}
}


// Non-interface methods

/**
* This method adds a new column to the table, if no columns were defined, a new list is created
*/
private void addColumn(String columnName) {
if (columns == null)
columns = new LinkedList<String>();
columns.add(columnName);
}

/**
* This method creates a new ring row
*
* @return <b>true</b> - always
*/
public void addRow(int i) {
tableRings.add(i, new TableRing(""));
}


public boolean addRow(TableRing tableRing) {
return tableRings.add(tableRing);
}


/**
* This method removes a single row from the table
* @param i the index of the row to be removed
*/
public void removeRow(int i) {
tableRings.remove(i);
}

public void setColumns(List<String> columns) {
this.columns = columns;

int noAlarms = RootData.getRootData().getEditableRingView().getTableAlarms().size();
for (int i=1; i<=noAlarms; i++) {
addColumn("A_" + i);
}

}


}

如何将列“A_number”(A_1、A_2 ... 来自列表)的大小设置为 100px?

最佳答案

发件人:http://download.oracle.com/javase/tutorial/uiswing/components/table.html

要自定义初始列宽,您可以对表格的每一列调用 setPreferredWidth。这设置了列的首选宽度及其近似的相对宽度。例如,将以下代码添加到 SimpleTableDemo 使其第三列比其他列大:

TableColumn column = null;
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 2) {
column.setPreferredWidth(100); //third column is bigger
} else {
column.setPreferredWidth(50);
}
}

因此,遍历您的列列表,并将您想要设置的每个列的首选宽度设置为 100。

关于java - 在 JTable 中设置列​​的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com