0

I've a simple GUI file which is here: I want to update the Label text each time when new file is selected But when I am selecting any file, it is overlapping on the existing Jlabel text, so, please help me how do I update my JLabel text.

Here is my code:

protected static void excelButtonAction(){
    excelReturnVal = fc.showOpenDialog(excelButton);
    if(excelReturnVal==JFileChooser.APPROVE_OPTION){                
        FileValidation.excelFileValidation(fc); 
        System.out.println(FileValidation.getName() );
        if(status==JFileChooser.CANCEL_OPTION){

        }else{
          fileName=FileValidation.getName();

          FileValidation.updatemylabel(fileName);
          excelFileName = new JLabel(fileName);
          excelFileName.setText(fileName);
          excelFileName.setBounds(140, 67, 350, 30);
          excelFileName.setFont(new Font("Myriad Pro",Font.PLAIN,10));
          panel.add(excelFileName);
          panel.revalidate();
          panel.repaint();
        }
    } else{
        System.out.println("Open command cancelled by user." + newline);
    }
}    

public static void updatemylabel(String exfileName){
    excelFileName = new JLabel(fileName);
    excelFileName.setText(fileName);
    JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {


        public void propertyChange(PropertyChangeEvent evt) {
            if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())){
                JFileChooser chooser = (JFileChooser) evt.getSource();
                File oldFile = (File) evt.getOldValue();
                File newFile = (File) evt.getNewValue();
                File curFile = chooser.getSelectedFile();
            }else if(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())){
                 JFileChooser chooser = (JFileChooser)evt.getSource();
                    File[] oldFiles = (File[])evt.getOldValue();
                    File[] newFiles = (File[])evt.getNewValue();
                    File[] files = chooser.getSelectedFiles();
            }

        }
    });
    excelFileName = new JLabel(fileName);
    excelFileName.setText(fileName);
    excelFileName.setBounds(140, 67, 350, 30);
    excelFileName.setFont(new Font("Myriad Pro",Font.PLAIN,10));
    panel.add(excelFileName);
    panel.revalidate();
    panel.repaint();

    existingText=exfileName;


    }

Let me know if any further information is required to resolve my issue. Thanks in advance for your co-operateion.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ravi
  • 65
  • 2
  • 12
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) `excelFileName.setBounds(140, 67, 350, 30);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 20 '18 at 02:07

2 Answers2

4

Your code creates a new JLabel instance every time. You need to create an instance once, store it in a field of your class, and call setText() whenever you need to update it.

yole
  • 92,896
  • 20
  • 260
  • 197
  • if you explain in details, it would be appreciated, although, I had tried with that way as well but no luck – Ravi Feb 19 '18 at 15:13
  • 1
    What exactly did you try, and what exactly does "no luck" mean? – yole Feb 19 '18 at 15:14
  • 1
    @Ravi, The usage of static methods tells me you need to redesign your code. You should not be using static methods. I suggest you start by reading the section from the Swing tutorial on [How to Use Text Areas](https://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html). It shows you how to create a single instance of a JTextArea and then update the text as required in a different method. So download the `TextDemo` code from the tutorial and understand how it works and then fix your code. – camickr Feb 19 '18 at 15:43
0

You can have a look at the following for better understanding of labels in java :

setText

public void setText(String text)

Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed. The default value of this property is null.

This is a JavaBeans bound property.

Abhi
  • 995
  • 1
  • 8
  • 12