jtable interview questions and answers in java

jtable interview questions and answers in java




1. What is jtable in java and why it is used?



JTable is a component from java swing package which is used to show the data in table format. The data can be presented in many forms using the table models which can be customized too.

JTable is used for tabular presentation of the data, but it gives more than that such as you can edit the jTable values, customize the each row and column , show images in rows and column or headers.
Apart from that it gives very important facility to sort using the individual columns so it is very important component in java swing and used frequently in UI

2. Create a simple jtable in java and populate it with data



If you are doing face to face interview, possibility is that you will be asked to write a code for creating a simple jtable and populate it with data. It is one of the important jtable interview questions because it shows you know the constructor of the jtable and passing the two dimensional array structure to jtable in java.

String columnNames[] = { "Column 1", "Column 2", "Column 3" };

// Create some data
String dataValues[][] =
{
 { "a", "b", "c" },
 { "l", "m", "n" },
 { "p", "q", "r" },
 { "x", "y", "z" }
};

// Create a new table instance
table = new JTable( dataValues, columnNames );


3. What is the difference between DefaultTableModel and AbstractTableModel?



AbstractTableModel vs DefaultTableModel

The AbstractTableModel is a class that implements TableModel interface. Though many of the methods of the TableModel interface implemented by the AbstractTableModel, these methods are simple and minimal changes done. So AbstractTableModel gives you the choice to implement those methods, which collection to use etc. On the other hand DefaultTableModel is a subclass of AbstractTableModel, which makes defaultTableModel concrete but easy to use.A defaultTableModel uses Vectors of Vectors.

This question is again one of very important jtable interview questions because these two models are frequently used in jtable.
Choosing the model is dependent of usage because AbstractTableModel gives more flexibiliy than DefaultTableModel in java


4. Write a simple jtable with AbstractTableModel



This question is one of the tough jtable interview questions because it shows how you have used abstractTableModel to fill the data in jtable. Here you will find Simple JTable example using AbstractTableModel






5. How to write cell renderer component to the jtable?


jtable often requires cell to be rendered , it makes really one of the good jtable interview questions. You can find here how to create custom cell renderer for the jtable cell and make custom changes in jtable cell JTable with custom cell renderer example

6. How do you set the tooltip to the jtable



jtable cell have more data than their size, so it is very important to show the correct tool tip for each cell, so this is also one of the very important jtable interview questions. To find how to set tooltip for the jtable please click below :

How to set tooltip to JTable cell

7. How to add custom cell editor to jtable



This is really a one of the important jtable interview questions for senior level developers. A jtable cell editor requires to implement the TableCellEditor interface. We also need to extend AbstractCellEditor class to have listeners supported by the TableCellEditor. So most important things to remember is the implementing the TableCellEditor interface and extending the AbstractCellEditor class.

When we consider table cell editor , it is slightly different than the JTable with custom cell renderer where the editor not required create a new component each time getTableCellEditorComponent() is called. It should call the same component again.

public class CustomCellEditor extends AbstractCellEditor implements TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex) {
        // 'value' is value located at rowIndex, vColIndex

        if (isSelected) {
            // if cell is selected
        }

        // Setting the text
        ((JTextField)component).setText((String)value);

        return component;
    }

    public Object getCellEditorValue() {
        return ((JTextField)component).getText();
    }
    // getCellEditorValue method is called when editing is completed.
     
}

Using getTableCellEditorComponent() method you can get the value from the cell and configure it as show as setText. getCellEditorValue() method is called when editing is completed and returns the new value stored in the cell.

But how to use this CustomCellEditor class? Lets see with following code :
TableColumn col = table.getColumnModel().getColumn(columnIndex);
col.setCellEditor(new CustomCellEditor());


Thus we have seen how we can create custom cell editor in jtable, important things to note that TableCellEditor interface and AbstractCellEditor class. And methods to remember are getTableCellEditorComponent() and getCellEditorValue()


8. How to add background image to the jtable?



This is also one of the important jtable interview questions. Many times we need a watermark or insert an image behind the jtable. Following tutorial How to add watermark or background image to the JTable? have detailed explanation of adding background image to the jtable.

Hope you find these jtable interview questions and answers helpful.
If you like this article share it with your friends :)
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment