7

I've been doing some research about Swing in order to build a css editor with Java. I'm stuck trying to export CSS and HTML in JTextArea's ( I'll after create .css document. ) Here is the GridLayout that my main layout calls after clicking "Build" menu item.

package csseditor_gui_built;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.text.DefaultCaret;
import java.awt.Font;
import java.awt.Color;


public class ExportGridLayout extends JFrame {
    public ExportGridLayout(String HTML, String CSS){


        GridLayout layout = new GridLayout(1,2,2,2);
        setLayout(layout);

        JTextArea textAreaHtml = new JTextArea();
        JTextArea textAreaCss = new JTextArea();

        //Creating a new font.
        Font fontumuz = new Font("Courier New", Font.PLAIN, 12);

        // Setting constructor strings
        textAreaHtml.setText(HTML);
        textAreaCss.setText(CSS);

        //Additional details..
        textAreaHtml.setEditable(false);
        textAreaCss.setEditable(false);

        //Appending font to the textArea's
        textAreaHtml.setFont(fontumuz);
        textAreaCss.setFont(fontumuz);

        // Adding the objects to JFrame
        add(textAreaHtml);
        add(textAreaCss);

    }
}

It's pretty straight forward. Just help me adding scroll bars or panes to these textArea's. Any other suggestions in the website do not work.

mozcelikors
  • 2,582
  • 8
  • 43
  • 77
  • 1
    For doing HTML related work, consider using [JTextPane/JEditorPane](http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html), `JTextArea` is not meant for such things, IMHO. – nIcE cOw Jun 24 '12 at 17:06
  • It's the same thing, I will use, but just cant make them scrollable – mozcelikors Jun 24 '12 at 17:21

1 Answers1

8

Its this way...

JTextArea text = new JTextArea();

JScrollPane scroll = new JScrollPane(text);

Edited part

add(scroll);

Here is one working code for your help :

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

public class JTextAreaExample
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("JTextArea Scrollable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(1, 2, 2, 2));

        JTextArea tArea1 = new JTextArea();
        tArea1.setLineWrap(true);
        JTextArea tArea2 = new JTextArea();
        tArea2.setLineWrap(true);
        tArea1.setText("I got a long long line of text in my JTextArea");
        tArea2.setText("I got a long long line of text in my JTextArea");

        JScrollPane scroller1 = new JScrollPane();
        JScrollPane scroller2 = new JScrollPane();
        scroller1.setViewportView(tArea1);
        scroller2.setViewportView(tArea2);

        contentPane.add(scroller1);
        contentPane.add(scroller2);

        frame.setContentPane(contentPane);
        frame.setSize(100, 100);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JTextAreaExample().createAndDisplayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • I'm afraid it doesnt work. Just try it on the compiler. Is it because I use GridLayout or not, I dont know – mozcelikors Jun 24 '12 at 16:47
  • It works fine for JTextArea... And yes, i hope you have enough entry to make the JTextArea full so that scrollbar appears, also set the Frame Size. – Kumar Vivek Mitra Jun 24 '12 at 16:51
  • It really doesnt work. I've too much entry. The textareas are supposed to output the code of a whole webpage. Well, they do. But without scroll bars. – mozcelikors Jun 24 '12 at 16:52
  • Ok try this... Now see the code, add the scroll pane, not the textArea – Kumar Vivek Mitra Jun 24 '12 at 16:54
  • No, still doesnt. Its not that simple I think, I browsed many forums, they used many ways to achieve this. Its easy with NetBeans, but I'm trying not to use it. Btw, add(scroll); adds something (like a panel) in the grid layout, but not scrolls to the jtextarea's. Please compile the code on your IDE and see whats wrong. – mozcelikors Jun 24 '12 at 17:06
  • Listen.... make a fresh project, and try creating a JTextArea and a JScrollPane, and add it to a frame... see it will work..once it does, try adding another layout apart from grid and see it... if that works too then add the grid and try it out. – Kumar Vivek Mitra Jun 24 '12 at 17:08
  • Could you post the code that worked with you, so that I can work on it? – mozcelikors Jun 24 '12 at 17:20
  • 2
    +1 works for me. See also `HTMLDocumentEditor`, mentioned [here](http://stackoverflow.com/a/10160836/230513). – trashgod Jun 24 '12 at 17:21
  • +1 works like a charm for me, no worry related to outcome :-) Pardon me for my edit, if you feel there is something wrong, feel free to change it as you like or present a new one if that seems fit to you. – nIcE cOw Jun 24 '12 at 17:25
  • Worked perfectly fine, thanks a lot.. I guess the problem was with viewports and setContentPane stuff.. – mozcelikors Jun 24 '12 at 17:36
  • @user1478315 : The real problem might can neither `Viewport` nor `setContentPane()`, but since you hadn't provided any value for your `Rows and Columns` while initiating your `JTextArea`, that might can trigger this behaviour as well. Try to write something like this `JTextArea tArea1 = new JTextArea(2, 10);`, that will bring the `ScrollPane` easily, even if you use `frameObject.pack()` instead of providing arbitrary values :-) – nIcE cOw Jun 24 '12 at 18:01
  • I tried and I think the problem is not to use JPanel, adding the components to a JPanel is the real solution, for my version of the code or the solution code. – mozcelikors Jun 24 '12 at 18:12
  • 2
    I was about to suggest using JPanel, but before that you got it right.... Congrats.... – Kumar Vivek Mitra Jun 24 '12 at 18:35