Results 1 to 9 of 9
Thread: Can't refresh a JPanel/text
- 06-22-2008, 10:39 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 6
- Rep Power
- 0
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
- 06-22-2008, 11:02 PM #2
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?
-
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.
- 06-23-2008, 02:41 AM #4
Then pass this method to a JFrame on which all components are,so the u can update them every time when you want to.Java Code:/* Calls updateUI on all sub-components of the JFrame */ private void updateUI() { SwingUtilities.updateComponentTreeUI(this); }
- 06-23-2008, 02:39 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 6
- Rep Power
- 0
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
- 06-23-2008, 03:35 PM #6
Oh,can you post the part of your code here,so that we can debug it.
Thanks in advance
- 06-23-2008, 03:55 PM #7
Member
- Join Date
- Jun 2008
- Posts
- 6
- Rep Power
- 0
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! }
- 06-23-2008, 04:35 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 6
- Rep Power
- 0
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.
- 06-23-2008, 05:23 PM #9
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
-
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM -
refresh JPanel
By olesja in forum AWT / SwingReplies: 1Last Post: 04-16-2008, 03:58 PM -
refresh a different window
By marceldupont in forum AWT / SwingReplies: 3Last Post: 03-22-2008, 02:44 AM -
Edit JPanel Text During Runtime...from another class
By bdn1404 in forum New To JavaReplies: 5Last Post: 08-11-2007, 03:14 AM -
How To:Use a JSlider to adjust Text size in a JPanel
By louiebagz in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 07:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks