• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable ........

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajit,
Look here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
Enjoy,
Manfred.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic