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
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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);
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