Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




Textarea and Scrollbars

 
Reply to this topicStart new topic

Textarea and Scrollbars

mrithula
18 Sep, 2007 - 07:35 PM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2007
Posts: 11


My Contributions
Hi,friends...
i am programming for builiding an IDE...I have written two differnt codes...one for creating the text area...and another for the scroll bars...but if i want to get the scroll bars and the text area in the same program..what should i do....i tried integrating the two codes but i get two different screens as output...(one displaying scrollbar other the text area..)

i ll attach the codes here..for ur reference...


CODE

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

public class BasicTextEditor1 extends JFrame
{
    public static final String FONTS[] = { "Serif", "SansSerif",
        "Courier" };
    protected Font m_fonts[];

    protected JTextArea m_monitor;
    protected JMenuItem[] m_fontMenus;
    protected JCheckBoxMenuItem m_bold;
    protected JCheckBoxMenuItem m_italic;

    protected JFileChooser m_chooser;

    public BasicTextEditor1()
    {
        super("Basic text editor: part I - Menus");
        setSize(450, 350);

        m_fonts = new Font[FONTS.length];
        for (int k = 0; k < FONTS.length; k++)
           m_fonts[k] = new Font(FONTS[k], Font.PLAIN, 12);

        m_monitor = new JTextArea();
        JScrollPane ps = new JScrollPane(m_monitor);
        getContentPane().add(ps, BorderLayout.CENTER);

        m_monitor.append("Basic text editor");

        JMenuBar menuBar = createMenuBar();
        setJMenuBar(menuBar);

        m_chooser = new JFileChooser();
        m_chooser.setCurrentDirectory(new File("."));

        WindowListener wndCloser = new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        };
        addWindowListener(wndCloser);
        
        updateMonitor();
        setVisible(true);
    }

    protected JMenuBar createMenuBar()
    {
        final JMenuBar menuBar = new JMenuBar();
        
        JMenu mFile = new JMenu("File");
        mFile.setMnemonic('f');

        JMenuItem item = new JMenuItem("New");
        item.setIcon(new ImageIcon("file_new.gif"));
        item.setMnemonic('n');
        ActionListener lst = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                m_monitor.setText("");
            }
        };
        item.addActionListener(lst);
        mFile.add(item);

        item = new JMenuItem("Open...");
        item.setIcon(new ImageIcon("file_open.gif"));
        item.setMnemonic('o');
        lst = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                                BasicTextEditor1.this.repaint();
                if (m_chooser.showOpenDialog(BasicTextEditor1.this) !=
                    JFileChooser.APPROVE_OPTION)
                    return;
                                Thread runner = new Thread() {
                                  public void run() {
                    File fChoosen = m_chooser.getSelectedFile();
                    try
                    {
                    FileReader in = new FileReader(fChoosen);
                    m_monitor.read(in, null);
                    in.close();
                    }
                    catch (IOException ex)
                    {
                    ex.printStackTrace();
                    }
                                  }
                                };
                                runner.start();
            }
        };
        item.addActionListener(lst);
        mFile.add(item);

        item = new JMenuItem("Save...");
        item.setIcon(new ImageIcon("file_save.gif"));
        item.setMnemonic('s');
        lst = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                                BasicTextEditor1.this.repaint();
                if (m_chooser.showSaveDialog(BasicTextEditor1.this) !=
                    JFileChooser.APPROVE_OPTION)
                    return;
                                Thread runner = new Thread() {
                                  public void run() {
                     File fChoosen = m_chooser.getSelectedFile();
                    try
                    {
                    FileWriter out = new FileWriter(fChoosen);
                    m_monitor.write(out);
                    out.close();
                    }
                    catch (IOException ex)
                    {
                    ex.printStackTrace();
                    }
                                  }
                                };
                                runner.start();
            }
        };
        item.addActionListener(lst);
        mFile.add(item);

        mFile.addSeparator();

        item = new JMenuItem("Exit");
        item.setMnemonic('x');
        lst = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        };
        item.addActionListener(lst);
        mFile.add(item);
        menuBar.add(mFile);

        ActionListener fontListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                updateMonitor();
            }
        };
        
        JMenu mFont = new JMenu("Font");
        mFont.setMnemonic('o');

        ButtonGroup group = new ButtonGroup();
        m_fontMenus = new JMenuItem[FONTS.length];
        for (int k = 0; k < FONTS.length; k++)
        {
            int m = k+1;
            m_fontMenus[k] = new JRadioButtonMenuItem(
                m+" "+FONTS[k]);
            boolean selected = (k == 0);
            m_fontMenus[k].setSelected(selected);
            m_fontMenus[k].setMnemonic('1'+k);
            m_fontMenus[k].setFont(m_fonts[k]);
            m_fontMenus[k].addActionListener(fontListener);
            group.add(m_fontMenus[k]);
            mFont.add(m_fontMenus[k]);
        }
        
        mFont.addSeparator();

        m_bold = new JCheckBoxMenuItem("Bold");
        m_bold.setMnemonic('b');
        Font fn = m_fonts[1].deriveFont(Font.BOLD);
        m_bold.setFont(fn);
        m_bold.setSelected(false);
        m_bold.addActionListener(fontListener);
        mFont.add(m_bold);

        m_italic = new JCheckBoxMenuItem("Italic");
        m_italic.setMnemonic('i');
        fn = m_fonts[1].deriveFont(Font.ITALIC);
        m_italic.setFont(fn);
        m_italic.setSelected(false);
        m_italic.addActionListener(fontListener);
        mFont.add(m_italic);

        menuBar.add(mFont);

        return menuBar;
    }

    protected void updateMonitor()
    {
        int index = -1;
        for (int k = 0; k < m_fontMenus.length; k++)
        {
            if (m_fontMenus[k].isSelected())
            {
                index = k;
                break;
            }
        }
        if (index == -1)
            return;

        if (index==2)  // Courier
        {
            m_bold.setSelected(false);
            m_bold.setEnabled(false);
            m_italic.setSelected(false);
            m_italic.setEnabled(false);
        }
        else
        {
            m_bold.setEnabled(true);
            m_italic.setEnabled(true);
        }

        int style = Font.PLAIN;
        if (m_bold.isSelected())
            style |= Font.BOLD;
        if (m_italic.isSelected())
            style |= Font.ITALIC;
        Font fn = m_fonts[index].deriveFont(style);
        m_monitor.setFont(fn);
        m_monitor.repaint();
    }
public static void main(String argv[])
    {
        new BasicTextEditor();
                new ScrollDemo();
    }
    
}
class ScrollDemo extends JFrame
{
  public ScrollDemo() {
    super("JScrollBar Demo");
    setSize(300,250);

    ImageIcon ii = new ImageIcon("earth.jpg");
    CustomScrollPane sp = new CustomScrollPane(new JLabel(ii));
    getContentPane().add(sp);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    setVisible(true);
  }

}

class CustomScrollPane extends JPanel
{
  protected JScrollBar m_vertSB;
  protected JScrollBar m_horzSB;
  protected CustomViewport m_viewport;
  protected JComponent m_comp;

  public CustomScrollPane(JComponent comp) {
    setLayout(null);
    m_viewport = new CustomViewport();
    m_viewport.setLayout(null);
    add(m_viewport);
    m_comp = comp;
    m_viewport.add(m_comp);

    m_vertSB = new JScrollBar(
      JScrollBar.VERTICAL, 0, 0, 0, 0);
    m_vertSB.setUnitIncrement(5);
    add(m_vertSB);

    m_horzSB = new JScrollBar(
      JScrollBar.HORIZONTAL, 0, 0, 0, 0);
    m_horzSB.setUnitIncrement(5);
    add(m_horzSB);

    AdjustmentListener lst = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        m_viewport.doLayout();
      }
    };
    m_vertSB.addAdjustmentListener(lst);
    m_horzSB.addAdjustmentListener(lst);
  }

  public void doLayout() {
    Dimension d = getSize();
    Dimension d0 = m_comp.getPreferredSize();
    Dimension d1 = m_vertSB.getPreferredSize();
    Dimension d2 = m_horzSB.getPreferredSize();

    int w = Math.max(d.width - d1.width-1, 0);
    int h = Math.max(d.height - d2.height-1, 0);
    m_viewport.setBounds(0, 0, w, h);
    m_vertSB.setBounds(w+1, 0, d1.width, h);
    m_horzSB.setBounds(0, h+1, w, d2.height);

    int xs = Math.max(d0.width - w, 0);
    m_horzSB.setMaximum(xs);
    m_horzSB.setBlockIncrement(xs/5);
    m_horzSB.setEnabled(xs > 0);

    int ys = Math.max(d0.height - h, 0);
    m_vertSB.setMaximum(ys);
    m_vertSB.setBlockIncrement(ys/5);
    m_vertSB.setEnabled(ys > 0);

    m_horzSB.setVisibleAmount(m_horzSB.getBlockIncrement());
    m_vertSB.setVisibleAmount(m_vertSB.getBlockIncrement());
  }

  public Dimension getPreferredSize() {
    Dimension d0 = m_comp.getPreferredSize();
    Dimension d1 = m_vertSB.getPreferredSize();
    Dimension d2 = m_horzSB.getPreferredSize();
    Dimension d = new Dimension(d0.width+d1.width,
      d0.height+d2.height);
    return d;
  }

  class CustomViewport extends JPanel
  {
    public void doLayout() {
      Dimension d0 = m_comp.getPreferredSize();
      int x = m_horzSB.getValue();
      int y = m_vertSB.getValue();
      m_comp.setBounds(-x, -y, d0.width, d0.height);
    }
  }
}


This post has been edited by William_Wilson: 18 Sep, 2007 - 08:23 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Textarea And Scrollbars
18 Sep, 2007 - 08:28 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
please remember to use code tags.

To use a scroll pane properly, you create the JTextarea, and pass it to the JScrollPane:
CODE

JScrollPane scrollPane = new  ScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane. HORIZONTAL_SCROLLBAR_NEVER);

The 2nd and 3rd parameters determine when/if the scrollbars appear
_NEVER //never display
_AS_NEEDED //when text is too large
_ALWAYS //always visible

adding either HORIZONTAL_SCROLLBAR or VERTICAL_SCROLLBAR to the front of the above.
User is offlineProfile CardPM
+Quote Post

mrithula
RE: Textarea And Scrollbars
18 Sep, 2007 - 09:39 PM
Post #3

New D.I.C Head
*

Joined: 16 Sep, 2007
Posts: 11


My Contributions
QUOTE(William_Wilson @ 18 Sep, 2007 - 09:28 PM) *

please remember to use code tags.

To use a scroll pane properly, you create the JTextarea, and pass it to the JScrollPane:
CODE

JScrollPane scrollPane = new  ScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane. HORIZONTAL_SCROLLBAR_NEVER);

The 2nd and 3rd parameters determine when/if the scrollbars appear
_NEVER //never display
_AS_NEEDED //when text is too large
_ALWAYS //always visible

adding either HORIZONTAL_SCROLLBAR or VERTICAL_SCROLLBAR to the front of the above.



Ya...i ll try...
thanks a lot.........
User is offlineProfile CardPM
+Quote Post

gyron
RE: Textarea And Scrollbars
19 Sep, 2007 - 02:20 AM
Post #4

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
Or you could do it like this:

CODE

jScrollPane1.setViewportView(getJTextArea());
            jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


Were the method getJTextArea returns a JTextArea.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month