aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable ........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable ........" Watch "JTable ........" New topic
Author

JTable ........

Ajit B
Greenhorn

Joined: Jul 24, 2001
Posts: 28
hello friends
i want to have one JTable having 2 columns and number of rows. now in the first column i want to add JButtons and in the second there is going to be some text.
now i m not getting how to add Button to cell. what i tried thro' constructor was something like this.........
Object lb1 = new JButton("click......");
Object lb2 = new JButton("click......");
Object lb3 = new JButton("click......");
Object lb4 = new JButton("click......");
Object lb5 = new JButton("click......");

Object [][] data = {{lb1},{lb2},{lb3},{lb4},{lb5}};
Object [] value ={"label1","label2","label3","label4","label5"};

JFrame jf = new JFrame ();
jf.getContentPane().add(new JTable(data,value));
jf.setVisible(true);
................but its not showing JTable plus throwing Exception ArrayIndexOutOfBounds..........thx in advance...........Ajit B
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Ajit,
Look here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Enjoy,
Manfred.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
First
Object [][] data = {{lb1},{lb2},{lb3},{lb4},{lb5}};
Object [] value ={"label1","label2","label3","label4","label5"};
Does not create 2 columns. You have 5 with none being text.
Object [][] data = {{lb1, "aaa"},{lb2,"bbb"},{lb3,"ccc"},{lb4,"ddd"},{lb5,"eee"}};
Object [] value ={"label1","label2};
That creates 2 columns. Here is a link that shows how to put buttons in a JTable. SwingExamples

Ajit B
Greenhorn

Joined: Jul 24, 2001
Posts: 28
thx for replying............now its showing JTable .......but what i wanted was to have buttons in first column. instead of that it shows text "javax.swing.JButton".
............plz help me out......Ajit B
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Check the site I gave you (example 2). You need to create a renderer for the button. The default is JLabel. So you need your own.
Ajit B
Greenhorn

Joined: Jul 24, 2001
Posts: 28
thx paul..........
i hv created renderer for the Button........
it helped me.........the JButtons are visible but if i double click, Button disappers and it shows text in which all attributes of that particular button is shown.
i hv also checked the example u suggested . i guess i need to create editor for JButton......now in the said example to create editor it extends DefaultCellEditor........but i can't do this in case of JButton coz DefaultCellEditor constructor takes either JCheckBox, JComboBox or JTextField. There is no constructor which takes JButton as argument..........so my question is how will i create Editor for JButton?
or is there any other way to avoid disappearing of JButton when double clicked?
thx in advance.............Ajit B
Ajit B
Greenhorn

Joined: Jul 24, 2001
Posts: 28
thx paul..........
i hv created renderer for the Button........
it helped me.........the JButtons are visible but if i double click, Button disappers and it shows text in which all attributes of that particular button is shown.
i hv also checked the example u suggested . i guess i need to create editor for JButton......now in the said example to create editor it extends DefaultCellEditor........but i can't do this in case of JButton coz DefaultCellEditor constructor takes either JCheckBox, JComboBox or JTextField. There is no constructor which takes JButton as argument..........so my question is how will i create Editor for JButton?
or is there any other way to avoid disappearing of JButton when double clicked?
thx in advance.............Ajit B
Ajit B
Greenhorn

Joined: Jul 24, 2001
Posts: 28
hello once again.........
here is the code for those who want to help me......
//------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.table.*;
public class JButtonInJTable extends JFrame implements ActionListener
{
JTable table;
DefaultTableModel dModel;
DefaultTableModel dm;
/** Creates new TryLegendJTable */
public JButtonInJTable ()
{
JPanel panel = new JPanel();
panel.add(new JButton("aaa"));
Object [][] data = {{ new JButton("A"),"aaa"},{new JButton("B"),"bbb"},{new JButton("C"),"ccc"},{new JButton("D"),"ddd"},{new JButton("E"),"eee"}};
Object [] value ={"label1","label2"};
dModel = new DefaultTableModel(data,value);

table = new JTable(dModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 80));
table.setRowSelectionAllowed(true);
JButtonRenderer jbr = new JButtonRenderer();
table.getColumn("label1").setCellRenderer(jbr);
//table.getColumn("label1").setCellEditor(new JButtonEditor(new JButton()));
JScrollPane scrollPane = new JScrollPane(table);

JButton addRow = new JButton("Add Row"); addRow.addActionListener(this);
JButton removeRow = new JButton("Remove Row"); removeRow.addActionListener(this);

JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();

centerPanel.add(scrollPane);
southPanel.add(addRow);
southPanel.add(removeRow);

this.getContentPane().add(centerPanel,BorderLayout.CENTER);
this.getContentPane().add(southPanel,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,200,200);
pack();
}

public static void main(String [] a)
{
new TryLegendJTable();
}

public void actionPerformed(java.awt.event.ActionEvent event)
{
if(event.getActionCommand()=="Add Row")
{
Object[] a = {new JButton("click.."),"new Row"};
dModel.addRow(a);

}
else if(event.getActionCommand()=="Remove Row")
{
try{
dModel.removeRow(table.getSelectedRow());
}catch(Exception e)
{
dModel.removeRow(0);
}
}
else
{
System.out.println("other event");
}
}

class JButtonRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
if (value==null) return null;
System.out.println("inside getTableCellRendererComponent");
return (Component)value;
}


}



}
////------------------------------------------------------------

the JButtons are visible but if i double click, Button disappers and it shows text in which all attributes of that particular button is shown. ........how to avoid this?..........
.........waiting for help........Ajit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable ........