Results 1 to 9 of 9
  1. #1
    nickbeacroft is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default Can't refresh a JPanel/text

    Hi everyone,

    I'm new to java GUI stuff, and am trying to add a component that can be blank until I click a button, at which point I want it to display some text. The problem is that I have made the jPanel, with no text, but when I try to add the text with panel.setText("text"), it doesn't change on the display. I have tried to repaint etc, but nothing works. I am using a group layout, and as such, the JPanel is created when the layout is setup.

    Thanks for any help,
    Nick Beacroft

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default try text area

    This seems to be more common than we would hope.

    Try something along the lines of:

    TextArea ta = new TextArea();//
    JCanvas jca = new JCanvas();
    JFrame.getContentPane().add(ta);
    //......
    //......
    ta.setText(String newText);
    //......
    //......
    JFrame.refresh / .repaint / invalidate() / SwingInvokeLater .........

    The list seems to double in size according to Moore's law.

    Which JDK version are you using?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) JPanel doesn't have a setText(...) method (not that I know of anyway). What are you calling this method on? A JLabel?
    2) If you are just learning Swing, I strongly advise you not to use NetBeans-generated code. This won't help you learn Swing but actually will set back your learning by about 3-4 months. Better to learn to do Swing by hand, and then when you are familiar with it, go ahead and use NetBeans to generate code, but not until then.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    /* Calls updateUI on all sub-components of the JFrame */
    	private void updateUI() {
    		SwingUtilities.updateComponentTreeUI(this);
    	}
    Then pass this method to a JFrame on which all components are,so the u can update them every time when you want to.

  5. #5
    nickbeacroft is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    Sorry my mistake, it is a JLabel I am trying to alter and update the text on, not the JPanel as I said.

    Thanks for the suggestions. I have tried the updateUI() method suggested but this does not seem to work either.

    To clarify, I have created the JFrame, added the JLabel which at the start has the text "Result". I then wish this text to change when I click on a JButton, but cannot get the text to update. When debugging, the text on the JLabel is changing to the new string in its properties, but it is not showing it on the graphical display.

    Any further help would be really appreciated, this tiny little problem has taken up over 6 hours of my time so far, and for such a small thing, it is driving me crazy!!!

    Thanks

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Oh,can you post the part of your code here,so that we can debug it.
    Thanks in advance

  7. #7
    nickbeacroft is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks, I've tried to include all the relevant code, as the actual class is huge. Thanks for your help

    Java Code:
    public class AnalysisWindow extends javax.swing.JFrame {
    
    	private JLabel resultLabel1;
    	private static AnalysisWindow analysisWindow;
    	private AbstractAction predictLine1Action;
    	private JButton predictButton1;
    
    
    	/**
    	* Loads the welcome screen
    	*/
    	public void loadAnalysisWindow(String[] args, View viewIn) {
    		
    		view = viewIn;
    		
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				AnalysisWindow inst = new AnalysisWindow();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    				analysisWindow = inst;
    			}
    		});
    	}
    	
    	public AnalysisWindow() {
    		super();
    		initGUI();
    	}
    	
    	private void initGUI() {
    
    		//sets all the layout and adds the various components, including the JLabel.
    		
    	}
    
    	//This creates the JLabel, with text starting as Result
    	private JLabel getResultLabel1() {
    		if(resultLabel1 == null) {
    			resultLabel1 = new JLabel();
    			GroupLayout resultLabel1Layout = new GroupLayout((JComponent)resultLabel1);
    			resultLabel1.setLayout(null);
    			resultLabel1.setText("Result");
    			resultLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    			resultLabel1.setFont(new java.awt.Font("Tahoma",0,12));
    			resultLabel1Layout.setVerticalGroup(resultLabel1Layout.createParallelGroup());
    			resultLabel1Layout.setHorizontalGroup(resultLabel1Layout.createParallelGroup());
    		}
    		return resultLabel1;
    	}
    
    
    	//this method is comes from the model when the predict button is called, and this is when
    	//I want to display the result in the JLabel
    
    	public void displayPrediction(ResultPrediction prediction) {
    		
    		resultLabel1.setText(getResult(prediction));
    		//I have tried at this point - resultLabel1. repaint() revalidate() validate(), as well as many others!
    	}

  8. #8
    nickbeacroft is offline Member
    Join Date
    Jun 2008
    Posts
    6
    Rep Power
    0

    Default

    Ok it' official, I am an idiot. I realised the problem through fixing another issue. Basically I was calling the alter text method in an old version of the JPanel, ie my view had stored the original version of the JPanel, not the latest. So I was calling the right method, it was changing the JLabel but in the old JPanel, which was why it wasn't changing what I saw.

    Now, all I simply have to do is call jLabel.setText("new text"); which automatically redraws/repaints the JLabel to include the new text. Hope my woes help others in similar situations.

    Thanks again for your help guys, really appreciate it.

  9. #9
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Maybe you have problem with ActionListener which is passed to the JButton,becuase look at my code and it works excellent:
    Java Code:
    import  javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Test extends JFrame {
    
    	
    	private JLabel label=new JLabel();
    	private JButton b=new JButton("Add text");
    	public Test(){
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.pack();
    		this.setLayout(new FlowLayout());
    		this.add(label);
    	
    		this.add(b);
    		b.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				label.setText("It Works");
    				label.updateUI();
    			}
    		});
    		setSize(200,200);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		new Test();
    	}
    }

Similar Threads

  1. Replies: 0
    Last Post: 05-26-2008, 04:26 PM
  2. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  3. refresh a different window
    By marceldupont in forum AWT / Swing
    Replies: 3
    Last Post: 03-22-2008, 02:44 AM
  4. Replies: 5
    Last Post: 08-11-2007, 03:14 AM
  5. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 07:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •