adding text to a text area dynamically
what i'm trying to achieve here is to add a line of text in a text area in another class called PageList
i'm stuck, because in order for the text area to update, i have to exit the program completely and run it again
any ideas?
Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;
public class PageAdd extends JFrame
{
private JPanel panel;
private JTextField wordField;
/**
* Launch the application.
*/
public static void main(String[] args) throws IOException
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
PageAdd frame = new PageAdd();
frame.setVisible(false);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PageAdd()
{
setTitle("Add");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 408, 223);
panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(panel);
JLabel lblWord = new JLabel("Word:");
lblWord.setPreferredSize(new Dimension(330, 14));
panel.add(lblWord);
wordField = new JTextField();
wordField.setColumns(30);
panel.add(wordField);
JLabel lblDefinition = new JLabel("Definition:");
lblDefinition.setPreferredSize(new Dimension(330, 14));
panel.add(lblDefinition);
final JTextArea defArea = new JTextArea();
panel.add(defArea);
defArea.setLineWrap(true);
defArea.setRows(4);
defArea.setColumns(30);
JScrollPane scroll = new JScrollPane(defArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
if(wordField.getText().equals("") || defArea.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please enter a word and a definition.");
}
else
{
try
{
FileWriter fw = new FileWriter("G:\\FINAL PROJECT\\program\\list.txt", true); //your text file
PrintWriter output = new PrintWriter(fw);
output.println(wordField.getText() + "\t" + defArea.getText());
output.close();
fw.close();
dispose();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
});
btnAdd.setPreferredSize(new Dimension(80, 20));
panel.add(btnAdd);
}
}
Re: adding text to a text area dynamically
Can you post the specific code segment where you added the text.
Re: adding text to a text area dynamically
this segment is in another class called PageList
Code:
class CustomListRenderer implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
JTextArea renderer = new JTextArea(3,5);
renderer.append(value.toString());
renderer.setCaretPosition(renderer.getText().length()-1);
renderer.setLineWrap(true);
return renderer;
}
}
DefaultListModel listModel = new DefaultListModel();
for(int n=0; n<=pairs.size()-1; n++)
{
listModel.addElement("Word: " + pairs.get(n).word + "\n"+ "Definition: " + pairs.get(n).definition);
}
Re: adding text to a text area dynamically
Did you try to simply add element to the list model with the relevant index?