Fixed row in JTable
Dear All
Does anyone know how to fix a row in a JTable, for example, to fix the bottom row in a JTable, so that users can scroll through the rest of the rows in the table without affecting this row. But when if a column size is changed, it will resize its cell simultaneously.
if you know the answer, please provide sample code as well.
Thanks in advance
Ken
Hi,
to do that, you need an own DataModel in the table, I guess. What is displayed in a cell is asked from the JTable by the getValueAt(...)-method of the datamodel. If you want to fix a row that way, you have to answer this "question" with the right response - that will be not the original cell there but another one while scrolling during a row is fixed.
You also must keep track of the messages that are to be fired to synchronize the display of the table with the answers you return on the getValueAt(...)-method. The "fixing" is a kind of shifting the fixed row in the table, that must be reflected by the messages you have to fire. fireTableDataChanged() would be an idea but that cause rerendering of all the visible cells of the table - other messages are more precise, but you need to do more and have to worry about more things while programming this.
In the case of the bottom line it is possible to use a second JTable in the south of the panel which has only one row - the fixed row - in it - if you discard the header of it, it would look like the row is fixed in the original table, but by using this possibility you must synchronize the changing of the column-width and ofcourse the reordering of columns too done in the original table with the second table. I guess the method in the above paragraph would be the better approach to it.
I have no code for it, but I have tried, to give you a guideline how this can be done.
greetings Marsian
Similar Messages
-
JTable fixed Row and Column in the same window
Hi
Could anyone tip me how to make fixed row and column in the same table(s).
I know how to make column fixed and row, tried to combine them but it didnt look pretty.
Can anyone give me a tip?
Thanks! :)Got it to work!
heres the kod.. nothing beautiful, didnt clean it up.
* NewClass.java
* Created on den 29 november 2007, 12:51
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
package tablevectortest;
* @author Sockan
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class FixedRowCol extends JFrame {
Object[][] data;
Object[] column;
JTable fixedTable,table,fixedColTable,fixedTopmodelTable;
private int FIXED_NUM = 16;
public FixedRowCol() {
super( "Fixed Row Example" );
data = new Object[][]{
{ "","A","A","A","",""},
{ "a","b","","","",""},
{ "a","","c","","",""},
{ "","","","d","",""},
{ "","","","","e",""},
{ "","","","","","f"},
{ "","b","","","",""},
{ "","","c","","",""},
{ "","","","d","",""},
{ "","","","","e",""},
{ "","b","","","",""},
{ "","","c","","",""},
{ "","","","d","",""},
{ "","","","","e",""},
{ "","","","","","f"},
{ "I","","W","G","A",""}};
column = new Object[]{"A","B","C","D","E","F"};
AbstractTableModel fixedColModel = new AbstractTableModel() {
public int getColumnCount() {
return 1;
public int getRowCount() {
return data.length;
public String getColumnName(int col) {
return (String) column[col];
public Object getValueAt(int row, int col) {
return data[row][col];
public boolean CellEditable(int row, int col) {
return true;
AbstractTableModel model = new AbstractTableModel() {
public int getColumnCount() { return column.length-2; }
public int getRowCount() { return data.length - FIXED_NUM; }
public String getColumnName(int col) {
return (String)column[col+1];
public Object getValueAt(int row, int col) {
return data[row][col+1];
public void setValueAt(Object obj, int row, int col) {
data[row][col+1] = obj;
public boolean CellEditable(int row, int col) {
return true;
AbstractTableModel fixedTopModel = new AbstractTableModel() {
public int getColumnCount() { return 1; }
public int getRowCount() { return data.length - FIXED_NUM; }
public String getColumnName(int col) {
return (String)column[col];
public Object getValueAt(int row, int col) {
return data[row][col];
public void setValueAt(Object obj, int row, int col) {
data[row][col] = obj;
public boolean CellEditable(int row, int col) {
return true;
AbstractTableModel fixedModel = new AbstractTableModel() {
public int getColumnCount() { return column.length-2; }
public int getRowCount() { return FIXED_NUM; }
public Object getValueAt(int row, int col) {
return data[row + (data.length - FIXED_NUM)][col+1];
table = new JTable( model );
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable = new JTable( fixedModel );
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedColTable= new JTable(fixedColModel);
fixedColTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedColTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTopmodelTable = new JTable(fixedTopModel);
fixedTopmodelTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTopmodelTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane( table );
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane fixedScroll = new JScrollPane( fixedTable ) {
public void setColumnHeaderView(Component view) {}
fixedScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
fixedScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollBar bar = scroll.getVerticalScrollBar();
JScrollBar dummyBar = new JScrollBar() {
public void paint(Graphics g) {}
dummyBar.setPreferredSize(bar.getPreferredSize());
scroll.setVerticalScrollBar(dummyBar);
final JScrollBar bar1 = scroll.getHorizontalScrollBar();
JScrollBar bar2 = fixedScroll.getHorizontalScrollBar();
bar2.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
bar1.setValue(e.getValue());
JViewport viewport = new JViewport();
viewport.setView(fixedColTable);
viewport.setPreferredSize(fixedColTable.getPreferredSize());
fixedScroll.setRowHeaderView(viewport);
fixedScroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedColTable
.getTableHeader());
JViewport viewport2 = new JViewport();
viewport2.setView(fixedTopmodelTable);
viewport2.setPreferredSize(fixedTopmodelTable.getPreferredSize());
scroll.setRowHeaderView(viewport2);
scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTopmodelTable
.getTableHeader());
scroll.setPreferredSize(new Dimension(600, 19));
fixedScroll.setPreferredSize(new Dimension(600, 100));
getContentPane().add( scroll, BorderLayout.NORTH);
getContentPane().add(fixedScroll, BorderLayout.CENTER);
public static void main(String[] args) {
FixedRowCol frame = new FixedRowCol();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
frame.pack();
frame.setVisible(true);
} -
I am trying to change the following code so then the fixed rows will stay on top(but below the headers!), but when I change getContentPane().add(fixedScroll, BorderLayout.SOUTH); to getContentPane().add(fixedScroll, BorderLayout.NORTH); it stays above the headers. How can I put the first rows on top, but below the headers ? Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
* @version 1.0 03/05/99
public class FixedRowExample extends JFrame {
Object[][] data;
Object[] column;
JTable fixedTable,table;
private int FIXED_NUM = 2;
public FixedRowExample() {
super( "Fixed Row Example" );
data = new Object[][]{
{ "a","","","","",""},
{ "","b","","","",""},
{ "","","c","","",""},
{ "","","","d","",""},
{ "","","","","e",""},
{ "","","","","","f"},
{"fixed1","","","","","","",""},
{"fixed2","","","","","","",""}};
column = new Object[]{"A","B","C","D","E","F"};
AbstractTableModel model = new AbstractTableModel() {
public int getColumnCount() { return column.length; }
public int getRowCount() { return data.length - FIXED_NUM; }
public String getColumnName(int col) {
return (String)column[col];
public Object getValueAt(int row, int col) {
return data[row][col];
public void setValueAt(Object obj, int row, int col) {
data[row][col] = obj;
public boolean CellEditable(int row, int col) {
return true;
AbstractTableModel fixedModel = new AbstractTableModel() {
public int getColumnCount() { return column.length; }
public int getRowCount() { return FIXED_NUM; }
public Object getValueAt(int row, int col) {
return data[row + (data.length - FIXED_NUM)][col];
table = new JTable( model );
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
fixedTable = new JTable( fixedModel );
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane( table );
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane fixedScroll = new JScrollPane( fixedTable ) {
public void setColumnHeaderView(Component view) {} // work around
// fixedScroll.setColumnHeader(null);
fixedScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollBar bar = fixedScroll.getVerticalScrollBar();
JScrollBar dummyBar = new JScrollBar() {
public void paint(Graphics g) {}
dummyBar.setPreferredSize(bar.getPreferredSize());
fixedScroll.setVerticalScrollBar(dummyBar);
final JScrollBar bar1 = scroll.getHorizontalScrollBar();
JScrollBar bar2 = fixedScroll.getHorizontalScrollBar();
bar2.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
bar1.setValue(e.getValue());
scroll.setPreferredSize(new Dimension(400, 100));
fixedScroll.setPreferredSize(new Dimension(400, 52)); // Hmm...
getContentPane().add( scroll, BorderLayout.CENTER);
getContentPane().add(fixedScroll, BorderLayout.SOUTH);
public static void main(String[] args) {
FixedRowExample frame = new FixedRowExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
frame.pack();
frame.setVisible(true);Sorry, this code missed a very important line:
table.setAutoCreateRowSorter(true);
So you meant:
Object[][] header;//class atribute
header=new Object[][]{//in the constructor
{ "h","i","j","k","l","m"},
{ "h","i","j","k","l","m"}
public Object getValueAt(int row, int col) {//in the TableModel
if (row==0){
return header[row][col];
else {
return data[row][col];
I tried, but when I sort, the first row get sorted with the rest. -
How to set background color of row in JTable
Hi,I want to set different background color to rows in JTable according to some value in the this row.
eg.
no name isGood
1 aaa yes (this row's background color is red)
2 bbb no (this row's background color is blue)
3 ccc yes (this row's background color is red)
4 ddd yes (this row's background color is red)
5 eee no (this row's background color is blue)
thanksthanks!*_*
-
How to set background color in row of JTable ?
i am new in java please tell me about How to set background color in row of JTable ? please example code. Thnak you.
Here is an example: http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html
For more info on how to use tables read the swing tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
ICE -
How to hide row in JTable?
Hi all,
How to hide some specific rows in JTable for user view filtering purpose?
ThanksTry to use the Table Model.
The "getValueAt" Methode decide what to Display.
So a simple "if" command can hide the complete row - or a single Statement.
public Object getValueAt(int row, int col) {
ArrayList al = new ArrayList();
StueliTeil tabellenzeile = (StueliTeil) getDaten().get(row);
switch (col) {
case 0 :
return tabellenzeile.getUmfang();
case 1 :
return tabellenzeile.getTakt();
... -
Inserting row in JTable (runtime)
i need to inserting row in JTable in runtime so tell me what it do
dear farhanaj ,
try this code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
/* <applet code=tableadd.class width=200 height=200>
</applet>
public class tableadd extends JApplet implements ActionListener
Object[] data = new Object[5];
DefaultTableModel defaulttablemodel = new DefaultTableModel();
JTable jtable=new JTable(defaulttablemodel);
JPanel jpanel= new JPanel();
private JPanel topPanel,jpPanel;
public JTable table;
JButton jbutton1 = new JButton("create new row");
JButton jbutton2 = new JButton("create new col");
//JButton jbutton1=new Jbutton("r"),jbutton2=new Jbutton("c");
public tableadd()
for(int column=0 ; column<5 ; column++)
defaulttablemodel.addColumn("column" + column);
for(int row=0 ; row<5;row++)
for(int column=0 ;column<5;column++)
data[column]="cell" row "," + column;
defaulttablemodel.addRow(data);
//getContentPane().add(new JScrollPane(jtable) ,BorderLayout.CENTER);
//getContentPane().add(new JPanel() ,BorderLayout.SOUTH);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jtable , v, h);
getContentPane().add(jsp, BorderLayout.CENTER);
topPanel = new JPanel();
jpPanel=new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel,BorderLayout.NORTH);
getContentPane().add(jpPanel,BorderLayout.SOUTH);
jpPanel.add(jbutton1);
jpPanel.add(jbutton2);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actionPerformed(ActionEvent e)
if (e.getSource() == jbutton1)
int numberrows = defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
Object[] data = new Object[numbercolumns];
for(int column=0 ; column<numbercolumns;column++)
data[column]="cell" numberrows "," +column;
defaulttablemodel.addRow(data);
else
if (e.getSource() ==jbutton2)
int numberrows=defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
defaulttablemodel.addColumn("column" + numbercolumns);
for(int row=0; row<numberrows ; row++)
defaulttablemodel.setValueAt("cell" row "," +numbercolumns ,row , numbercolumns);
System.out.println(row);
//jtable.sizeColumnsToFit(0); -
How to set different font for a particular row in jtables?
How to set different font size and font type for a particular row in jtable?
More than enough sample code here:
[http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]
db -
How to select a row in Jtable at runtime
how to select a row in Jtable at runtime.
use
setRowSelectionInterval(int fromRowIndex, int toRowIndex);example if your table has 10 rows then u want to select the rows from 4 to 8 then use
setRowSelectionInterval(3, 7);if you want to select just one row for example 5 then use
setRowSelectionInterval(5, 5); -
How to remove a row from JTable
Hi!
I'm used to remove rows from JTables getting the model and doing a removeRow(num) like this:
((DefaultTableModel)jTable1.getModel()).removeRow(0);
But with ADF and JDeveloper the model says it's a JUTableBinding.JUTableModel but its not accessible.
How to remove a row in Jdeveloper 10.1.3.4.0?Or maybe is just better to refresh data in the jTable but I do not know either like doing it.
-
How to apply different colors to specific rows in JTable
hi,
Anybody could tell me the way of colorising the rows in jtable.. I want to apply different colors to diff rows..
If i get a piece of code that could help me a lot..
thanks in advance,
Sapnayou'll find the answer at http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
-
Hello I am new To JAVA SWING
I have 2 questions related to JTable
1) How do i remove selected row in JTable ...for instance, I click on the row to delete (It is selected) , later I press the delete button, and then row is removed .... any hints ?
2) If I would like to save the rows in a JTable into a database , .... How can iterate through the rows in the JTable..each row .... the rows are saved in a List object...and a List Object is what I pass as a parameter in a class method in order to be saved into Database.
Thanks,
deibys1) use the removeRow(...) method of the DefaultTableModel
20 use table.getModel().getValueAt(...) to get the data from the model and create your list Object. -
Problem in adding/deleting rows in JTable
I am trying to add /remove rows from JTable whose first column is JButton and others are JComboBox's.If no rows are selected,new row is added at the end.If user selects some row & then presses insert button,new row is added below it.Rows can only be deleted if user has made some selection.Kindly help me,where i am making mistake.If any function is to be used.My code is as follows....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class JButtonTableExample extends JFrame implements ActionListener{
JComboBox mComboLHSType = new JComboBox();
JComboBox mComboRHSType = new JComboBox();
JLabel mLabelLHSType = new JLabel("LHS Type");
JLabel mLabelRHSType = new JLabel("RHS Type");
JButton mButtonDelete = new JButton("Delete");
JButton mButtonInsert = new JButton("Insert");
JPanel mPanelButton = new JPanel();
JPanel mPanelScroll = new JPanel();
JPanel mPanelCombo = new JPanel();
DefaultTableModel dm ;
JTable table;
int currentRow = -1;
static int mSelectedRow = -1;
public JButtonTableExample()
super( "JButtonTable Example" );
makeForm();
setSize( 410, 222 );
setVisible(true);
private void makeForm()
this.getContentPane().setLayout(null);
mPanelCombo.setLayout(null);
mPanelCombo.setBorder(new LineBorder(Color.red));
mPanelCombo.setBounds(new Rectangle(1,1,400,30));
mLabelLHSType.setBounds(new Rectangle(26,5,71,22));
mComboLHSType.setBounds(new Rectangle(83,5,100,22));
mLabelRHSType.setBounds(new Rectangle(232,5,71,22));
mComboRHSType.setBounds(new Rectangle(292,5,100,22));
mPanelCombo.add(mLabelLHSType,null);
mPanelCombo.add(mComboLHSType,null);
mPanelCombo.add(mLabelRHSType,null);
mPanelCombo.add(mComboRHSType,null);
mPanelScroll.setLayout(null);
mPanelScroll.setBorder(new LineBorder(Color.blue));
mPanelScroll.setBounds(new Rectangle(1,28,400,135));
mPanelButton.setLayout(null);
mPanelButton.setBorder(new LineBorder(Color.green));
mPanelButton.setBounds(new Rectangle(1,165,400,30));
mButtonInsert.setBounds(new Rectangle(120,5,71,22));
mButtonDelete.setBounds(new Rectangle(202,5,71,22));
mButtonDelete.addActionListener(this);
mButtonInsert.addActionListener(this);
mPanelButton.add(mButtonDelete,null);
mPanelButton.add(mButtonInsert,null);
dm = new DefaultTableModel();
//dm.setDataVector(null,
//new Object[]{"Button","Join","LHS","Operator","RHS"});
dm.setDataVector(new Object[][]{{"","","","",""}},
new Object[]{"","Join","LHS","Operator","RHS"});
table = new JTable(dm);
table.getTableHeader().setReorderingAllowed(false);
table.setRowHeight(25);
int columnWidth[] = {20,45,95,95,95};
TableColumnModel modelCol = table.getColumnModel();
for (int i=0;i<5;i++)
modelCol.getColumn(i).setPreferredWidth(columnWidth);
//modelCol.getColumn(0).setCellRenderer(new ButtonRenderer());
//modelCol.getColumn(0).setCellEditor(new ButtonEditor(new JCheckBox()));
modelCol.getColumn(0).setCellRenderer(new ButtonCR());
modelCol.getColumn(0).setCellEditor(new ButtonCE(new JCheckBox()));
modelCol.getColumn(0).setResizable(false);
setUpJoinColumn(modelCol.getColumn(1));
setUpLHSColumn(modelCol.getColumn(2));
setUpOperColumn(modelCol.getColumn(3));
setUpRHSColumn(modelCol.getColumn(4));
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(new Rectangle(1,1,400,133));
mPanelScroll.add(scroll,null);
this.getContentPane().add(mPanelCombo,null);
this.getContentPane().add(mPanelScroll,null);
this.getContentPane().add(mPanelButton,null);
}//end of makeForm()
public void actionPerformed(ActionEvent ae)
if (ae.getSource() == mButtonInsert)
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before Insert CURRENT ROW"+currentRow);
if(currentRow == -1)
int rowCount = dm.getRowCount();
//mSelectedRow = rowCount-1;
//table.clearSelection();
dm.insertRow(rowCount,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
else
table.clearSelection();
dm.insertRow(currentRow+1,new Object[]{"","","","",""});
currentRow = -1;
ButtonCE.selectedRow = -1;
//System.out.println("After INSERT CURRENT ROW"+currentRow);
if(ae.getSource() == mButtonDelete)
//int currentRow = table.getSelectedRow();
currentRow = ButtonCE.selectedRow;
System.out.println("Before DELETE CURRENT ROW"+currentRow);
if(currentRow != -1)
dm.removeRow(currentRow);
table.clearSelection();
currentRow = -1;
ButtonCE.selectedRow = -1;
//System.out.println("Selected Row"+mSelectedRow);
else
JOptionPane.showMessageDialog(null, "Select row first", "alert", JOptionPane.ERROR_MESSAGE);
//System.out.println("DELETE CURRENT ROW"+currentRow);
public void setUpJoinColumn(TableColumn joinColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("AND");
comboBox.addItem("OR");
comboBox.addItem("NOT");
joinColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
joinColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = joinColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpLHSColumn(TableColumn LHSColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Participant1");
comboBox.addItem("Participant2");
comboBox.addItem("Variable1");
LHSColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
LHSColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = LHSColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpOperColumn(TableColumn operColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("=");
comboBox.addItem("!=");
comboBox.addItem("Contains");
operColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
operColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = operColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public void setUpRHSColumn(TableColumn rhsColumn)
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Variable1");
comboBox.addItem("Constant1");
comboBox.addItem("Constant2");
rhsColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
rhsColumn.setCellRenderer(renderer);
//Set up tool tip for the sport column header.
TableCellRenderer headerRenderer = rhsColumn.getHeaderRenderer();
if (headerRenderer instanceof DefaultTableCellRenderer) {
((DefaultTableCellRenderer)headerRenderer).setToolTipText(
"Click the sport to see a list of choices");
public static void main(String[] args) {
JButtonTableExample frame = new JButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
//Button as a renderer for the table cells
class ButtonCR implements TableCellRenderer
JButton btnSelect;
public ButtonCR()
btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
if (column != 0) return null; //meany !!!
//System.out.println("Inside renderer########################Selected row");
//btnSelect.setText(value.toString());
//btnSelect.setIcon(new ImageIcon("capsigma.gif"));
return btnSelect;
}//end fo ButtonCR
//Default Editor for table
class ButtonCE extends DefaultCellEditor implements ActionListener
JButton btnSelect;
JTable table;
//Object val;
static int selectedRow = -1;
public ButtonCE(JCheckBox whoCares)
super(whoCares);
//this.row = row;
btnSelect = new JButton();
btnSelect.setMargin(new Insets(0,0,0,0));
btnSelect.addActionListener(this);
setClickCountToStart(1);
public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column)
if (column != 0) return null; //meany !!!
this.selectedRow = row;
this.table = table;
table.clearSelection();
System.out.println("Inside getTableCellEditorComponent");
return btnSelect;
//public Object getCellEditorValue()
//return val;
public void actionPerformed(ActionEvent e)
// Your Code Here...
System.out.println("Inside actionPerformed");
System.out.println("Action performed Row selected "+selectedRow);
btnSelect.setIcon(new ImageIcon("capsigma.gif"));
}//end of ButtonCEHi,
All the thing you have to do is to return a boolean for the column. JTable will use a checkbox (as default) to show boolean values. -
Please I need help
How can I hide a row in Jtable.
For example if you push a button hide first row.
ThanksOne simple way you can do is:
just remove this row but keep it somewhere so that you may add it back when you want later. :-) -
Dear friends,
When i select a whole row in JTable it gets selected but the background color of the row selected does not change. i.e. Its not highlighted as in MS-Excel.
How can i overcome this problem?
Kindly reply
Thanks.Hi,
Thanx for ur inputs. This is what the code i wrote. When u run this code,
If we select row1 column1 the cells highlighted will be on row 2, 3 and 4. If we select row1 column 2 the selected cells will be on row 1, 3 and 4. I want the row to be selected fully when we select coumn 1 only.
For eg. if i select row 200 from column 1 the 200 th row should only be fully highlighted
Anyway i'll try the above code and check
Here is the code
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class table1 extends JFrame
// Variables declaration
private JTable jTable1;
private JScrollPane jScrollPane1;
private JPanel contentPane;
// End of variables declaration
public table1()
super();
this.setVisible(true);
private void initializeComponent()
jTable1 = new JTable();
jScrollPane1 = new JScrollPane();
contentPane = (JPanel)this.getContentPane();
jTable1.setModel(new DefaultTableModel(4, 4));
jScrollPane1.setViewportView(jTable1);
contentPane.setLayout(null);
addComponent(contentPane, jScrollPane1, 58,65,301,207);
this.setTitle("table1 - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(473, 539));
private void addComponent(Container container,Component c,int x,int
y,int width,int height)
c.setBounds(x,y,width,height);
container.add(c);
} Thanks again
Maybe you are looking for
-
Move up one level in path and make new folder
How do I get applescript to make a new folder one level up from myFolder i.e. set myFolder to ¬ (choose folder with prompt "Where are the image files for which you want to change exif data ?") as reference make new folder at myFolder with properties
-
It could be a coincidence but when I upgraded to the latest version of Adobe Flash Player 11.3.300.262 I can't seem to play any games on Yahoo Messenger with my friends. I know when I updated for some reason I had to try 3 times before it took it. It
-
most of my applications on my macbook will not open (ichat, mail, iphoto, etc..). It tells me that the application has quit unexpectedly, mac OS X and other applications are not affected. Click relaunch to launch the application again. Click report t
-
How I can eliminate commas in a concatenation when the records is not there
I am getting these results Holly Eliza Congdon, 2002, Son, John Harvey , 1981, other, , , Peter Allen, 1966, other, Duke Beardsley , 1992, other, , , Nellie Wood, 2011, Son, , , , , , Shawn Burns, 1986, other, , , , , , Jay Taylor, 1977, , , , , , ,
-
I have a sole i-pod and two pc's, used the instructions for "iTunes for Windows: Moving your iTunes Media folder" in the hope that I could synch my one i-pod between two different computers that are sharing the same library. Is it possible to have tw