JTable problem..

Really need help.. urgent..

I'm creating a JTable which obtains its data from a database. This part i manage to get it up and working. My problem now is inserting a JButton (e.g. Edit, View, Delete) for user functionality.

I tried a few ways and also read thru forums with no progress. Then i found out that JTable treats every cell as a JLable. I'm stuck.

Thanks for your help in advance.

[417 byte] By [-TiKuS-a] at [2007-10-3 8:54:27]
# 1
Do you mean you are going to make JButtons table cells?
hiwaa at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

I just recently figured this out myself here is what I do to add buttons to a JTable.

This is the code I add to my display

//makes the buttons in the strategy report column

JTable table = new JTable();

table .getColumn("column name").setCellRenderer(new ButtonRenderer());

table .getColumn("column name").setCellEditor(new ButtonEditor(new JCheckBox()));

This is the class I add to my prgram to handle the buttons.

class ButtonRenderer extends JButton implements TableCellRenderer

{

public ButtonRenderer()

{

setOpaque(true);

}

public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)

{

if(value==null)

setText("");

else

setText(value.toString());

return this;

}

}

class ButtonEditor extends DefaultCellEditor

{

protected JButton button;

private String label;

private boolean buttonIsPushed;

public ButtonEditor(JCheckBox checkBox)

{

super(checkBox);

button = new JButton();

button.setOpaque(true);

button.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

fireEditingStopped();

}

});

}

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column)

{

if(value==null)

label="";

else

label = value.toString();

button.setText(label);

buttonIsPushed = true;

return button;

}

public Object getCellEditorValue()

{

if (buttonIsPushed)

{

int selectedRow = marketTable.getSelectedRow();

if(selectedRow<numValidRows)

{

/********************************

put your code of what you want to happen when button is pushed.

*************************************/

}

buttonIsPushed = false;

return new String(label) ;

}

public boolean stopCellEditing()

{

buttonIsPushed = false;

return super.stopCellEditing();

}

protected void fireEditingStopped()

{

super.fireEditingStopped();

}

}

I hope this helps.

Cheers,

Ricky>

Saintsairforcea at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yeah.. i wanna put a JButton inside a Jtable cell..
-TiKuS-a at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
Saintsairforce : Thanks alot for the code.. really appreciate it.. i kinda understand wat the 2 classes do but under the ButtonEditor class, getCellEditorValue() method has a marketTable and numValidRows variable. I don't seem to be able to find how it got there.. mind helping out?
-TiKuS-a at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hehe i got it running liao.. thanks alot.. a billion..
-TiKuS-a at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

Saintsairforce's implementation has some specific dependency or coupling.

I think camickr's implementation is much more generic, standard, simple, reusable and scalable.

Study it if you have time and guts.

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=680674

hiwaa at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

I have another problem. Now each button has a name example Edit. Then the button has a value which stores the value which is the primary key of a table in the database. When the user clicks on the button, it will retrieve the value from the button.

I have used the button's setActionCommand() to store this value which i'm not sure is the proper way but it works. When i tried to inser the value into the buttons using the buttonEditor and buttonRenderer, it always gets the last value as the values of the whole buttons in the table. How do u assign each value to each button?

Thanks in advance.

-TiKuS-a at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...
# 8
Your conditionals on 'row' parameter is wrong, I blindly guess.
hiwaa at 2007-7-15 4:04:26 > top of Java-index,Desktop,Core GUI APIs...