1

This code is creating a problem that is when I click the button two strings are being painted one horizontal and one vertical, but need only horizontal string to be painted, so please tell what should I do???

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;





public class R implements ActionListener {


    static int y;
    CustomProgressBar b = new CustomProgressBar();



    public static void main(String arg[]) throws Exception {
        new R();
    }



    public R() throws Exception {
        JFrame f = new JFrame();
        JButton btn = new JButton("Click");
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        f.setLayout(new FlowLayout());
        btn.addActionListener(this);
        f.add(b);
        f.add(btn);
        f.setVisible(true);
    }



    class CustomProgressBar extends JProgressBar{


        private static final long serialVersionUID = 1L;
        private boolean isStringToBePainted = false;
        public CustomProgressBar() {
            super(JProgressBar.VERTICAL,0,100);
        }



        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(isStringToBePainted ) {
                Dimension size = CustomProgressBar.this.getSize();
                if( CustomProgressBar.this.getPercentComplete()<0.9  )
                    R.y = (int)( size.height - size.height * CustomProgressBar.this.getPercentComplete() );     
                String text = getString();
                g.setColor(Color.BLACK );
                g.drawString(text, 0, R.y);
            }
        }
        @Override
        public void setStringPainted(boolean b) {
            super.setStringPainted(b);
            isStringToBePainted=b;
        }
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        b.setStringPainted(true);
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2

The problem is, the property setStringPainted is already defined within by the component and has a predefined functionality...which you know seem to want to change...

You will either, need to define a new property of your own which you can control OR change the way the UI delegate works, which is a lot of work as you will need to provide one for each look and feel you might want to support...

Instead of doing custom painting, you could cheat (a little)... and use a JLabel instead, for example...

Progress

class CustomProgressBar extends JProgressBar {

    private static final long serialVersionUID = 1L;
    private boolean isStringToBePainted = false;

    private JLabel progress;

    public CustomProgressBar() {
        super(JProgressBar.VERTICAL, 0, 100);

        progress = new JLabel("0%");
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weighty = 1;
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.SOUTH;
        add(progress, gbc);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        Dimension labelSize = progress.getPreferredSize();
        Insets insets = getInsets();
        if (size.width < labelSize.width) {
            size.width = insets.left + insets.right + labelSize.width;
        }
        return size;
    }

}

You're still going to need to provide your own property to turn it on or off, but it's an idea...

(ps- I had a quick look at trying to implement my own UI delegate, after I copy and pasted my third method, I gave up, as it would just be more work then the reward would provide)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366