JtextArea not updating

Hi I have a strange problem with appending text to a JTextArea.

Basically I have a JinternalFrame with a table. When the user clicks on a particular row, I want that some of the data in that row to be displayed in a dialog which has a JTextArea included.

here is part of the code of from the JinternalFrame that calls the Dialog

private JTable getStatTable(){

if (cTable ==null){

cTable =new JTable(){

privatestaticfinallong serialVersionUID = 1;

public TableCellEditor getCellEditor(int row,int column)

{

if (column == 1 && !caseTableEditors.isEmpty())

{

return (TableCellEditor)caseTableEditors.get(row);

}else{

return super.getCellEditor(row, column);

}

}

};

cTable.setSize(new java.awt.Dimension(650,140));

cTable.setMinimumSize(new java.awt.Dimension(650,140));

cTable.setMaximumSize(new java.awt.Dimension(650,140));

cTable.setShowGrid(true);

cTable.setPreferredSize(new java.awt.Dimension(650,140));

cTable.setPreferredScrollableViewportSize(new java.awt.Dimension(650,140));

cModel=new DefaultTableModel(20,2);

cTable.setModel(caseModel);

TableColumnModel cModel = cTable.getColumnModel();

TableColumn ta=cModel.getColumn(0);

ta.setHeaderValue("ID");

ta.setPreferredWidth(100);

TableColumn tb = cModel.getColumn(1);

tb.setPreferredWidth(10);

tb.setHeaderValue("Value");

cTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

if (ALLOW_ROW_SELECTION){// true by default

ListSelectionModel rowSM = caseStatTable.getSelectionModel();

rowSM.addListSelectionListener(new ListSelectionListener(){

publicvoid 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();

displayRow(cTable.getValueAt(selectedRow,0));

}

}

});

}else{

cTable.setRowSelectionAllowed(false);

}

}

return cTable;

}

the code of the displayRow method is below:

publicvoid displayRow(Object o){

if(oinstanceof String){

SDisplay sd=new SDisplay(this,(String)o);

sd.display();

}

}

and below is the SDisplay Dialog class:

publicclass SDisplayextends JDialogimplements Runnable{

privatestaticfinallong serialVersionUID = 1;

privateshort selectedOption = -1;

private JPanel jContentPane =null;

private JPanel btnPnl =null;

private JScrollPane displayPnl =null;

private JTextArea cInfo =null;

private JButton okBtn =null;

private JButton cnlBtn =null;

int x,y;

String cr=null;

public SDisplay(Frame owner,boolean modal){

super(owner);

initialize();

}

//PreviousClass is a user defined class that extends JInternalFrame

public SDisplay(PreviousClass jp, String cr){

x=jp.getLocationOnScreen().x;

y=jp.getLocationOnScreen().y;

this.cr=cr;

initialize();

}

/**

* This method initializes this

*

* @return void

*/

privatevoid initialize(){

this.setSize(500, 400);

this.setModal(true);

this.setTitle("Display");

this.setMaximumSize(new Dimension(500, 400));

this.setMinimumSize(new Dimension(500, 400));

this.setPreferredSize(new Dimension(500, 400));

this.setLocation(x,y);

this.setContentPane(getJContentPane());

this.setVisible(true);

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane(){

if (jContentPane ==null){

GridBagConstraints gridBagConstraints1 =new GridBagConstraints();

gridBagConstraints1.fill = GridBagConstraints.BOTH;

gridBagConstraints1.gridy = 0;

gridBagConstraints1.weightx = 1.0;

gridBagConstraints1.weighty = 1.0;

gridBagConstraints1.gridx = 0;

GridBagConstraints gridBagConstraints =new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 1;

jContentPane =new JPanel();

jContentPane.setLayout(new GridBagLayout());

jContentPane.setPreferredSize(new Dimension(500, 400));

jContentPane.setMaximumSize(new Dimension(500, 400));

jContentPane.setMinimumSize(new Dimension(500, 400));

jContentPane.add(getBtnPnl(), gridBagConstraints);

jContentPane.add(getDisplayPnl(), gridBagConstraints1);

}

return jContentPane;

}

/**

* This method initializes btnPnl

*

* @return javax.swing.JPanel

*/

private JPanel getBtnPnl(){

if (btnPnl ==null){

try{

GridBagConstraints gridBagConstraints3 =new GridBagConstraints();

gridBagConstraints3.gridx = 1;

gridBagConstraints3.insets =new Insets(0, 50, 0, 0);

gridBagConstraints3.gridy = 0;

GridBagConstraints gridBagConstraints2 =new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.insets =new Insets(0, 0, 0, 50);

gridBagConstraints2.gridy = 0;

btnPnl =new JPanel();

btnPnl.setLayout(new GridBagLayout());

btnPnl.setMinimumSize(new Dimension(500, 50));

btnPnl.setMaximumSize(new Dimension(500, 50));

btnPnl.setPreferredSize(new Dimension(500, 50));

btnPnl.add(getOkBtn(), gridBagConstraints2);

btnPnl.add(getCnlBtn(), gridBagConstraints3);

}catch (java.lang.Throwable e){

// TODO: Something

}

}

return btnPnl;

}

/**

* This method initializes displayPnl

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getDisplayPnl(){

if (displayPnl ==null){

try{

displayPnl =new JScrollPane();

displayPnl.setPreferredSize(new Dimension(500, 350));

displayPnl.setMaximumSize(new Dimension(500, 350));

displayPnl.setViewportView(getCInfo());

displayPnl.setMinimumSize(new Dimension(500, 350));

}catch (java.lang.Throwable e){

// TODO: Something

}

}

return displayPnl;

}

/**

* This method initializes cInfo

*

* @return javax.swing.JTextArea

*/

private JTextArea getCInfo(){

if (cInfo ==null){

try{

cInfo =new JTextArea();

cInfo.setSize(new Dimension(500, 350));

cInfo.setMaximumSize(new Dimension(500, 350));

cInfo.setPreferredSize(new Dimension(500, 350));

cInfo.setEditable(true);

cInfo.setLineWrap(true);

cInfo.setRows(100);

cInfo.setDoubleBuffered(true);

cInfo.setMinimumSize(new Dimension(500, 350));

}catch (java.lang.Throwable e){

// TODO: Something

}

}

return cInfo;

}

/**

* This method initializes okBtn

*

* @return javax.swing.JButton

*/

private JButton getOkBtn(){

if (okBtn ==null){

try{

okBtn =new JButton();

okBtn.setPreferredSize(new Dimension(70, 20));

okBtn.setMaximumSize(new Dimension(70, 20));

okBtn.setText("OK");

okBtn.setMinimumSize(new Dimension(70, 20));

okBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

SDisplay.this.setVisible(false);

}

});

}catch (java.lang.Throwable e){

// TODO: Something

}

}

return okBtn;

}

/**

* This method initializes cnlBtn

*

* @return javax.swing.JButton

*/

private JButton getCnlBtn(){

if (cnlBtn ==null){

try{

cnlBtn =new JButton();

cnlBtn.setMinimumSize(new Dimension(70, 20));

cnlBtn.setPreferredSize(new Dimension(70, 20));

cnlBtn.setText("Close");

cnlBtn.setMaximumSize(new Dimension(70, 20));

cnlBtn.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

SDisplay.this.setVisible(false);

}

});

}catch (java.lang.Throwable e){

// TODO: Something

}

}

return cnlBtn;

}

publicvoid run(){

cInfo.append("String to append: "+cr);

caseInfo.append("\n");

}

publicvoid display(){

new Thread(this).start();

}

}

I implemented The SDisplay to use a Thread to append the text, so that appending is independent of any other process.

When I execute this code, the jtextarea does not display any appended text. Can anyone explain what i am doing wrong?

Regards

[17172 byte] By [cicho_41a] at [2007-10-3 3:23:00]
# 1

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 21:15:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

Re-Hi,

I have arranged a bit the code and here I provide a better/shorter working example:

import javax.swing.UIManager;

import java.awt.*;

public class Main {

boolean packFrame = false;

//Construct the application

public Main() {

Display frame = new Display();

//Validate frames that have preset sizes

//Pack frames that have useful preferred size info, e.g. from their layout

if (packFrame) {

frame.pack();

}

else {

frame.validate();

}

//Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width > screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

frame.setVisible(true);

}

//Main method

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e) {

e.printStackTrace();

}

new Main();

}

}

import javax.swing.JPanel;

import java.awt.Frame;

import java.awt.BorderLayout;

import javax.swing.JDialog;

import java.awt.Dimension;

import java.awt.GridBagLayout;

import javax.swing.JTextArea;

import java.awt.GridBagConstraints;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.Insets;

import javax.swing.JScrollBar;

import javax.swing.JScrollPane;

import java.util.*;

public class Display extends JDialog implements Runnable{

private static final long serialVersionUID = 1;

public static short ADD_OPTION = 0;

public static short CANCEL_OPTION = 1;

private short selectedOption = -1;

private JPanel jContentPane = null;

private JPanel btnPnl = null;

private JScrollPane displayPnl = null;

private JTextArea caseInfo = null;

private JButton okBtn = null;

private JButton cnlBtn = null;

int x,y;

/**

* @param owner

*/

public Display(Frame owner, boolean modal) {

super(owner);

initialize();

}

public Display(){

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setSize(500, 400);

this.setModal(true);

this.setTitle("Case Display");

this.setLocation(x,y);

this.setContentPane(getJContentPane());

//this.setVisible(true);

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.fill = GridBagConstraints.BOTH;

gridBagConstraints1.gridy = 0;

gridBagConstraints1.weightx = 1.0;

gridBagConstraints1.weighty = 1.0;

gridBagConstraints1.gridx = 0;

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 1;

jContentPane = new JPanel();

jContentPane.setLayout(new GridBagLayout());

jContentPane.setPreferredSize(new Dimension(500, 400));

jContentPane.setMaximumSize(new Dimension(500, 400));

jContentPane.setMinimumSize(new Dimension(500, 400));

jContentPane.add(getBtnPnl(), gridBagConstraints);

jContentPane.add(getDisplayPnl(), gridBagConstraints1);

}

return jContentPane;

}

/**

* This method initializes btnPnl

*

* @return javax.swing.JPanel

*/

private JPanel getBtnPnl() {

if (btnPnl == null) {

try {

GridBagConstraints gridBagConstraints3 = new GridBagConstraints();

gridBagConstraints3.gridx = 1;

gridBagConstraints3.insets = new Insets(0, 50, 0, 0);

gridBagConstraints3.gridy = 0;

GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.insets = new Insets(0, 0, 0, 50);

gridBagConstraints2.gridy = 0;

btnPnl = new JPanel();

btnPnl.setLayout(new GridBagLayout());

btnPnl.setMinimumSize(new Dimension(500, 50));

btnPnl.setMaximumSize(new Dimension(500, 50));

btnPnl.setPreferredSize(new Dimension(500, 50));

btnPnl.add(getOkBtn(), gridBagConstraints2);

btnPnl.add(getCnlBtn(), gridBagConstraints3);

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return btnPnl;

}

/**

* This method initializes displayPnl

*

* @return javax.swing.JScrollPane

*/

private JScrollPane getDisplayPnl() {

if (displayPnl == null) {

try {

displayPnl = new JScrollPane();

displayPnl.setPreferredSize(new Dimension(500, 350));

displayPnl.setMaximumSize(new Dimension(500, 350));

displayPnl.setViewportView(getCaseInfo());

displayPnl.setMinimumSize(new Dimension(500, 350));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return displayPnl;

}

/**

* This method initializes caseInfo

*

* @return javax.swing.JTextArea

*/

private JTextArea getCaseInfo() {

if (caseInfo == null) {

try {

caseInfo = new JTextArea();

caseInfo.setSize(new Dimension(500, 350));

caseInfo.setMaximumSize(new Dimension(500, 350));

caseInfo.setPreferredSize(new Dimension(500, 350));

caseInfo.setEditable(true);

caseInfo.setLineWrap(true);

caseInfo.setRows(100);

caseInfo.setDoubleBuffered(true);

caseInfo.setMinimumSize(new Dimension(500, 350));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return caseInfo;

}

/**

* This method initializes okBtn

*

* @return javax.swing.JButton

*/

private JButton getOkBtn() {

if (okBtn == null) {

try {

okBtn = new JButton();

okBtn.setPreferredSize(new Dimension(70, 20));

okBtn.setMaximumSize(new Dimension(70, 20));

okBtn.setText("OK");

okBtn.setMinimumSize(new Dimension(70, 20));

okBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

Display.this.setVisible(false);

Display.this.dispose();

}

});

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return okBtn;

}

/**

* This method initializes cnlBtn

*

* @return javax.swing.JButton

*/

private JButton getCnlBtn() {

if (cnlBtn == null) {

try {

cnlBtn = new JButton();

cnlBtn.setMinimumSize(new Dimension(70, 20));

cnlBtn.setPreferredSize(new Dimension(70, 20));

cnlBtn.setText("Close");

cnlBtn.setMaximumSize(new Dimension(70, 20));

cnlBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

Display.this.setVisible(false);

Display.this.dispose();

}

});

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return cnlBtn;

}

public void run(){

caseInfo.append("ID: ");

caseInfo.append("\n");

caseInfo.append("Value: ");

caseInfo.append("\n");

caseInfo.append("New ID: ");

caseInfo.append("\n");

}

public void display(){

new Thread(this).start();

}

}

The append still does not seem to be working here...

thanks

cicho_41a at 2007-7-14 21:15:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
Your second example as posted doesn't do anything.I invoked the display() method at the end of the initialize() method and got 3 lines of text added as expected.
camickra at 2007-7-14 21:15:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

In my last email there was a mistake in the code..basically the run method is never called.

If I call display() from within the actionListener of the okBtn, then all is fine.

What I really want is to be able to display the text when the dialog window is triggerred.

The code in my first email had this in scope.

I hope I am clear.

thanks

cicho_41a at 2007-7-14 21:15:28 > top of Java-index,Desktop,Core GUI APIs...