linenumbers in a JTable
My last question for today:
I have a JTable and a TableModel in a Program. Now I'm wondering how to get linenumbers in that table?
Furthermore, if I mark one or more "linenumber fields" the whole rows shall be selected and the same thing with the columns - how to implement this?
The same thing if I select some fields in that table, the corresponding row names (=linenumber fields) and column names (=title names) should change its color/should get selected - this last thing is minor, if I know how to implement the other things before everything is fine.
[584 byte] By [
Corcovadoa] at [2007-10-2 10:53:22]

If you mean line numbers you mean a column that displays the row number of a particular row, that is quite simple. In your custom table model, just add another header nad as your populate your data structure, just increment the counter from 1 instead of 0.
I dont however understand what you mean when you say marking one or more line number fields...are you talking about selecting only 1 cell? Because by default if you click on a row, I think the entire row gets selected/highlighted. So please be a bit more specific. By any chance are you working on the java dev exam?
Cheers,
Surya
> My last question for today:
> I have a JTable and a TableModel in a Program. Now
> I'm wondering how to get linenumbers in that table?
For this one a very simple example based on "DefaultTableModel" to give you just an idea to handle it:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class LineNumberTable extends JTable
{
protected JTable mainTable;
public LineNumberTable(JTable table)
{
super();
mainTable = table;
setModel(new RowNumberTableModel());
setPreferredScrollableViewportSize(getPreferredSize());
getColumnModel().getColumn(0).setPreferredWidth(50);
getColumnModel().getColumn(0).setCellRenderer( mainTable.getTableHeader().getDefaultRenderer() );
}
public int getRowHeight(int row)
{
return mainTable.getRowHeight();
}
class RowNumberTableModel extends AbstractTableModel
{
public int getRowCount()
{
return mainTable.getModel().getRowCount();
}
public int getColumnCount()
{
return 1;
}
public Object getValueAt(int row, int column)
{
return new Integer(row + 1);
}
}
public static void main(String[] args)
{
Object[][] data = { {"1", "A"}, {"2", "B"}, {"3", "C"} };
String[] columnNames = {"Number","Letter"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane( table );
JTable lineTable = new LineNumberTable( table );
scrollPane.setRowHeaderView( lineTable );
JFrame frame = new JFrame( "Line Number Table" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
abollm:
Thank you abollm for the table (model). These are at least the line numbers I'm looking for.
suryade:
I'm sorry, my english is sometimes a bit sleazy. Especially when I am searching for names of items in Java. ok - so what I meant was:
I would like to be able to select a complete row or a (range of complete rows) by a click on the row number (cell) or a range of row numbers (cells). The same way using the column numbers for complete columns. Since I already implemented some, I don't want to change every index that starts with a 0, so I thought/hoped that there might be a way using any TableModel.
What is "the Java Dev Exam" ? - No, I'm working here just on a project, I should develop an Interface in Java and this now should be something for data selection - I should present something by the beginning of next week, therefore I don't have much time to read a lot about e.g. tables or TableModels (I found lots, though..).
Do you have a link on that (java dev exam)? Sounds quite interesting.
Oh man, I'm also just a Java rookie driving people crazy here with my quests. Reading is one thing - probably the better - to get deeper info, when you don't have enough time, it's rather tough. Another thing is asking people.
Personally I like C++/Qt a little more, (hope I may say that here, smile) but I think C++ is not better or worse than Java, it's a personal thing. Most of my experiences I have in Java and Java is very good to get started and to do almost everything else, too, this is very awesome. I found a quite good tuto on tables which I'd like to read by next week, we'll see. Wish you more luck for your exam.