To refresh the contents in JTextArea on selection of a row in JTable.
This is the block of code that i have tried :
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import ui.layouts.ComponentsBox;
import ui.layouts.GridPanel;
import ui.layouts.LayoutConstants;
import util.ui.UIUtil;
public class ElectronicJournal extends GridPanel {
private boolean DEBUG = false;
private boolean ALLOW_COLUMN_SELECTION = false;
private boolean ALLOW_ROW_SELECTION = true;
private GridPanel jGPanel = new GridPanel();
private GridPanel jGPanel1 = new GridPanel();
private GridPanel jGPanel2 = new GridPanel();
DefaultTableModel model;
private JLabel jLblTillNo = UIUtil.getHeaderLabel("TillNo :");
private JLabel jLblTillNoData = UIUtil.getBodyLabel("TILL123");
private JLabel jLblData = UIUtil.getBodyLabel("Detailed View");
private JTextArea textArea = new JTextArea();
private JScrollPane spTimeEntryView = new JScrollPane();
private JScrollPane pan = new JScrollPane();
String html= " Item Description: Price Change \n Old Price: 40.00 \n New Price: 50.00 \n Authorized By:USER1123 \n";
private JButton jBtnExporttoExcel = UIUtil.getButton(85,
"Export to Excel - F2", "");
final String[] colHeads = { "Task No", "Data", "User ID", "Date Time",
"Description" };
final Object[][] data = {
{ "1", "50.00", "USER123", "12/10/2006 05:30", "Price Change" },
{ "2", "100.00", "USER234", "15/10/2006 03:30", "Price Change12345"},
};
final String[] colHeads1 = {"Detailed View" };
final Object[][] data1 = {
{ "Task:Price Change", "\n"," Old Price:50.00"," \n ","New Price:100.00"," \n" }
};
JTable jtblTimeEntry = new JTable(data, colHeads);
JTable jTbl1 = new JTable(data1,colHeads1);
ComponentsBox jpBoxButton = new ComponentsBox(LayoutConstants.X_AXIS);
public ElectronicJournal() {
super();
jtblTimeEntry.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = jtblTimeEntry.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
int selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row " + selectedRow
+ " is now selected.");
textArea.append("asd \n 123 \n");
}
}
});
} else {
jtblTimeEntry.setRowSelectionAllowed(false);
}
if (DEBUG) {
jtblTimeEntry.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(jtblTimeEntry);
}
});
}
initialize();
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print("row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--");
}
private void initialize() {
this.setSize(680, 200);
this.setBackground(java.awt.SystemColor.control);
jBtnExporttoExcel.setBackground(SystemColor.control);
ComponentsBox cmpRibbonHORZ = new ComponentsBox(LayoutConstants.X_AXIS);
cmpRibbonHORZ.addComponent(jBtnExporttoExcel, false);
jpBoxButton.add(cmpRibbonHORZ);
this.addFilledComponent(jGPanel, 1, 1, 1, 1, GridBagConstraints.BOTH);
this.addFilledComponent(jGPanel1, 2, 1, 11, 5, GridBagConstraints.BOTH);
this.addFilledComponent(jGPanel2, 2, 13, 17, 5, GridBagConstraints.BOTH);
jGPanel.setSize(650, 91);
jGPanel.setBackground(SystemColor.control);
jGPanel.addFilledComponent(jLblTillNo,1,1,GridBagConstraints.WEST);
jGPanel.addFilledComponent(jLblTillNoData,1,10,GridBagConstraints.BOTH);
jGPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0));
jGPanel1.setBackground(SystemColor.control);
jGPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0,
0));
spTimeEntryView.setViewportView(jtblTimeEntry);
jGPanel1.addFilledComponent(spTimeEntryView, 1, 1, 11, 4,
GridBagConstraints.BOTH);
jGPanel2.addFilledComponent(jLblData,1,1,GridBagConstraints.WEST);
jGPanel2.setBackground(SystemColor.control);
jGPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0,
0));
//textArea.setText(html);
pan.setViewportView(textArea);
int selectedRow = jTbl1.getSelectedRow();
System.out.println("selectedRow ::" +selectedRow);
int colCount = jTbl1.getColumnCount();
System.out.println("colCount::" +colCount);
StringBuffer buf = new StringBuffer();
System.out.println("Out Of For");
/*for(int count =0;count<colCount;count++)
{System.out.println("Inside For");
buf.append(jTbl1.getValueAt(selectedRow,count));
// method 1 : Constructs a new text area with the specified text.
textArea =new JTextArea(buf.toString());
//method 2 :To Append the given string to the text area's current text.
textArea.append(buf.toString());
}*/
jGPanel2.addFilledComponent(pan,2,1,5,5,GridBagConstraints.BOTH);
this.addAnchoredComponent(jpBoxButton, 7, 5, 11, 5,
GridBagConstraints.CENTER);
}
}
This code displays the same data on the JTextArea everytime i select each row,but my requirement is ,it has to refresh and display different datas in the JTextArea accordingly,as i select each row.Please help.Its urgent.
Message was edited by: Samyuktha
Samyuktha>

