0

I am trying to add a scrollbar to a jtextarea which is right rotated on loading. However the scrollpane does not work as expected. The need is to get a scrollpane added to the right rotated jtextarea. Any suggestion for the same is welcome. Original code snippet is attached

import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;

public class InhibitScreenRightRotated extends JPanel {
    public InhibitScreenRightRotated() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        InhibitScreenRightRotated inhibitScreenRightRotated = new InhibitScreenRightRotated();
        JFrame f = new JFrame();
        f.setSize(450, 450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(inhibitScreenRightRotated);
        f.setVisible(true);
    }

    public String inhibitList() {

        String str = null;

        for (int i = 0; i < 100; i++) {
            str = str + "Room door is open. Close door to continue." + i + "\n";
        }
        return str;

    }

    public void jbInit() throws Exception {
        this.setBounds(0, 0, 400, 375);
        this.setLayout(null);
        this.setBorder(new BevelBorder(0, new Color(255, 255, 255, 255), new Color(182, 182, 182, 255),
                new Color(62, 62, 255, 255), new Color(89, 89, 89, 255)));
        this.setBackground(new Color(47, 61, 110, 255));

        rotatingLabel.setBounds(new Rectangle(334, 2, 40, 399));
        rotatingLabel.setForeground(new Color(255, 255, 255, 255));
        rotatingLabel.setText("System Inhibits");
        rotatingLabel.setFont(new Font("Dialog", 1, 14));
        rotatingLabel.setName("INHIBITS_LIST_HEADER");

        jButton1.setBackground(new Color(141, 152, 190, 255));
        // jButton1.setBorder(new CuiBorder());
        jButton1.setFocusPainted(false);
        jButton1.setText("Close");
        jButton1.setName("INHIBITS_LIST_CLOSE");

        jTextArea1.getCaret().deinstall(jTextArea1);
        jTextArea1.setEditable(false);
        jTextArea1.setBounds(new Rectangle(9, 42, 382, 275));
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setLineWrap(true);
        jTextArea1.setBackground(new Color(186, 193, 218, 255));
        jTextArea1.setText(inhibitList());
        jTextArea1.setName("INHIBITS_LIST_TEXTAREA");
        // setDocument... needs to be done

        jButton1.setBounds(new Rectangle(15, ((jTextArea1.getWidth() + 9) - 83), 30, 83));

        this.add(rotatingLabel);
        this.add(jTextArea1);
        this.add(jButton1);

    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(340, 10);
        g2d.rotate(Math.toRadians(90));
        jTextArea1.setVisible(false);
        jTextArea1.update(g2d);
        g2d.dispose();

    }

    JTextArea jTextArea1 = new JTextArea();
    JScrollPane scrollPane = new JScrollPane();
    public static final Font FONT_MSEC_LABEL_VALUE = new Font("Arial", Font.BOLD, 16);
    RotatingLabel rotatingLabel = new RotatingLabel(Math.toRadians(90), FONT_MSEC_LABEL_VALUE, "System Inhibits", 0,
            -170);
    IUIRotatedButton jButton1 = new IUIRotatedButton();

    public class IUIRotatedButton extends JButton {
        private String str = "";

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int iX = getWidth() / 2;
            int iY = getHeight() / 2;
            g2.rotate(+Math.PI / 2, iX, iY);
            g2.drawString(str, iX - 15, iY + 5);
        }

        public void setText(String str) {
            this.str = str;
            repaint();
        }
    }

    public class RotatingLabel extends JLabel {

        private Font font = null;
        private String sText = null;
        private double dThetaInRadians = 0;
        private int iXExtra = 15;
        private int iYExtra = 25;
        private Icon icon = null;

        public RotatingLabel(double dThetaInRadians, Font font, String sText, int iXExtra, int iYExtra) {
            super();
            this.font = font;
            this.sText = sText;
            this.dThetaInRadians = dThetaInRadians;
            this.iXExtra = iXExtra;
            this.iYExtra = iYExtra;
            this.setHorizontalAlignment(SwingConstants.CENTER);
            this.setVerticalAlignment(SwingConstants.CENTER);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setFont(font);
            int iX = getWidth() / 2 + iXExtra;
            int iY = getHeight() / 2 + iYExtra;
            g2.rotate(dThetaInRadians, iX, iY);
            if (sText != null)
                g2.drawString(sText, iX, iY);
            if (icon != null) {
                g2.drawImage(((ImageIcon) icon).getImage(), iX, iY, this);
            }
        }

        public void setText(String sText) {
            // uncomment it for testing only.
            // super.setText(sText);
            this.sText = sText;
            repaint();
        }
    }
}

In the above code block , I added JScrollPane for the JTextArea, however the scrollpane is not working as expected

        import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;

public class InhibitScreenRightRotated extends JPanel {
    public InhibitScreenRightRotated() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        InhibitScreenRightRotated inhibitScreenRightRotated = new InhibitScreenRightRotated();
        JFrame f = new JFrame();
        f.setSize(450, 450);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(inhibitScreenRightRotated);
        f.setVisible(true);
    }

    public String inhibitList() {

        String str = null;

        for (int i = 0; i < 100; i++) {
            str = str + "Room door is open. Close door to continue." + i + "\n";
        }
        return str;

    }

    public void jbInit() throws Exception {
        this.setBounds(0, 0, 400, 375);
        this.setLayout(null);
        this.setBorder(new BevelBorder(0, new Color(255, 255, 255, 255), new Color(182, 182, 182, 255),
                new Color(62, 62, 255, 255), new Color(89, 89, 89, 255)));
        this.setBackground(new Color(47, 61, 110, 255));

        rotatingLabel.setBounds(new Rectangle(334, 2, 40, 399));
        rotatingLabel.setForeground(new Color(255, 255, 255, 255));
        rotatingLabel.setText("System Inhibits");
        rotatingLabel.setFont(new Font("Dialog", 1, 14));
        rotatingLabel.setName("INHIBITS_LIST_HEADER");

        jButton1.setBackground(new Color(141, 152, 190, 255));
        // jButton1.setBorder(new CuiBorder());
        jButton1.setFocusPainted(false);
        jButton1.setText("Close");
        jButton1.setName("INHIBITS_LIST_CLOSE");

        jTextArea1.getCaret().deinstall(jTextArea1);
        jTextArea1.setEditable(false);
        jTextArea1.setBounds(new Rectangle(9, 42, 382, 275));
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setLineWrap(true);
        jTextArea1.setBackground(new Color(186, 193, 218, 255));
        jTextArea1.setText(inhibitList());
        jTextArea1.setName("INHIBITS_LIST_TEXTAREA");
        // setDocument... needs to be done

        scrollPane.setViewportView(jTextArea1);
        scrollPane.setBounds(new Rectangle(69, 10, 275, 382));
        scrollPane.setBackground(Color.white);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        jButton1.setBounds(new Rectangle(15, ((jTextArea1.getWidth() + 9) - 83), 30, 83));

        this.add(rotatingLabel);
        this.add(scrollPane);
        this.add(jButton1);

    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(340, 10);
        g2d.rotate(Math.toRadians(90));
        jTextArea1.setVisible(false);
        jTextArea1.update(g2d);
        g2d.dispose();

    }

    JTextArea jTextArea1 = new JTextArea();
    JScrollPane scrollPane = new JScrollPane();
    public static final Font FONT_MSEC_LABEL_VALUE = new Font("Arial", Font.BOLD, 16);
    RotatingLabel rotatingLabel = new RotatingLabel(Math.toRadians(90), FONT_MSEC_LABEL_VALUE, "System Inhibits", 0,
            -170);
    IUIRotatedButton jButton1 = new IUIRotatedButton();

    public class IUIRotatedButton extends JButton {
        private String str = "";

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int iX = getWidth() / 2;
            int iY = getHeight() / 2;
            g2.rotate(+Math.PI / 2, iX, iY);
            g2.drawString(str, iX - 15, iY + 5);
        }

        public void setText(String str) {
            this.str = str;
            repaint();
        }
    }

    public class RotatingLabel extends JLabel {

        private Font font = null;
        private String sText = null;
        private double dThetaInRadians = 0;
        private int iXExtra = 15;
        private int iYExtra = 25;
        private Icon icon = null;

        public RotatingLabel(double dThetaInRadians, Font font, String sText, int iXExtra, int iYExtra) {
            super();
            this.font = font;
            this.sText = sText;
            this.dThetaInRadians = dThetaInRadians;
            this.iXExtra = iXExtra;
            this.iYExtra = iYExtra;
            this.setHorizontalAlignment(SwingConstants.CENTER);
            this.setVerticalAlignment(SwingConstants.CENTER);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setFont(font);
            int iX = getWidth() / 2 + iXExtra;
            int iY = getHeight() / 2 + iYExtra;
            g2.rotate(dThetaInRadians, iX, iY);
            if (sText != null)
                g2.drawString(sText, iX, iY);
            if (icon != null) {
                g2.drawImage(((ImageIcon) icon).getImage(), iX, iY, this);
            }
        }

        public void setText(String sText) {
            // uncomment it for testing only.
            // super.setText(sText);
            this.sText = sText;
            repaint();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Simply rotating the `Graphics` context of a component won't be enough, you've not rotated the coordinates or the dimensions or the mouse events or a swagger of other properties you'd need to in order to achieve the result. Instead, you should be making use of the `JLayer` API, [for example](http://stackoverflow.com/questions/22976226/is-there-any-way-i-can-rotate-this-90-degrees/22976755#22976755) and [example](http://stackoverflow.com/questions/25252127/java-rotating-non-square-jpanel-component/25253453#25253453) – MadProgrammer Mar 14 '17 at 08:00
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Mar 14 '17 at 08:01
  • and [example](http://stackoverflow.com/questions/14324460/rotating-a-jtextfield-vertically/14328881#14328881) – MadProgrammer Mar 14 '17 at 08:02

0 Answers0