Ali Payandeh
Ali Payandeh

Reputation: 63

label unclear text when change its text

unfortunately I can't handle the change of txt when the button is clicked, I try to write a txt and overtime that I click the button, this txt value should change and allotting seems right, the only problem is that the printed number is not obvious and it seems some part of previous txt remains with it.

package berGenerator;
import java.awt.EventQueue;
public class sscce {
 private JFrame frame;
 private final Action action = new SwingAction();
 private static     int i = 555;

/**
 * Launch the application.
 */
public static void main(String[] args) {    
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                sscce window = new sscce();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
/**
 * Create the application.
 */
public sscce() {
    initialize();
}
/**
 * Initialize the contents of the frame.
 */
private void initialize() {


    frame = new JFrame();
    frame.setBounds(100, 100, 550, 401);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);


    JButton Next = new JButton("Next");
    Next.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    Next.setAction(action);
    Next.setBounds(167, 290, 198, 64);
    frame.getContentPane().add(Next);



}
private class SwingAction extends AbstractAction {
    public SwingAction() {
        putValue(NAME, "Next Timeslot/scheduler");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        i = i+1;
        frame.getContentPane().validate();
        frame.getContentPane().repaint();
        String from = String.valueOf(i);
        System.out.println("sw is: "+from);
        JTextArea TextArea11 = new JTextArea("");
        TextArea11.setText(from);
        TextArea11.setForeground(Color.GREEN);
        TextArea11.setBounds(6, 66, 87, 16);
        frame.getContentPane().add(TextArea11);
    }
}
}

Upvotes: 0

Views: 50

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.

Layout managers are fundamental to the Swing API, you should make the time to learn how to use them, they will solve more problems than you think they create.

See Laying Out Components Within a Container for more details.

You're creating multiple instances of JTextArea and adding to the frame, but you're not removing any, you're running into a potential z-ordering problem at best and a major resource management issue at worst.

Instead, simply create a single instance of JTextArea, add it to the frame (just like you did your button) and simply update it when the Action is triggered, for example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import static javax.swing.Action.SHORT_DESCRIPTION;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test {

    private JFrame frame;
    private final Action action = new SwingAction();
    private static int i = 555;
    private JTextArea textArea;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test window = new Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test() {
        initialize();
    }

    private void initialize() {

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textArea = new JTextArea(10, 20);
        textArea.setText(String.valueOf(i));
        frame.add(new JScrollPane(textArea));

        JButton next = new JButton("Next");
        next.setAction(action);
        frame.getContentPane().add(next, BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null);

    }

    private class SwingAction extends AbstractAction {

        public SwingAction() {
            putValue(NAME, "Next Timeslot/scheduler");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }

        public void actionPerformed(ActionEvent e) {
            i = i + 1;
            String from = String.valueOf(i);
            System.out.println("sw is: " + from);
            textArea.setText(from);
            textArea.setForeground(Color.GREEN);
        }
    }
}

Upvotes: 2

Related Questions