Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Swing problem, newbie'ish . Perhaps thread related?

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing problem, newbie'ish . Perhaps thread related?

    Hi- this is working (kind of ) , but its instantly refreshing over the boxes Im trying to draw. I can only see the boxes briefly when resizing the window. So its like they are drawn and then instantly drawn over. I've been pulling out small chunks of hair all morning over it...!!! Please someone save me !

    Heres some code, its part of a big program I'm writing, so I've tried to cut out lots of the irrelevant stuff.

    import java.awt.*;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
     
    public class Artything extends JPanel {
     
    private static final long serialVersionUID = 7747161700374293379L;
    public Artything(Graphics g)
    {
    g2d = (Graphics2D) g;
    }
     
    Graphics2D g2d;
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (int i=10; i<300; i++){
    g2d.fillRect(10+i, 10+i, 100, 150);
    }
    }
    }

    Here is the class I'm trying to use it inside...or a small part of it

    public class NewJFrame extends javax.swing.JFrame {
    ....
    public JPanel labelLayoutJPanel;
    ....
    pack();
    ArtyThing renderer = new ArtyThing labelLayoutJPanel.getGraphics());
    labelLayoutJPanel.add(renderer);
    ....
    }

    Any help appreciated. Thanks

    PS Its all running from here:

    import java.util.Vector;
    import javax.swing.SwingUtilities;
     
    public class RunClient {
    public static void main(String[] args) {
     
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    DatabaseManager dbm = new DatabaseManager();
    Vector<String> list = dbm.returnList();
    NewJFrame inst = new NewJFrame();
    inst.initGUI(list);
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    }
     
    }

    Tell me its not a threading problem please Anyone who helps is welcome to free guitar advice =P
    Last edited by JavaPF; July 22nd, 2009 at 04:43 AM. Reason: Please use [code] [/code] tags


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing problem, newbie'ish . Perhaps thread related?

    Sorry I spotted a mistake in the original post

    Hi- this is working (kind of ) , but its instantly refreshing over the boxes Im trying to draw. I can only see the boxes briefly when resizing the window. So its like they are drawn and then instantly drawn over. I've been pulling out small chunks of hair all morning over it...!!! Please someone save me !

    Heres some code, its part of a big program I'm writing, so I've tried to cut out lots of the irrelevant stuff.

    import java.awt.*;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
     
    public class Artything extends JPanel {
     
    private static final long serialVersionUID = 7747161700374293379L;
    public Artything(Graphics g)
    {
    g2d = (Graphics2D) g;
    }
     
    Graphics2D g2d;
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (int i=10; i<300; i++){
    g2d.fillRect(10+i, 10+i, 100, 150);
    }
    }
    }

    Here is the class I'm trying to use it inside...or a small part of it

    public class NewJFrame extends javax.swing.JFrame {
    ....
    public JPanel labelLayoutJPanel;
    ....
    pack();
    ArtyThing renderer = new ArtyThing(this.labelLayoutJPanel.getGraphics());
    labelLayoutJPanel.add(renderer);
    ....
    }

    Any help appreciated. Thanks

    PS Its all running from here:

    import java.util.Vector;
    import javax.swing.SwingUtilities;
     
    public class RunClient {
    public static void main(String[] args) {
     
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    DatabaseManager dbm = new DatabaseManager();
    Vector<String> list = dbm.returnList();
    NewJFrame inst = new NewJFrame();
    inst.initGUI(list);
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    }
     
    }

    Tell me its not a threading problem please Anyone who helps is welcome to free guitar advice =P

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing problem, newbie'ish . Perhaps thread related?

    Ok - repaint(); Inserted into the paint method seems to have fixed it. No idea why. But thats hacking for you

    SOLVED.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: Swing problem, newbie'ish . Perhaps thread related?

    Quote Originally Posted by fenderman View Post
    Ok - repaint(); Inserted into the paint method seems to have fixed it. No idea why. But thats hacking for you

    SOLVED.
    well that was easy I'm glad you solved it fenderman.

    If you ever need to edit your original post, click the EDIT button and you can make whatever changes you wish.

    I have marked your thread as SOLVED. Read the tutorial in my signature to see how to do it yourself.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Funny computer and office related cartoons
    By Flash in forum The Cafe
    Replies: 6
    Last Post: April 9th, 2010, 04:13 AM
  3. How to Use a JSlider - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 29th, 2010, 09:33 AM
  4. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM