Headache: Data Xchange from JDialog to JTable using JButton

My head is spinning about how to get this to work. What is really confusing me is the data exchange from the JDialog.

I have a JTable where one of the columns uses a JButton as the cell editor for each cell in that column. That part seems to be no problem. The JButton is labeled "Define Staff..."

When the user clicks on the JButton, a JDialog containing a JList is to appear. If the user clicks cancel, the JDialog is disposed and nothing happens. If the user clicks OK, the selected items are to be returned to the cell in some way and the label of the JButton is to change to "Modify Staff..."

How can I return the list from the JDialog to the "cell" so that when I access the cell using getValueAt() I get the list of values, and not the JButton object?

[783 byte] By [AsymptoticCodera] at [2007-10-2 21:16:02]
# 1

simple demo of one way

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Testing extends JFrame

{

JLabel lbl = new JLabel("Name = ");

public Testing()

{

setTitle("JFrame");

setSize(150,75);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocation(400,300);

JPanel main = new JPanel(new BorderLayout());

main.add(lbl,BorderLayout.NORTH);

JButton btn = new JButton("Enter name");

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

String returnedName = new MyDialog(Testing.this).getName();

if(returnedName != null) lbl.setText("Name = "+returnedName);

}});

main.add(btn,BorderLayout.SOUTH);

getContentPane().add(main);

}

public static void main(String[] args){new Testing().setVisible(true);}

}

class MyDialog extends JDialog

{

String name = null;

public MyDialog(JFrame parent)

{

super(parent);

setTitle("JDialog");

setModal(true);

setSize(150,75);

setLocation(400,300);

final JTextField tf = new JTextField(10);

getContentPane().add(tf,BorderLayout.NORTH);

JButton btn1 = new JButton("OK");

btn1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

name = tf.getText();

dispose();}});

getContentPane().add(btn1);

setVisible(true);

}

public String getName(){return name;}

}

Michael_Dunna at 2007-7-14 0:24:03 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks.

The actual JButton is inside a JTable cell though.

Where would I add the event handler for the button? In the editor? In the renderer?

Excerpt from the JTable file:

PositionTable.getColumn("Staff").setCellRenderer(new ButtonRenderer());

PositionTable.getColumn("Staff").setCellEditor(new ButtonEditor(new JCheckBox()));

ButtonEditor.java

public class ButtonEditor extends DefaultCellEditor {

protected JButton button;

private Stringlabel;

private booleanisPushed;

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 (isSelected) {

} else{

button.setForeground(table.getForeground());

button.setBackground(table.getBackground());

}

label = (value ==null) ? "" : value.toString();

isPushed = true;

return button;

}

public Vector<String> getCellEditorValue() {

Vector<String> staff = new Vector<String>();

if (isPushed) {

staffSelector temp = new staffSelector(null,true,"Building Supervisor");

temp.setVisible(true);

staff = temp.getSelectedStaff(true);

}

isPushed = false;

// return new String( label ) ;

//Return vector people selected.

return staff;

}

public boolean stopCellEditing() {

isPushed = false;

return super.stopCellEditing();

}

protected void fireEditingStopped() {

super.fireEditingStopped();

}

}

ButtonRenderer.java

public 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 (isSelected) {

setForeground(table.getForeground());

setBackground(table.getBackground());

} else{

setForeground(table.getForeground());

setBackground(UIManager.getColor("Button.background"));

}

setText( (value ==null) ? "Define Staff..." : "Modify Staff..." );

return this;

}

}

Message was edited by:

AsymptoticCoder

Made an error.

AsymptoticCodera at 2007-7-14 0:24:03 > top of Java-index,Desktop,Core GUI APIs...