Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable

Thread: JTextPane/Custom JPanel problem

  1. #1
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default JTextPane/Custom JPanel problem

    Hi,
    I created a custom JPanel with paintComponent() and paintBorder() overridden. The problem is: when i add a JTextPane with the "Opaque" property set to false, it wont display the text. I'm thinking that the text is behind the painting of the panel since if i set the JTextPanel to be opaque whenever it creates a new line, the Panel repaints itslef above the new line.
    Here are the overridden paintComponent() and paintBorder() methods:
    Java Code:
    @Override
    	public void paintComponent(Graphics g){
    		this.w = getWidth();
    		this.h = getHeight();
    		this.arcw = getWidth()/wRatio;
    		this.arch = getHeight()/hRatio;
    		re = new RoundRectangle2D.Double(st.getLineWidth()/2,st.getLineWidth()/2,w-(st.getLineWidth()/0.5), h-(st.getLineWidth()/0.5), arcw, arch);
    
    		GradientPaint up = new GradientPaint(w/2, h/2, Color.WHITE, w/2, h, Color.LIGHT_GRAY, false);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
    		g2.setPaint(up);
    		g2.fill(re);
    		//super.paintComponent(g);
    		
    		
    		
    		
    		
    	}
    @Override
    	public void paintBorder(Graphics g){
    		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setColor(Color.GRAY);
    		g2.setStroke(st);
    		g2.draw(re);
    		g2.dispose();
    	
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    You didn't return to the first thread you started, not even to thank the two members who contributed responses. Why should we expect anything different here?

    db
    Fubarable likes this.

  3. #3
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    You didn't return to the first thread you started, not even to thank the two members who contributed responses. Why should we expect anything different here?

    db
    i edited post with the code of the button i made with your help, i thought that was enough.

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

    Default

    Quote Originally Posted by adwart View Post
    i edited post with the code of the button i made with your help, i thought that was enough.
    Folks can miss your edit. Since this site is staffed by volunteers, our only "pay" is your thanks, and so many appreciate a post with a little note of thanks. Even if their advice didn't help you, it's usually a good idea to thank them for their time and effort.

    As to your current problem, I think that to be able to help you, we'll need to see a small bit of compilable and runnable code, enough to reproduce your problem, but not so much as to drown us in code, basically an SSCCE.

  5. #5
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Folks can miss your edit. Since this site is staffed by volunteers, our only "pay" is your thanks, and so many appreciate a post with a little note of thanks. Even if their advice didn't help you, it's usually a good idea to thank them for their time and effort.

    As to your current problem, I think that to be able to help you, we'll need to see a small bit of compilable and runnable code, enough to reproduce your problem, but not so much as to drown us in code, basically an SSCCE.
    Yes, you are right, next time i will properly thank them.
    Here is the SSCCE:
    Java Code:
    import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.RoundRectangle2D;
    
    public class JRPanel extends JPanel{
    	private int w =400;
    	private int h  =400;
    	private RoundRectangle2D re;
    	private final BasicStroke st = new BasicStroke(2f);
    	private double wRatio = 200/15;
    	private double hRatio = 25/12;
    	private double arcw;
    	private double arch;
    	public JRPanel(){
    		super();
    		setPreferredSize(new Dimension(w,h));		
    		setFocusable(false);
    		setOpaque(false);
    	}
    @Override
    	public void paintComponent(Graphics g){
    		this.w = getWidth();
    		this.h = getHeight();
    		this.arcw = getWidth()/wRatio;
    		this.arch = getHeight()/hRatio;
    		re = new RoundRectangle2D.Double(st.getLineWidth()/2,st.getLineWidth()/2,w-(st.getLineWidth()/0.5), h-(st.getLineWidth()/0.5), arcw, arch);
    		GradientPaint up = new GradientPaint(w/2, h/2, Color.WHITE, w/2, h, Color.LIGHT_GRAY, false);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    		g2.setPaint(up);
    		g2.fill(re);
    	}
    @Override
    	public void paintBorder(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setColor(Color.GRAY);
    		g2.setStroke(st);
    		g2.draw(re);
    		g2.dispose();
    	}
    	public static void main(String[] args) {
    		JFrame j = new JFrame();
    		j.setLayout(new FlowLayout());
    		JTextArea txtArea = new JTextArea();
    		txtArea.setLineWrap(true);
    		JScrollPane scrlPane = new JScrollPane(txtArea);
    		scrlPane.setPreferredSize(new Dimension(200,50));
    		scrlPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
    		JRPanel panel = new JRPanel();
    		panel.setPreferredSize(new Dimension(300,100));
    		panel.setLayout(new FlowLayout());
    		panel.add(scrlPane);
    		txtArea.setOpaque(false);
    		scrlPane.setOpaque(false);
    		scrlPane.getViewport().setOpaque(false);
    		j.getContentPane().add(panel);
    		j.setSize(400,400);
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		j.setVisible(true);
    	}
    }

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

    Default

    I see a problem here:

    Java Code:
       public void paintBorder(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.GRAY);
          g2.setStroke(st);
          g2.draw(re);
          [color="red"][b]g2.dispose(); // **** here ****[/b][/color] 
       }
    You're calling dispose on a Graphicis object that was not created by you but rather that was given to you by the JVM, and this can be dangerous as this Graphics object might still be needed elsewhere in the program for other drawing. I would comment out the line:



    Java Code:
       public void paintBorder(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.GRAY);
          g2.setStroke(st);
          g2.draw(re);
          [color="red"][b]// g2.dispose(); [/b][/color] 
       }
    adwart likes this.

  7. #7
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I see a problem here:

    Java Code:
       public void paintBorder(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.GRAY);
          g2.setStroke(st);
          g2.draw(re);
          [color="red"][b]g2.dispose(); // **** here ****[/b][/color] 
       }
    You're calling dispose on a Graphicis object that was not created by you but rather that was given to you by the JVM, and this can be dangerous as this Graphics object might still be needed elsewhere in the program for other drawing. I would comment out the line:



    Java Code:
       public void paintBorder(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.GRAY);
          g2.setStroke(st);
          g2.draw(re);
          [color="red"][b]// g2.dispose(); [/b][/color] 
       }
    Thanks ! That was the problem. I didn't quite undertand how to use dispose yet, better check that out.

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

    Default

    Quote Originally Posted by adwart View Post
    Thanks ! That was the problem. I didn't quite undertand how to use dispose yet, better check that out.
    You are welcome. But you're sort of correct: if you create a Graphics object for instance if you derive it from the Graphics object in the paintComponent method or if you create one off of a BufferedImage, you must remember to dispose of it immediately after using it. But again, you don't do this for Graphics objects given to you by the JVM.

Similar Threads

  1. Custom JPanel
    By morris4019 in forum AWT / Swing
    Replies: 4
    Last Post: 04-08-2011, 09:40 PM
  2. JTextPane Unicode/UTF-8 problem?
    By LeonLanford in forum AWT / Swing
    Replies: 15
    Last Post: 08-16-2010, 02:36 PM
  3. problem in JTextPane
    By jperson in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 04:57 PM
  4. JTextPane focus problem
    By LeonLanford in forum AWT / Swing
    Replies: 1
    Last Post: 06-22-2010, 06:10 AM
  5. Problem in adding new JTextPane at Runtime
    By png in forum AWT / Swing
    Replies: 13
    Last Post: 12-14-2009, 12:14 PM

Tags for this Thread

Posting Permissions

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