JTextPane not updating on JButton press

90 views Asked by At

I am currently having trouble with a JTextPane not updating when a JButton ActionListener is telling it to update. In the code, I have a button that assigns a selected value from a JList to a global variable.

This process works because when I print out the variable when I press the button it prints the selected value in a list. However, when I push the button I also want that variable to populate the text pane so I know what value I am working with when using the UI. Does anyone know what might help with this?

I will try to get the relevant code from my large JavaSwing script as I'm using Eclipse WindowBuilder and it throws everything in the source in whatever order I created it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MainMappingGui extends JFrame {
    private JPanel contentPane; 
    String attSelected = "Hello";
    String attButSelect = "";
    JTextPane textPaneEdits;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainMappingGui frame = new MainMappingGui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public MainMappingGui() {
        setTitle("Data Mapping");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1105, 892);
        contentPane = new JPanel();
        contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnConfigMap = new JButton("Configure Mapping");
        btnConfigMap.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                attButSelect = attSelected;
                System.out.println(attButSelect);
                textPaneEdits.repaint();
                textPaneEdits.revalidate();
            }
        });
        btnConfigMap.setFont(new Font("Tahoma", Font.PLAIN, 9));
        btnConfigMap.setBounds(206, 406, 122, 23);
        contentPane.add(btnConfigMap);  

        textPaneEdits = new JTextPane();
        textPaneEdits.setBounds(634, 38, 314, 357);
        textPaneEdits.setEditable(false);
        textPaneEdits.setText("Current Output Column: " + attButSelect);
        contentPane.add(textPaneEdits);      
  }
}

For this example, please assume that attSelected is a value that changes dynamically with a ListSelectionListener and works properly for selected values in a list. I have set it to "Hello" for simplicity sake.

1

There are 1 answers

0
David Almeida On

As the one of the comments says, you have to manually change the text in the text pane.

btnConfigMap.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
      attButSelect = attSelected;
      textPaneEdits.setText(attButSelect);
   }
});

Repaint and revalidate are not necessary.