您好。我需要一些帮助。 JScrollPane工作垂直良好但水平不工作。我无法找到问题所在。我知道这是一个很大的项目,并且有很多令人困惑的代码,但我无法删除任何该功能。
public final class Salary extends javax.swing.JFrame {
private SalaryTableModel tableModel;
public int selectedRow = -1;
public static String bolme;
public static String ay;
public static int il = 0;
private final String selectedItem_Bolme;
private final String selectedItem_Ay;
private final int selectedItem_Il;
private List<Pojo> sortedList;
public TableColumn date;
public static int bolmeId = 0;
public static String ad;
public static int row2;
public void visible(String bolme2, String ay2, int il2) {
il = il2;
ay = ay2;
bolme = bolme2;
new Salary().setVisible(true);
}
public RXTable autoResizeColWidth(RXTable table, SalaryTableModel model) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setModel(model);
int margin = 5;
for (int i = 0; i < table.getColumnCount(); i++) {
int vColIndex = i;
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
renderer = table.getTableHeader().getDefaultRenderer();
}
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
width = comp.getPreferredSize().width;
for (int r = 0; r < table.getRowCount(); r++) {
renderer = table.getCellRenderer(r, vColIndex);
comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
r, vColIndex);
width = Math.max(width, comp.getPreferredSize().width);
}
width += 2 * margin;
col.setPreferredWidth(width);
}
((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
SwingConstants.LEFT);
table.getTableHeader().setReorderingAllowed(false);
return table;
}
public Salary() {
initComponents();
Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/db.png"));
setDefaultCloseOperation(EXIT_ON_CLOSE);
List<Pojo> model = DBO.findBolme();
HashSet hs = new HashSet();
ArrayList al = new ArrayList();
model.stream().forEach((model1) -> {
hs.add(model1.getBolme());
});
al.addAll(hs);
comboBolme.addItem("Butun Bolmeler");
al.stream().forEach((al1) -> {
comboBolme.addItem(al1);
bolmeId++;
});
comboBolme.removeItem("null");
if (bolme == null) {
comboBolme.setSelectedIndex(0);
} else {
comboBolme.setSelectedItem(bolme);
}
if (ay == null) {
comboAy.setSelectedIndex(0);
} else {
comboAy.setSelectedItem(ay);
}
List<Pojo> model2 = DBO.findIl();
HashSet hs2 = new HashSet();
ArrayList al2 = new ArrayList();
model2.stream().forEach((model1) -> {
hs2.add(model1.getIl());
});
al2.addAll(hs2);
comboIl.addItem(0000);
al2.stream().forEach((al1) -> {
comboIl.addItem(al1);
});
comboIl.removeItem(0);
if (il == 0) {
comboIl.setSelectedIndex(0);
} else {
comboIl.setSelectedItem(il);
}
selectedItem_Bolme = (String) comboBolme.getSelectedItem();
selectedItem_Ay = (String) comboAy.getSelectedItem();
selectedItem_Il = Integer.parseInt(comboIl.getSelectedItem().toString());
Pojo tempVar;
sortedList = DBO.salary_Find(selectedItem_Bolme, selectedItem_Ay, selectedItem_Il);
for (int i = 0; i < sortedList.size(); i++) {
if (sortedList.get(i).getIsleyir() == 1) {
sortedList.remove(i);
i--;
}
}
for (int i = 0; i < sortedList.size(); i++) {
for (int j = 0; j < sortedList.size(); j++) {
if (sortedList.get(i).getCem() == sortedList.get(j).getCem()) {
}
if (sortedList.get(i).getCem() > sortedList.get(j).getCem()) {
tempVar = sortedList.get(j);
sortedList.set(j, sortedList.get(i));
sortedList.set(i, tempVar);
}
}
}
tableModel = new SalaryTableModel(sortedList) {
@Override
public boolean isCellEditable(int row, int column) {
ad = tableModel.getTopic(row).getAd();
selectedRow = tableModel.getTopic(row).getId();
row2 = row;
return (column != 0) && (column != 17);
}
};
table.setModel(tableModel);
table = autoResizeColWidth(table, tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM");
DatePickerCellEditor datePicker = new DatePickerCellEditor(formatter);
datePicker.setFormats(formatter);
date = table.getColumnModel().getColumn(18);
date.setCellEditor(datePicker);
table = new RXTable(tableModel);
JScrollPane pane = new JScrollPane(table);
add(pane);
table.setSelectAllForEdit(true);
table.getColumnModel().getColumn(0).setMinWidth(100);
table.getColumnModel().getColumn(1).setMinWidth(100);
table.getColumnModel().getColumn(2).setMinWidth(100);
table.getColumnModel().getColumn(3).setMinWidth(100);
table.getColumnModel().getColumn(4).setMinWidth(100);
table.getColumnModel().getColumn(5).setMinWidth(100);
table.getColumnModel().getColumn(6).setMinWidth(100);
table.getColumnModel().getColumn(7).setMinWidth(100);
table.getColumnModel().getColumn(8).setMinWidth(100);
table.getColumnModel().getColumn(9).setMinWidth(100);
table.getColumnModel().getColumn(10).setMinWidth(100);
table.getColumnModel().getColumn(11).setMinWidth(100);
table.getColumnModel().getColumn(12).setMinWidth(100);
table.getColumnModel().getColumn(13).setMinWidth(100);
table.getColumnModel().getColumn(14).setMinWidth(100);
table.getColumnModel().getColumn(15).setMinWidth(100);
table.getColumnModel().getColumn(16).setMinWidth(100);
table.getColumnModel().getColumn(17).setMinWidth(100);
table.getColumnModel().getColumn(18).setMinWidth(100);
table.getColumnModel().getColumn(19).setMinWidth(100);
table.getColumnModel().getColumn(20).setMinWidth(100);
table.getColumnModel().getColumn(21).setMinWidth(100);
table.getColumnModel().getColumn(21).setMaxWidth(10000);
setLocationRelativeTo(null);
double x = 0;
for (int i = 0; i < tableModel.getRowCount(); i++) {
x += tableModel.getTopic(i).getArtim();
}
lblArtiminCemi.setText(String.valueOf((x)));
double y = 0;
for (int i = 0; i < tableModel.getRowCount(); i++) {
y += tableModel.getTopic(i).getCem();
}
lblTotal.setText(String.valueOf((y)));
comboBolme.addActionListener((ActionEvent e) -> {
String selectedBolme = (String) comboBolme.getSelectedItem();
visible(selectedBolme, ay, il);
setVisible(false);
});
comboAy.addActionListener((ActionEvent e) -> {
String selectedAy = (String) comboAy.getSelectedItem();
visible(bolme, selectedAy, il);
setVisible(false);
});
comboIl.addActionListener((ActionEvent e) -> {
int selectedIl = Integer.parseInt(comboIl.getSelectedItem().toString());
visible(bolme, ay, selectedIl);
setVisible(false);
});
table.getModel().addTableModelListener((TableModelEvent e) -> {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model1 = (TableModel) e.getSource();
String columnName = model1.getColumnName(column);
Object value = model1.getValueAt(row, column);
Pojo temp;
sortedList = DBO.salary_Find(selectedItem_Bolme, selectedItem_Ay, selectedItem_Il);
for (int i = 0; i < sortedList.size(); i++) {
if (sortedList.get(i).getIsleyir() == 1) {
sortedList.remove(i);
i--;
}
}
for (int i = 0; i < sortedList.size(); i++) {
for (int j = 0; j < sortedList.size(); j++) {
if (sortedList.get(i).getCem() > sortedList.get(j).getCem()) {
temp = sortedList.get(j);
sortedList.set(j, sortedList.get(i));
sortedList.set(i, temp);
}
}
}
List<Pojo> list = sortedList;
Pojo data = list.get(row);
switch (column) {
case 0:
data.setId((int) value);
break;
case 1:
data.setAd((String) value);
break;
case 2:
data.setCins((int) value);
break;
case 3:
data.setBank((Double) value);
data.setCem(data.getBank() + data.getIlk_maas() + data.getSon_maas() + data.getArtim());
break;
case 4:
data.setIlk_maas((Double) value);
data.setCem(data.getBank() + data.getIlk_maas() + data.getSon_maas() + data.getArtim());
break;
case 5:
data.setSon_maas((Double) value);
data.setCem(data.getBank() + data.getIlk_maas() + data.getSon_maas() + data.getArtim());
break;
case 6:
data.setArtim((Double) value);
data.setCem(data.getBank() + data.getIlk_maas() + data.getSon_maas() + data.getArtim());
break;
case 7:
data.setCem((Double) value);
break;
case 8:
data.setElave_is((int) value);
break;
case 9:
data.setMukafatlandirma((Double) value);
break;
case 10:
data.setQayib_gunu((int) value);
break;
case 11:
data.setDetal_xetasi((Double) value);
break;
case 12:
data.setSatin_alma((Double) value);
break;
case 13:
data.setBolme(value.toString().toUpperCase());
break;
case 14:
data.setAy((String) value);
break;
case 15:
data.setIl((Integer) value);
break;
case 16:
data.setIsleyir((int) value);
break;
case 17:
data.setNe_qeder_isleyib((String) value);
break;
case 18:
try {
String time = value.toString();
Date date3 = new Date();
SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM");
Date date4 = format2.parse(time);
SimpleDateFormat format3 = new SimpleDateFormat("yyyy/MM");
data.setBaslama_tarixi(format3.format(date4));
Calendar cal = Calendar.getInstance();
cal.setTime(date3);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date4);
int diffYear = cal.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
int diffMonth = cal.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
if (diffMonth < 0) {
diffMonth += 12;
diffYear--;
}
data.setNe_qeder_isleyib(diffYear + " il " + diffMonth + " ay");
} catch (NumberFormatException | IndexOutOfBoundsException | ParseException ex) {
Logger.getLogger(Salary.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case 19:
data.setBorc((Double) value);
break;
case 20:
data.setYekun_maas((Double) value);
data.setCem(data.getBank() + data.getIlk_maas() + data.getSon_maas() + data.getArtim());
data.setYekun_maas(data.getCem() - data.getBorc()
- ((data.getCem() / 26) * data.getQayib_gunu()) - data.getDetal_xetasi()
+ data.getSatin_alma() + ((data.getCem() / 26) * data.getElave_is()));
break;
case 21:
data.setQeyd((String) value);
break;
}
DBO.salary_Update(data, selectedRow);
visible(bolme, ay, il);
setVisible(false);
});
}`
答案 0 :(得分:1)
如果您想要显示单元格的全文而不是缩写,则可以使用Table Column Adjuster。 e.g:
String[] columnNames = {"Colum1", "Colum2", "Colum2"};
String[][] data = {
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "-", "-"},
{"-", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "-"},
{"-", "-", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
};
TableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
JScrollPane pane = new JScrollPane(table);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.setSize(400, 300);
frame.setVisible(true);
您可以从http://www.camick.com/java/source/TableColumnAdjuster.java
获取课程TableColumnAdjuster
的代码
答案 1 :(得分:0)
找到方法)
table.setModel(tableModel);
table = autoResizeColWidth(table, tableModel);
//table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM");
DatePickerCellEditor datePicker = new DatePickerCellEditor(formatter);
datePicker.setFormats(formatter);
date = table.getColumnModel().getColumn(18);
date.setCellEditor(datePicker);
table = new RXTable(tableModel) {
@Override
public boolean getScrollableTracksViewportWidth() {
return getPreferredSize().width < getParent().getWidth();
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane pane = new JScrollPane(table);
add(pane);
//table.setSelectAllForEdit(true);
//table.getColumnModel().getColumn(0).setMinWidth(100);
//table.getColumnModel().getColumn(1).setMinWidth(100);
//....
//table.getColumnModel().getColumn(21).setMinWidth(100);
//table.getColumnModel().getColumn(21).setMaxWidth(10000);
setLocationRelativeTo(null);