I want to dispose the text inserted into a JTable cell in a particular way. Infact now if I write into a cell, the text will be all on the same line, while I would like to see it in two lines.
Maybe I would be clearer with a graphic description. See, I would like the second one:
My Table code here:
public class TablePanel extends JPanel
{
private JTable table;
public Tabella()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane jps = new JScrollPane(table);
add(jps);
add(new JScrollPane(table));
table.setCellSelectionEnabled(true);
table.setRowHeight(30);
TableColumn tcol;
for (int i=0; i<table.getColumnCount(); i++)
{
tcol = table.getColumnModel().getColumn(i);
tcol.setCellRenderer(new CustomTableCellRenderer());
}
table.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
if (col>0) {
if (e.getClickCount() > 1) {
if (row == 5 | row == 6)
{
JOptionPane.showMessageDialog(null, "Impossible to set lesson.");
return;
}
else {
table.getColumnName(col);
String day = table.getColumnName(col);
String hour = (String) table.getValueAt(row, 0);
InsertLesson cell = new InsertLesson(day, hour);
cel.setVisible(true);
}
}
}
}
}
);
}
private class MyTableModel extends AbstractTableModel {
private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private String[][] data = {{"8:30 - 9:30","","","","","",""},
{"9:30 - 10:30","","","","","",""},
{"10:30 - 11:30","","","","","",""},
{"11:30 - 12:30","","","","","",""},
{"12:30 - 13:30","","","","","",""},
{"13:30 - 14:30","","","","","",""},
{"14:30 - 15:30","","","","","",""},
{"15:30 - 16:30","","","","","",""},
{"16:30 - 17:30","","","","","",""}};
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columns[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.lightGray);
}
else {
if (row % 2 == 0) {
cell.setBackground(new Color (245,245,250));
}
else {
cell.setBackground(new Color(250,250,240));
}
}
return cell;
}
}
}
Thanks to the user Kiheru, a solution has been found: we have to:
1) centre the format of the string that will be inserted into the cell;
2) centre the "space" of the cell itself;
We can do these action by:
1) insert html centring text code in the string that has to be inserted into the cell (html code is automatically recognized);
2) centre the cell itself with the command setHorizontalAlignment(JLabel.CENTER);
.
This question might be useful for everyone has this problem.
Class1" in you table model. That should result in a two line cell with no changes in your actual code at all. – kiheru Sep 13 '13 at 11:17
Class1