0

I have this for my jtextarea i try a lot of things to align it on center or right but it's always outside the windows

This is the result I have for now :

With this code :

    Popup noticePopup = new Popup("Notice" , 1500, 900);
                JPanel noticePanel = new JPanel();
                noticePanel.setPreferredSize(new Dimension(1500,1000));
                List<List> ListNote = controller.Medicament.consultationNotice(idMedoc);
                String noticeThis =null;
                for(int n=0;n<ListNote.size();n++) {
                    noticeThis = (String) ListNote.get(n).get(1);
                }
                JTextArea noticeArea = new JTextArea(noticeThis);
                noticePopup.add(noticePanel);
                noticePanel.add(noticeArea);
                noticeArea.setAlignmentX(JTextArea.CENTER_ALIGNMENT);

Thank you

camickr
  • 321,443
  • 19
  • 166
  • 288
mel
  • 11
  • 2
  • Is `JTextPane` good for you? Because `JTextArea` has some handicaps about text alignment. Take a look here: https://stackoverflow.com/questions/24315757/java-align-jtextarea-to-the-right – FonzTech Mar 14 '19 at 09:58
  • Why are you using `Popup` a "heavyweight" *AWT* component wile the others are "lightweight" *Swing* components? – Usagi Miyamoto Mar 14 '19 at 10:04
  • because I need to have this text on a new page – mel Mar 15 '19 at 10:09

1 Answers1

0

you can try something like JTextPane rather than JTextArea

JTextPane textPane = new JTextPane();

SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);
textPane.setParagraphAttributes(attributes, true);

if you want to used JTextArea then you need to add in scrollpane and then scorllpane will be part of Panel please try below code

                JTextArea text = new JTextArea(-, -);
                JScrollPane sPane = new JScrollPane( text );
                JPanel panel = new JPanel();
                panel.add( sPane );

                sPane.addAncestorListener( new AncestorListener()
                {
                    public void ancestorAdded(AncestorEvent e)
                    {
                        JScrollPane sPane = (JScrollPane)e.getComponent();
                        sPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    }
                });

hope it will work for you

NPE
  • 429
  • 2
  • 16
  • no i want to use a jtextarea because i need that for put a variable on it – mel Mar 15 '19 at 10:08
  • added with JTextArea plz try – NPE Mar 15 '19 at 10:42
  • I cant use scroll pane because it's extends on jpanel not jframe ( for some requierement) And with a jtextarea i tried you're colution CRR is not working too... – mel Mar 15 '19 at 11:39
  • `i need that for put a variable on it` - why do you think you can't use a variable with a JTextPane? Use the setText(...) method. – camickr Mar 15 '19 at 14:46