Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-22-2008, 11:39 PM
Member
 
Join Date: Jun 2008
Posts: 6
nickbeacroft is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-23-2008, 12:02 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-23-2008, 01:55 AM
Senior Member
 
Join Date: Jun 2008
Posts: 180
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-23-2008, 03:41 AM
serjant's Avatar
Member
 
Join Date: Jun 2008
Location: Israel,Tel-Aviv
Posts: 41
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-23-2008, 03:39 PM
Member
 
Join Date: Jun 2008
Posts: 6
nickbeacroft is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-23-2008, 04:35 PM
serjant's Avatar
Member
 
Join Date: Jun 2008
Location: Israel,Tel-Aviv
Posts: 41
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Oh,can you post the part of your code here,so that we can debug it.
Thanks in advance
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-23-2008, 04:55 PM
Member
 
Join Date: Jun 2008
Posts: 6
nickbeacroft is on a distinguished road
Thanks, I've tried to include all the relevant code, as the actual class is huge. Thanks for your help

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! }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-23-2008, 05:35 PM
Member
 
Join Date: Jun 2008
Posts: 6
nickbeacroft is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-23-2008, 06:23 PM
serjant's Avatar
Member
 
Join Date: Jun 2008
Location: Israel,Tel-Aviv
Posts: 41
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Maybe you have problem with ActionListener which is passed to the JButton,becuase look at my code and it works excellent:
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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unsupported Content-Type: text/html Supported ones are: [text/xml] luislopezco Advanced Java 0 05-26-2008 05:26 PM
refresh JPanel olesja AWT / Swing 1 04-16-2008 04:58 PM
refresh a different window marceldupont AWT / Swing 3 03-22-2008 03:44 AM
Edit JPanel Text During Runtime...from another class bdn1404 New To Java 5 08-11-2007 04:14 AM
How To:Use a JSlider to adjust Text size in a JPanel louiebagz AWT / Swing 2 07-01-2007 08:37 AM


All times are GMT +3. The time now is 07:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org