inserting tables to html document java swing - java

I have a simple swing application with two JTextPanes. In the first I write a html code and in the second I get the appearance defined by this html. I want to have button which allows me to insert a table into the document.
Here is my code:
public class TestEditor extends JFrame {
public TestEditor(){
createConnection();
createGUI();
}
private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
JScrollPane scroll1=new JScrollPane(text);
JScrollPane scroll2=new JScrollPane(html);
JSplitPane split=new JSplitPane();
split.setLeftComponent(scroll1);
split.setRightComponent(scroll2);
split.setDividerLocation(0.5);
split.setResizeWeight(0.5);
getContentPane().add(split);
table=new JButton("Insert table");
table.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
StringBuilder builder=new StringBuilder("<table border=1>\n");
for(int i=0;i<4;i++){
builder.append("<tr>");
for(int j=0;j<4;j++){
builder.append("<td></td>");
}
builder.append("</tr>\n");
}
builder.append("</table>\n");
try{
text.getStyledDocument().insertString(text.getCaretPosition(), builder.toString(), null);
}catch(BadLocationException ex){
ex.printStackTrace();
}
}
});
getContentPane().add(table, BorderLayout.SOUTH);
setTitle("Test");
setPreferredSize(new Dimension(600,300));
pack();
}
private void createConnection(){
text=new JTextPane();
html=new JTextPane();
html.setContentType("text/html");
html.getStyledDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
update();
}
#Override
public void removeUpdate(DocumentEvent e) {
update();
}
#Override
public void changedUpdate(DocumentEvent e) {
update();
}
private void update(){
if(fromText) return;
fromHtml=true;
text.setText(html.getText());
fromHtml=false;
}
});
text.getStyledDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
update();
}
#Override
public void removeUpdate(DocumentEvent e) {
update();
}
#Override
public void changedUpdate(DocumentEvent e) {
update();
}
private void update(){
if(fromHtml) return;
fromText=true;
html.setText(text.getText());
fromText=false;
}
});
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new TestEditor().setVisible(true);
}
});
}
private JTextPane text;
private JTextPane html;
private boolean fromHtml, fromText;
private JButton table;
}
However, this inserts a table in the wrong place, most often after the ending tag </html>. My goal is to insert the table in the place in which caret is on the right editor.
What should I do? How to detect the proper position?

I tested your code, and it fails when you are editing the styled document, but it works when you edit the text document. The reason is simple: when a call to update() occurs, the use of setText() puts the caret at the end of the document. The html code is then naturally inserted at the end.
So ideally you would want to keep track of the caret position and cleverly update it after setText(). But this is not easy: How do you manage inserted or removed text?
So here is a proposal: When the "add table" button is clicked, detect which editor has focus (= where the caret position is relevant), and insert the html code in the corresponding one.
However, inserting html code in the styled document will be done slightly differently (to avoid being escaped):
HTMLEditorKit kit = (HTMLEditorKit) html.getEditorKit();
HTMLDocument doc = (HTMLDocument) html.getStyledDocument();
kit.insertHTML(doc, html.getCaretPosition(), builder.toString(), 0, 0, null);
So you should get something like that:
Component c = getFocusOwner();
if(c==html){
HTMLEditorKit kit = (HTMLEditorKit) html.getEditorKit();
HTMLDocument doc = (HTMLDocument) html.getStyledDocument();
kit.insertHTML(doc, html.getCaretPosition(), builder.toString(), 0, 0, null);
}else if(c==text){
text.getStyledDocument().insertString(text.getCaretPosition(), builder.toString(), null);
}
Also, make the button unfocusable to avoid getFocusOwner() to detect the button click:
table.setFocusable(false);

Related

AddDocumentListener's variable scope when creating two titles (using TitledBorder) in Java Swing

Hello I am trying to display one String ("Character Count: ") and one dynamic character count on the bottom of JTextArea.
When I run this code below, there is a panel that opens up without characterCountTitle. Only when I start typing, characterCountTitle displays and the number is correctly dynamic.
My goal is to show characterCountTitle (string + character count) as soon as the panel is open to users.
private void initComponents() {
this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener() {
TitledBorder characterCountTitle;
Border emptyBorder;
public void insertUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void removeUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void changedUpdate(DocumentEvent e) {
displayEditInfo(e);
}
private void displayEditInfo(DocumentEvent e) {
Document document = e.getDocument();
emptyBorder = BorderFactory.createEmptyBorder();
//displays a string of "Character Count: " and another string of dynamic character count
characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count: " + document.getLength());
characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
panel.setBorder(characterCountTitle);
}
});
this.panel.add(notePanel, BorderLayout.CENTER);
this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}
Due to this issue, I was trying to create two titles; one for string(outside of addDocumentListener) and one for character count (inside displayEditInfo method), but it messes up the variable scope.
I'd greatly appreciate your input!
You may simply create and add your border outside of the DocumentListener, and just change the title text on document events :
private void initComponents() {
Border emptyBorder = BorderFactory.createEmptyBorder();
final TitledBorder characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count:");
characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
panel.setBorder(characterCountTitle);
this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void removeUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void changedUpdate(DocumentEvent e) {
displayEditInfo(e);
}
private void displayEditInfo(DocumentEvent e) {
Document document = e.getDocument();
//displays a string and dynamic character count
characterCountTitle.setTitle("Character Count: " + document.getLength());
panel.repaint();
}
});
this.panel.add(notePanel, BorderLayout.CENTER);
this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}

changing the value of a text field as another is changed and the same but reversed causing an error

I am making a simple Miles-Kilometers converter that updates automatically as you type. The problem is that is that an error is thrown somewhere. I believe that this is because as i change one of the fields it handles the event and changes the other field but since that also has an event handler for when it is changed it tries to change the other field itself and they keep firing events back and forth until something somewhere explodes. Any idea how I can fix this or is there a different problem completely ?
Here's my code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class Book extends JFrame{
private JTextField jtfKilometers = new JTextField(10);
private JTextField jtfMiles = new JTextField(10);
public Book(){
setLayout(new BorderLayout(10, 0));
JPanel jlblPanel = new JPanel(new GridLayout(2, 0, 50, 5));
jlblPanel.add(new JLabel("Kilometers"));
jlblPanel.add(new JLabel("Miles"));
add(jlblPanel, "West");
JPanel jtfPanel = new JPanel(new GridLayout(2, 0, 5, 5));
jtfPanel.add(jtfKilometers);
jtfPanel.add(jtfMiles);
add(jtfPanel, "Center");
jtfKilometers.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
if(jtfKilometers.getText().equals("")){
jtfMiles.setText("");
}else{
jtfMiles.setText(Double.parseDouble(jtfKilometers.getText()) * 0.621371 + "");
}
}
#Override
public void removeUpdate(DocumentEvent e) {
insertUpdate(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
insertUpdate(e);
}
});
jtfMiles.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
if(jtfMiles.getText().equals("")){
jtfKilometers.setText("");
}else{
jtfKilometers.setText(Double.parseDouble(jtfMiles.getText()) * 1.60934 + "");
}
}
#Override
public void removeUpdate(DocumentEvent e) {
insertUpdate(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
insertUpdate(e);
}
});
}
public static void main(String[] args){
Book f = new Book();
f.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
You need to add additional guard on the focus of the text fields, so that you will be modifying only the other text field, not recursively both of them.
jtfKilometers.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
if (jtfKilometers.hasFocus()) { // ADD THIS LINE
if (jtfKilometers.getText().equals("")) {
jtfMiles.setText("");
} else {
jtfMiles.setText(Double.parseDouble(jtfKilometers.getText()) * 0.621371 + "");
}
}
}
and similarly
jtfMiles.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
if (jtfMiles.hasFocus()) { // ADD THIS LINE
if (jtfMiles.getText().equals("")) {
jtfKilometers.setText("");
} else {
jtfKilometers.setText(Double.parseDouble(jtfMiles.getText()) * 1.60934 + "");
}
}
}
An easy fix for this is checking if the frame has focus when the event is triggered. This will prevent the event from triggering back and forth like is happening to you now.
See the adjusted code snippet from your sample below...
public void insertUpdate(DocumentEvent e) {
if(jtfMiles.hasFocus()){//Check for focus here....repeat same check on your other "insertUpdate" method for your other frame.
if(jtfMiles.getText().equals("")){
jtfKilometers.setText("");
}else{
jtfKilometers.setText(Double.parseDouble(jtfMiles.getText()) * 1.60934 + "");
}
}
}
Hope this helps!

how to prevent jscrollpane from scrolling to the bottom?

I have JTextPane (log) inside JScrollPane (logScrollPane) element. Log's content is set to "text/html".
I created a method that appends this log which looks like this:
public void appendLog(String someHTMLText)
{
HTMLDocument doc = (HTMLDocument) log.getDocument();
HTMLEditorKit editorKit = (HTMLEditorKit) log.getEditorKit();
try
{
editorKit.insertHTML(doc, doc.getLength(), someHTMLText, 0, 0, null);
}
catch (BadLocationException | IOException ex)
{
// handle exceptions
}
}
I want to improve this method and force logScrollPane's VerticalScrollBar to move_to_the_bottom/stay_at_it's_position depending on additional boolean argument.
Final method should look like this:
public void appendLog(String someHTMLText, boolean scroll)
{
if(scroll)
{
/*
* append log and set VerticalScrollBar to the bottom by
* log.setCaretPosition(log.getDocument().getLength());
*/
}
else
{
// append log BUT make VerticalScrollBar stay at it's previous position
}
}
Any suggestions? :)
Here is something similar to what I did when I wanted to achieve this. The key is to alter the behavior of scrollRectToVisible.
public class Docker extends JFrame {
boolean dockScrollbar = true;
MYTextPane textPane = new MYTextPane();
JScrollPane sp = new JScrollPane(textPane);
Docker() {
JCheckBox scrollbarDockCB = new JCheckBox("Dock scrollbar");
scrollbarDockCB.addItemListener(new DockScrollbarListener());
scrollbarDockCB.setSelected(true);
JButton insertText = new JButton("Insert text");
insertText.addActionListener(new TextInserter());
getContentPane().add(insertText, BorderLayout.PAGE_START);
getContentPane().add(sp);
getContentPane().add(scrollbarDockCB, BorderLayout.PAGE_END);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
class MYTextPane extends JTextPane {
MYTextPane() {
setEditorKit(new HTMLEditorKit());
}
#Override
public void scrollRectToVisible(Rectangle aRect) {
if (dockScrollbar)
super.scrollRectToVisible(aRect);
}
void insertText(String msg) {
HTMLEditorKit kit = (HTMLEditorKit) getEditorKit();
HTMLDocument doc = (HTMLDocument) getDocument();
try {
kit.insertHTML(doc, doc.getLength(), msg, 0, 0, null);
} catch (BadLocationException | IOException e1) {
e1.printStackTrace();
}
setCaretPosition(doc.getLength());
}
}
class TextInserter implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
textPane.insertText("AAA\n");
}
}
class DockScrollbarListener implements ItemListener {
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
dockScrollbar = true;
JScrollBar sb = sp.getVerticalScrollBar();
sb.setValue(sb.getMaximum());
}
else if (e.getStateChange() == ItemEvent.DESELECTED)
dockScrollbar = false;
}
}
public static void main(String[] args) {
new Docker();
}
}
Notes:
I made the docking set to false when you manually scroll in my code, you can add it here too. by adding a mouse listener to the vertical scrollbar.
I have boolean dockScrollbar as a field of the text pane because I have more than one.
I don't have a field for the JScrollPane, I get it through the text pane.
I've never tried it on a JEditorPane but you should be able to use the caret update policy to control this.
Check out Text Area Scrolling for more information.

searching a Jtable for a value in a textfield [duplicate]

I have a JTable (DefaultTableModel) and a JTextField. I'd like to filter the JTable with the regex I put into the text field. When I start the program, all entries are shown, but when I enter text into the text field, no rows are displayed even though it should find the text within a row.
private void createFilter() {
_sorter = new TableRowSorter<DefaultTableModel>(new DefaultTableModel());
JPanel filterPanel = new JPanel();
filterPanel.setLayout(new BorderLayout());
JLabel filterLabel = new JLabel("Filter:");
filterPanel.add(filterLabel, BorderLayout.WEST);
_inputField = new JTextField();
_inputField.setColumns(40);
filterPanel.add(_inputField, BorderLayout.CENTER);
_inputField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent de) {
newFilter();
}
public void removeUpdate(DocumentEvent de) {
newFilter();
}
public void changedUpdate(DocumentEvent de) {
newFilter();
}
});
JButton clearButton = new JButton("X");
clearButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
_inputField.setText("");
}
});
filterPanel.add(clearButton, BorderLayout.EAST);
_mainFrame.add(filterPanel, BorderLayout.SOUTH);
}
private void newFilter() {
RowFilter<DefaultTableModel, Object> rowFilter = null;
try {
rowFilter = RowFilter.regexFilter(_inputField.getText());
}
catch(java.util.regex.PatternSyntaxException ex) {
return;
}
_sorter.setRowFilter(rowFilter);
_table.setRowSorter(_sorter);
}
My debugger shows me, that rowFilter is initialized, so it can't be because of a wrong RegEx. Also newFilter() is called at every keystroke.
Thanks in advance. I'd be happy to provide more information if needed.
Sincerely,
Michael
It seems that the JTable and the TableRowSorter each have a different table model. The TableRowSorter should be constructed with the table model of the JTable.
At first it's easier to print stack trace on this block instead of debugging to be able to know if there was an error during initializing RowFilter.
catch(java.util.regex.PatternSyntaxException ex) {
ex.printStackTrace();
return;
}
I cannot find anything wrong with the code, it seems like problem is on the regular expression. So if you could test the regex typed into the JTextField against one of the row you expect to show to see if it matches or not:
Javascript Regular Expression Validator
JTable tutorial contains example for JTable Filtering and Sorting, another examples here,
for Case Insensitive you have to set
TableRowSorter<TableModel>#setRowFilter(
RowFilter.regexFilter("(?i)" + myTextField.getText()));
EDIT:
basic workaround:
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableFxModel);
myTable.setRowSorter(sorter);
filterFxText.getDocument().addDocumentListener(new DocumentListener() {
private void searchFieldChangedUpdate(DocumentEvent evt) {
String text = filterFxText.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
try {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
} catch (PatternSyntaxException pse) {
JOptionPane.showMessageDialog(null, "Bad regex pattern",
"Bad regex pattern", JOptionPane.ERROR_MESSAGE);
}
}
}
#Override
public void insertUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
#Override
public void removeUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
#Override
public void changedUpdate(DocumentEvent evt) {
searchFieldChangedUpdate(evt);
}
});

Simultaneously update text area while typing in text field

I want to update my text area along with typing in the text field but i get a delay of 1 keystroke while typing i.e when i press a key the previous key is displayed.Here is my snippet
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{
String a = jTextField1.getText();
jTextArea1.setText(a);
}
I would not recommend using KeyListeners
Simply add a DocumentListener to your JTextField via:
textField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent de) {
}
#Override
public void removeUpdate(DocumentEvent de) {
}
#Override
public void changedUpdate(DocumentEvent de) {
}
});
Inside each of the methods ( insertUpdate,removeUpdate and changedUpdate) simply put in a call to set the text of your JTextArea via setText():
textArea.setText(textField.getText());
Here is an example I made:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
final JTextField jtf = new JTextField(20);
final JTextArea ta = new JTextArea(20,20);
ta.setEditable(false);
jtf.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent de) {
ta.setText(jtf.getText());
}
#Override
public void removeUpdate(DocumentEvent de) {
ta.setText(jtf.getText());
}
#Override
public void changedUpdate(DocumentEvent de) {
//Plain text components don't fire these events.
}
});
frame.getContentPane().add(jtf, BorderLayout.WEST);
frame.getContentPane().add(ta, BorderLayout.EAST);
}
}
You should do that under the keyReleased event instead of the keyTyped and it will work as you need.
You need to wait till the event on your TextField is processed before updating the TextArea. Your code update the TextArea before the TextField is done processing the new typed character. Hence the text set in the TextArea is one keystroke behind.
You could try using recursion by referencing the method inside the method (avoid loops though).

Categories