Samsara82
asked on
JDialogs content doesn't update, using StringBuilder
Hi,
Im new with Java and I have a problem with using StringBuilder. I have Java GUI (Recipebook) where user can add, remove and list recipes. All works fine except the listing (JTextArea with added text from StringBuilder). It works the first time but when I e.g add new recipe and want to list all recipes (including the new one) my listing still shows the old list without last added recipe. I need to close my application before the changes shows in list.
My question is: How I can "update" my list so it will show the changes made instantly?
Here's the actionListener:
class RecipeList implements ActionListener {
public void actionPerformed(java.awt.e vent.Actio nEvent e) {
listDialog.setVisible(true );
listDialog.validate();
}
}
Here's ListDialog Class:
public class ListDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JScrollPane scrollPane1;
private JTextArea area;
public ListDialog(Frame parentFrame) {
super(parentFrame);
setTitle("Test Dialog");
setSize(350, 400);
setDefaultCloseOperation(D ISPOSE_ON_ CLOSE);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPa nel);
CreateTopPane(topPanel);
}
public void CreateTopPane( JPanel topPanel ) {
area = new JTextArea();
area.setEditable(false);
BuildList();
// Create the scrolling pane for the text area
scrollPane1 = new JScrollPane();
scrollPane1.getViewport(). add( area );
topPanel.add( scrollPane1, BorderLayout.CENTER );
}
//Parse Xml-file (DOM) and save in StringBuilder
public void BuildList() {
try {
BufferedReader br = new BufferedReader(new FileReader("TestDOMXML4.xm l"));
StringBuilder sb = new StringBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.new Instance() ;
DocumentBuilder loader = factory.newDocumentBuilder ();
Document doc = loader.parse("TestDOMXML4. xml");
doc.getDocumentElement().n ormalize() ;
NodeList listOfRecipes = doc.getElementsByTagName(" recipe");
for(int i=0; i < listOfRecipes.getLength(); i++) {
Node firstRecipeNode = listOfRecipes.item(i);
if (firstRecipeNode.getNodeTy pe() == Node.ELEMENT_NODE) {
Element firstRecipeElement = (Element)firstRecipeNode;
NodeList nameList = firstRecipeElement.getElem entsByTagN ame("name" );
Element nameElement = (Element)nameList.item(0);
NodeList textNameList = nameElement.getChildNodes( );
String name = textNameList.item(0).getNo deValue(). trim();
sb.append("Nimi: "+name+"\n");
NodeList categoryList = firstRecipeElement.getElem entsByTagN ame("categ ory");
Element categoryElement = (Element)categoryList.item (0);
NodeList textCategoryList = categoryElement.getChildNo des();
String category = textCategoryList.item(0).g etNodeValu e().trim() ;
sb.append("Kategoria: "+category+"\n");
NodeList preparationList =
firstRecipeElement.getElem entsByTagN ame("prepa ration");
Element preparationElement = (Element)preparationList.i tem(0);
NodeList textPreparationList = preparationElement.getChil dNodes();
String prep = textPreparationList.item(0 ).getNodeV alue().tri m();
sb.append("Ohje: "+prep+"\n\n");
}
}
br.close();
area.append(sb.toString()) ;
}
catch (Exception e) {
System.out.println(e.getMe ssage());
}
}
}
Im new with Java and I have a problem with using StringBuilder. I have Java GUI (Recipebook) where user can add, remove and list recipes. All works fine except the listing (JTextArea with added text from StringBuilder). It works the first time but when I e.g add new recipe and want to list all recipes (including the new one) my listing still shows the old list without last added recipe. I need to close my application before the changes shows in list.
My question is: How I can "update" my list so it will show the changes made instantly?
Here's the actionListener:
class RecipeList implements ActionListener {
public void actionPerformed(java.awt.e
listDialog.setVisible(true
listDialog.validate();
}
}
Here's ListDialog Class:
public class ListDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JScrollPane scrollPane1;
private JTextArea area;
public ListDialog(Frame parentFrame) {
super(parentFrame);
setTitle("Test Dialog");
setSize(350, 400);
setDefaultCloseOperation(D
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPa
CreateTopPane(topPanel);
}
public void CreateTopPane( JPanel topPanel ) {
area = new JTextArea();
area.setEditable(false);
BuildList();
// Create the scrolling pane for the text area
scrollPane1 = new JScrollPane();
scrollPane1.getViewport().
topPanel.add( scrollPane1, BorderLayout.CENTER );
}
//Parse Xml-file (DOM) and save in StringBuilder
public void BuildList() {
try {
BufferedReader br = new BufferedReader(new FileReader("TestDOMXML4.xm
StringBuilder sb = new StringBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.new
DocumentBuilder loader = factory.newDocumentBuilder
Document doc = loader.parse("TestDOMXML4.
doc.getDocumentElement().n
NodeList listOfRecipes = doc.getElementsByTagName("
for(int i=0; i < listOfRecipes.getLength();
Node firstRecipeNode = listOfRecipes.item(i);
if (firstRecipeNode.getNodeTy
Element firstRecipeElement = (Element)firstRecipeNode;
NodeList nameList = firstRecipeElement.getElem
Element nameElement = (Element)nameList.item(0);
NodeList textNameList = nameElement.getChildNodes(
String name = textNameList.item(0).getNo
sb.append("Nimi: "+name+"\n");
NodeList categoryList = firstRecipeElement.getElem
Element categoryElement = (Element)categoryList.item
NodeList textCategoryList = categoryElement.getChildNo
String category = textCategoryList.item(0).g
sb.append("Kategoria: "+category+"\n");
NodeList preparationList =
firstRecipeElement.getElem
Element preparationElement = (Element)preparationList.i
NodeList textPreparationList = preparationElement.getChil
String prep = textPreparationList.item(0
sb.append("Ohje: "+prep+"\n\n");
}
}
br.close();
area.append(sb.toString())
}
catch (Exception e) {
System.out.println(e.getMe
}
}
}
>>> area.append(sb.toString()) ;
I never did it that way.
I always practise:
String newText = ......................;
String s = area.getText();
area.setText(s + newText);
But .......................... .......... .......... .......
If you want the additions listed top-down, it's better to set the first text:
area.setText("<HTML><CENTE R>" + firstText;
and subsequently:
area.setText(area.getText( ) + "<BR>" + newText);
;JOOP!
I never did it that way.
I always practise:
String newText = ......................;
String s = area.getText();
area.setText(s + newText);
But ..........................
If you want the additions listed top-down, it's better to set the first text:
area.setText("<HTML><CENTE
and subsequently:
area.setText(area.getText(
;JOOP!
ASKER
Thanks for the answer sciuriware,
I changed
area.append(sb.toString()) ;
to your first solution
String newText = ......................;
String s = area.getText();
area.setText(s + newText);
It's working as the same as area.append(sb.toString()) ;
Im not quite sure what is this firstText in area.setText("<HTML><CENTE R>" + firstText); What String should it contain?
But still, this doesn't solve my original problem. If I list all recipes, close my listing dialog, add new recipe and open up my listing dialog again, it still shows the old list.
I changed
area.append(sb.toString())
to your first solution
String newText = ......................;
String s = area.getText();
area.setText(s + newText);
It's working as the same as area.append(sb.toString())
Im not quite sure what is this firstText in area.setText("<HTML><CENTE
But still, this doesn't solve my original problem. If I list all recipes, close my listing dialog, add new recipe and open up my listing dialog again, it still shows the old list.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again, I changed my code to this:
//Main GUI
class RecipeList implements ActionListener {
public void actionPerformed(java.awt.e vent.Actio nEvent e) {
listDialog.setVisible(true );
listDialog.validate();
}
}
//ListDialog class
public class ListDialog extends JDialog implements Runnable {
private static final long serialVersionUID = 1L;
private JScrollPane scroll;
private JTextArea area;
public ListDialog(Frame parentFrame) {
super(parentFrame);
setTitle("List Recipes");
setSize(350, 400);
setDefaultCloseOperation(D ISPOSE_ON_ CLOSE);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPa nel);
area = new JTextArea();
area.setEditable(false);
scroll = new JScrollPane();
//scroll.add(area);
scroll.getViewport().add( area );
topPanel.add(scroll);
new Thread(this).start();
}
public void run() {
try {
BufferedReader br = new BufferedReader(new FileReader("TestDOMXML4.xm l"));
StringBuilder sb = new StringBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.new Instance() ; DocumentBuilder loader = factory.newDocumentBuilder ();
Document doc = loader.parse("TestDOMXML4. xml");
doc.getDocumentElement().n ormalize() ;
NodeList listOfRecipes = doc.getElementsByTagName(" recipe");
for(int i=0; i < listOfRecipes.getLength(); i++) {
Node firstRecipeNode = listOfRecipes.item(i);
if (firstRecipeNode.getNodeTy pe() == Node.ELEMENT_NODE) {
Element firstRecipeElement = (Element)firstRecipeNode;
NodeList nameList = firstRecipeElement.getElem entsByTagN ame("name" );
Element nameElement = (Element)nameList.item(0);
NodeList textNameList = nameElement.getChildNodes( );
String name = textNameList.item(0).getNo deValue(). trim();
sb.append("Nimi: "+name+"\n");
NodeList categoryList = firstRecipeElement.getElem entsByTagN ame("categ ory");
Element categoryElement = (Element)categoryList.item (0);
NodeList textCategoryList = categoryElement.getChildNo des();
String category = textCategoryList.item(0).g etNodeValu e().trim() ;
sb.append("Kategoria: "+category+"\n");
NodeList preparationList = firstRecipeElement.getElem entsByTagN ame("prepa ration");
Element preparationElement = (Element)preparationList.i tem(0);
NodeList textPreparationList = preparationElement.getChil dNodes();
String prep = textPreparationList.item(0 ).getNodeV alue().tri m();
sb.append("Ohje: "+prep+"\n\n");
}
}
br.close();
area.append(sb.toString()) ;
}
catch (Exception e) {
System.out.println(e.getMe ssage());
}
}
}
But still..doesn't work :(
Can you suggest something that could be wrong? I have tested add/remove methods, these methods change my xml-file instantly, I just can't show these changes in my listing.
//Main GUI
class RecipeList implements ActionListener {
public void actionPerformed(java.awt.e
listDialog.setVisible(true
listDialog.validate();
}
}
//ListDialog class
public class ListDialog extends JDialog implements Runnable {
private static final long serialVersionUID = 1L;
private JScrollPane scroll;
private JTextArea area;
public ListDialog(Frame parentFrame) {
super(parentFrame);
setTitle("List Recipes");
setSize(350, 400);
setDefaultCloseOperation(D
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPa
area = new JTextArea();
area.setEditable(false);
scroll = new JScrollPane();
//scroll.add(area);
scroll.getViewport().add( area );
topPanel.add(scroll);
new Thread(this).start();
}
public void run() {
try {
BufferedReader br = new BufferedReader(new FileReader("TestDOMXML4.xm
StringBuilder sb = new StringBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.new
Document doc = loader.parse("TestDOMXML4.
doc.getDocumentElement().n
NodeList listOfRecipes = doc.getElementsByTagName("
for(int i=0; i < listOfRecipes.getLength();
Node firstRecipeNode = listOfRecipes.item(i);
if (firstRecipeNode.getNodeTy
Element firstRecipeElement = (Element)firstRecipeNode;
NodeList nameList = firstRecipeElement.getElem
Element nameElement = (Element)nameList.item(0);
NodeList textNameList = nameElement.getChildNodes(
String name = textNameList.item(0).getNo
sb.append("Nimi: "+name+"\n");
NodeList categoryList = firstRecipeElement.getElem
Element categoryElement = (Element)categoryList.item
NodeList textCategoryList = categoryElement.getChildNo
String category = textCategoryList.item(0).g
sb.append("Kategoria: "+category+"\n");
NodeList preparationList = firstRecipeElement.getElem
Element preparationElement = (Element)preparationList.i
NodeList textPreparationList = preparationElement.getChil
String prep = textPreparationList.item(0
sb.append("Ohje: "+prep+"\n\n");
}
}
br.close();
area.append(sb.toString())
}
catch (Exception e) {
System.out.println(e.getMe
}
}
}
But still..doesn't work :(
Can you suggest something that could be wrong? I have tested add/remove methods, these methods change my xml-file instantly, I just can't show these changes in my listing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER