Swing Copy Problem

  • Follow


Problem: I have a Swing GUI with two JTextAreas (Events and Events2)
and an Edit menu with one option (Copy).  It's a prototype I wrote in
order to try and figure out how to copy text from two different text
areas.  My assumption was that Swing was smart enough to determine in
what window the text was selected (highlighted) but that's not the
case.  If I just have text in one window and highlighted, the copy
works fine and I can paste the selected text into another document. 
However if text exists in both windows it gets confused and does not
work properly.

I've included a compilable version of the prototype at the end of this
posting if anyone is inclined to assist me in solving this problem.

What I need to know is the function to invoke in order to determine if
text is selected/highlighted in that JTextArea, and if it is, to copy
from that window.  Such that the actionPerformed code that performs
the copy text function would look something like this.

Copy.addActionListener(new ActionListener() { 
   public void actionPerformed(ActionEvent h) 
   {
      if(The Event Display is HIGHLIGHTED)
      { 
         JTextArea textArea =
(JTextArea)EventDisplay.scrollPane.getViewport().getView();
         textArea.copy();
      }
	
      if(The Event Display2 is HIGHLIGHTED)
      {
         JTextArea textArea2 =
(JTextArea)EventDisplay2.scrollPane.getViewport().getView();
         textArea2.copy();
      }
   }
});


-------------------------------------------------------------
Here is a compilable version of the prototype for this problem.
Thanks in advance to anyone providing me feedback.
--Eric
-------------------------------------------------------------

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.border.*;
import javax.swing.KeyStroke;
import java.awt.datatransfer.*;
import java.awt.dnd.*;


public class Copy implements ActionListener, ItemListener
{
   EventPanel EventDisplay;
   EventPanel EventDisplay2;
 
   public Copy (){}

   //*******************************
   // Creates items in the Menu Bar
   //*******************************
   //
   public JMenuBar createMenuBar()
   {
      //Create the menu bar.
      JMenuBar menuBar;
      menuBar = new JMenuBar();

      // Add the "Edit" menu title.
      JMenu menu = new JMenu("Edit");

      // Add Copy menu item.
      JMenuItem Copy = new JMenuItem("Copy");

      Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
ActionEvent.CTRL_MASK));

      menu.add(Copy); 
      
      menuBar.add(menu);

      Copy.addActionListener(new ActionListener() { 
	public void actionPerformed(ActionEvent h) 
	{ 
         JTextArea textArea =
(JTextArea)EventDisplay.scrollPane.getViewport().getView();
         textArea.copy();
	
         JTextArea textArea2 =
(JTextArea)EventDisplay2.scrollPane.getViewport().getView();
         textArea2.copy();
	}
	});

      return menuBar;
   }

   //*******************************************************
   // Generic function for setting the Grid Bag constraints 
   // when defining panel area and size.
   //*******************************************************
   //
   public void setGBconstraints(GridBagConstraints GBc, int gridX, int
gridY, int weightX, int weightY, int gwidth, int gheight)
   {
      GBc.gridx = gridX;
      GBc.gridy = gridY;
      GBc.weightx = weightX;
      GBc.weighty = weightY;
      GBc.gridwidth = gwidth;
      GBc.gridheight = gheight;
   }

   //****************************
   // Creates the Content Pane.
   //****************************
   //
   public Container createContentPane() 
   {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new GridBagLayout());
        JPanel filterPane = new JPanel(new GridBagLayout());
        JPanel eventsPane = new JPanel(new GridBagLayout());
        JPanel eventsPane2 = new JPanel(new GridBagLayout());

        GridBagConstraints GBconstraints = new GridBagConstraints();
        GBconstraints.insets = new Insets(0, 5, 0, 5);
        contentPane.setOpaque(true);

        Border line = BorderFactory.createLineBorder(Color.black);
        Font fnt = new Font("Times New Roman", Font.BOLD, 14);

        //
        // Add Event panel.
        //
        EventDisplay = new EventPanel();
        GBconstraints.fill = GridBagConstraints.BOTH;
        JScrollPane sp = EventDisplay.GetScrollPane();
        setGBconstraints(GBconstraints, 0, 0, 100, 10, 1, 3);
        eventsPane.add(sp, GBconstraints);

        TitledBorder Events = new
TitledBorder(line,"Events",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
        eventsPane.setBorder(Events);
        setGBconstraints(GBconstraints, 0, 0, 100, 100, 1, 3);
        contentPane.add(eventsPane, GBconstraints);

        //
        // Add Event2 panel.
        //
        EventDisplay2 = new EventPanel();
        GBconstraints.fill = GridBagConstraints.BOTH;
        JScrollPane sp2 = EventDisplay2.GetScrollPane();
        setGBconstraints(GBconstraints, 0, 1, 100, 10, 1, 3);
        eventsPane2.add(sp2, GBconstraints);

        TitledBorder Events2 = new
TitledBorder(line,"Events2",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
        eventsPane2.setBorder(Events2);
        setGBconstraints(GBconstraints, 0, 4, 100, 100, 1, 3);
        contentPane.add(eventsPane2, GBconstraints);

        return contentPane;
    }

   public void actionPerformed(ActionEvent e)
   {
   }

   public void itemStateChanged(ItemEvent e)
   {
   }

   //*****************************************************
    // Create the GUI and show it.  For thread safety,
    // this method should be invoked from the
    // event-dispatching thread.
    //*****************************************************
    //
    private static void createAndShowGUI()
    {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        //Create and set up the window.
        JFrame frame = new JFrame("Copy Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        Copy MainWin = new Copy();
        frame.setContentPane(MainWin.createContentPane());
        frame.setJMenuBar(MainWin.createMenuBar());
 
        //Display the window.
        frame.setSize(650, 660);
        frame.setVisible(true);
    }


   //*****************************************
   // Main function for the Copy GUI.
   //*****************************************
   //
   public static void main(final String[] args)
   {
      //Schedule a job for the event-dispatching thread:
      //creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() 
      {
         public void run()
         {
            createAndShowGUI();
         }
      });
     
   }

   public class EventPanel extends JPanel 
   {
      JTextArea EventDisplay;
      JScrollPane scrollPane;
   
      //*************************************
      // EventPanel Constructor
      //*************************************
      public EventPanel()
      {
         //Create a scrolled text area for event messages.
         //
         EventDisplay = new JTextArea(500, 100);
         EventDisplay.setEditable(true);
         EventDisplay.setLineWrap(false);
         scrollPane = new JScrollPane(EventDisplay);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

         // Set the Font for the JTextArea.
         //
         Font f = new Font("Monospaced", Font.BOLD, 14);
         EventDisplay.setFont(f);


         // JTextArea leaves no space between the edge and text that
it holds.
         // This can be fixed by adding an empty border to it.
         EventDisplay.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
      }

      //*****************************************
      // Returns scroll pane for the Even Panel
      //*****************************************
      public JScrollPane GetScrollPane()
      {
         return scrollPane;
      }
   }
}
0
Reply eric.martin (3) 11/27/2004 4:45:08 AM

On 11/26/2004 at 11:45:08 PM, Eric wrote:

> My assumption was that Swing was smart enough to determine in what
> window the text was selected (highlighted) but that's not the case.

The problem is that your base assumption, that only one component has
selected text, is incorrect.  Every text component can have a current
selection.  The fact that a text component only highlights the selection
when it is focused does not change that.

So the problem is not to determine which text component has the selection
- multiple text components do.  Rather, you need to determine which text
component has the focus (or perhaps most recently had the focus).  The
TextAction class has a method for determining that: getFocusedComponent().

To use this, you will need to abandon the ActionListener interface that
you are currently using and use Action objects instead.  That should not
be much of a problem - it would be a good thing to do anyway.  Here is an
example:

   menu.add( copyAction ); 
     :
     :
   TextAction copyAction = new TextAction( "Copy" ) {
      public void actionPerformed( ActionEvent event ) {
         getFocusedComponent().copy();
      }
   };

> If I just have text in one window and highlighted, the copy
> works fine and I can paste the selected text into another document. 
> However if text exists in both windows it gets confused and does not
> work properly.

It does not get confused.  Your ActionListener is executing the copy()
method on both text areas.  If you have a selection in both text areas,
the first copy() copies the selection to the clipboard and the second 
copy() overwrites it.


> public class Copy implements ActionListener, ItemListener
          :
>       // Add Copy menu item.
>       JMenuItem Copy = new JMenuItem("Copy");
> 
>       Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
> ActionEvent.CTRL_MASK));
> 
>       menu.add(Copy); 

Just a few comments on the coding style here.  First, it is a bad idea to
use the same identifier for a class name and a member name.  You are
adding a lot of unnecessary confusion here.  Second, using upper case for
some members and lower case for others just adds to that confusion.
Finally, you would not have this issue if you followed the standard Java
coding conventions.  Following the coding conventions improves the
readability of your code because other people can understand what things
mean more readily.

-- 
Regards,

John McGrath
0
Reply John 11/27/2004 9:08:45 AM


I did some reasearch and viewed some other posts on this issue and was
able to figure it out.  The solution I chose was to use the
DefaultEditorKit with an ActionTable. The new working/compilable code
is as follows:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.KeyStroke;
import java.awt.datatransfer.*;
import java.awt.dnd.*;


public class Copy implements ActionListener, ItemListener
{
   EventPanel EventDisplay;
   EventPanel EventDisplay2;
   HashMap actions;
 
   public Copy (){}

   //The following two methods allow us to find an
    //action provided by the editor kit by its name.
    private void createActionTable(JTextComponent textComponent) {
        actions = new HashMap();
        Action[] actionsArray = textComponent.getActions();
        for (int i = 0; i < actionsArray.length; i++) {
            Action a = actionsArray[i];
            actions.put(a.getValue(Action.NAME), a);
        }
    }

    private Action getActionByName(String name) {
        return (Action)(actions.get(name));
    }

   //*******************************
   // Creates items in the Menu Bar
   //*******************************
   //
   public JMenuBar createMenuBar()
   {
      createActionTable(EventDisplay.EventDisplay);
     
      //Create the menu bar.
      JMenuBar menuBar;
      menuBar = new JMenuBar();

      // Add the "Edit" menu title.
      JMenu menu = new JMenu("Edit");

      menu.add(getActionByName(DefaultEditorKit.copyAction));

      menuBar.add(menu);

      // Add Copy menu item.
 //     JMenuItem Copy = new JMenuItem("Copy");

 //     Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
ActionEvent.CTRL_MASK));

 //     menu.add(Copy); 
      
 //     menuBar.add(menu);

 //     Copy.addActionListener(new ActionListener() { 
//	public void actionPerformed(ActionEvent h) 
//	{ 
//         JTextArea textArea =
(JTextArea)EventDisplay.scrollPane.getViewport().getView();
//         textArea.copy();
	
//         JTextArea textArea2 =
(JTextArea)EventDisplay2.scrollPane.getViewport().getView();
//         textArea2.copy();
//	}
//	});
      
     
      return menuBar;
   }

   

   //*******************************************************
   // Generic function for setting the Grid Bag constraints 
   // when defining panel area and size.
   //*******************************************************
   //
   public void setGBconstraints(GridBagConstraints GBc, int gridX, int
gridY, int weightX, int weightY, int gwidth, int gheight)
   {
      GBc.gridx = gridX;
      GBc.gridy = gridY;
      GBc.weightx = weightX;
      GBc.weighty = weightY;
      GBc.gridwidth = gwidth;
      GBc.gridheight = gheight;
   }

   //****************************
   // Creates the Content Pane.
   //****************************
   //
   public Container createContentPane() 
   {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new GridBagLayout());
        JPanel filterPane = new JPanel(new GridBagLayout());
        JPanel eventsPane = new JPanel(new GridBagLayout());
        JPanel eventsPane2 = new JPanel(new GridBagLayout());

        GridBagConstraints GBconstraints = new GridBagConstraints();
        GBconstraints.insets = new Insets(0, 5, 0, 5);
        contentPane.setOpaque(true);

        Border line = BorderFactory.createLineBorder(Color.black);
        Font fnt = new Font("Times New Roman", Font.BOLD, 14);

        //
        // Add Event panel.
        //
        EventDisplay = new EventPanel();
        GBconstraints.fill = GridBagConstraints.BOTH;
        JScrollPane sp = EventDisplay.GetScrollPane();
        setGBconstraints(GBconstraints, 0, 0, 100, 10, 1, 3);
        eventsPane.add(sp, GBconstraints);

        TitledBorder Events = new
TitledBorder(line,"Events",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
        eventsPane.setBorder(Events);
        setGBconstraints(GBconstraints, 0, 0, 100, 100, 1, 3);
        contentPane.add(eventsPane, GBconstraints);

        //
        // Add Event2 panel.
        //
        EventDisplay2 = new EventPanel();
        GBconstraints.fill = GridBagConstraints.BOTH;
        JScrollPane sp2 = EventDisplay2.GetScrollPane();
        setGBconstraints(GBconstraints, 0, 1, 100, 10, 1, 3);
        eventsPane2.add(sp2, GBconstraints);

        TitledBorder Events2 = new
TitledBorder(line,"Events2",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
        eventsPane2.setBorder(Events2);
        setGBconstraints(GBconstraints, 0, 4, 100, 100, 1, 3);
        contentPane.add(eventsPane2, GBconstraints);

        return contentPane;
    }

   public void actionPerformed(ActionEvent e)
   {
   }

   public void itemStateChanged(ItemEvent e)
   {
   }

   //*****************************************************
    // Create the GUI and show it.  For thread safety,
    // this method should be invoked from the
    // event-dispatching thread.
    //*****************************************************
    //
    private static void createAndShowGUI()
    {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        //Create and set up the window.
        JFrame frame = new JFrame("Copy Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        Copy MainWin = new Copy();
        frame.setContentPane(MainWin.createContentPane());
        frame.setJMenuBar(MainWin.createMenuBar());
 
        //Display the window.
        frame.setSize(650, 660);
        frame.setVisible(true);
    }


   //*****************************************
   // Main function for the Copy GUI.
   //*****************************************
   //
   public static void main(final String[] args)
   {
      //Schedule a job for the event-dispatching thread:
      //creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() 
      {
         public void run()
         {
            createAndShowGUI();
         }
      });
     
   }

   public class EventPanel extends JPanel 
   {
      JTextArea EventDisplay;
      JScrollPane scrollPane;
   
      //*************************************
      // EventPanel Constructor
      //*************************************
      public EventPanel()
      {
         //Create a scrolled text area for event messages.
         //
         EventDisplay = new JTextArea(500, 100);
         EventDisplay.setEditable(true);
         EventDisplay.setLineWrap(false);
         scrollPane = new JScrollPane(EventDisplay);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

         // Set the Font for the JTextArea.
         //
         Font f = new Font("Monospaced", Font.BOLD, 14);
         EventDisplay.setFont(f);


         // JTextArea leaves no space between the edge and text that
it holds.
         // This can be fixed by adding an empty border to it.
         EventDisplay.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
      }

      //*****************************************
      // Returns scroll pane for the Even Panel
      //*****************************************
      public JScrollPane GetScrollPane()
      {
         return scrollPane;
      }
   }
}






eric.martin@omitron.com (Eric) wrote in message news:<203544bd.0411262045.2df963e3@posting.google.com>...
> Problem: I have a Swing GUI with two JTextAreas (Events and Events2)
> and an Edit menu with one option (Copy).  It's a prototype I wrote in
> order to try and figure out how to copy text from two different text
> areas.  My assumption was that Swing was smart enough to determine in
> what window the text was selected (highlighted) but that's not the
> case.  If I just have text in one window and highlighted, the copy
> works fine and I can paste the selected text into another document. 
> However if text exists in both windows it gets confused and does not
> work properly.
> 
> I've included a compilable version of the prototype at the end of this
> posting if anyone is inclined to assist me in solving this problem.
> 
> What I need to know is the function to invoke in order to determine if
> text is selected/highlighted in that JTextArea, and if it is, to copy
> from that window.  Such that the actionPerformed code that performs
> the copy text function would look something like this.
> 
> Copy.addActionListener(new ActionListener() { 
>    public void actionPerformed(ActionEvent h) 
>    {
>       if(The Event Display is HIGHLIGHTED)
>       { 
>          JTextArea textArea =
> (JTextArea)EventDisplay.scrollPane.getViewport().getView();
>          textArea.copy();
>       }
> 	
>       if(The Event Display2 is HIGHLIGHTED)
>       {
>          JTextArea textArea2 =
> (JTextArea)EventDisplay2.scrollPane.getViewport().getView();
>          textArea2.copy();
>       }
>    }
> });
> 
> 
> -------------------------------------------------------------
> Here is a compilable version of the prototype for this problem.
> Thanks in advance to anyone providing me feedback.
> --Eric
> -------------------------------------------------------------
> 
> import java.io.*;
> import java.awt.*;
> import java.awt.event.*;
> import java.util.*;
> import javax.swing.*;
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.JMenu;
> import javax.swing.JMenuBar;
> import javax.swing.JFrame;
> import javax.swing.border.*;
> import javax.swing.KeyStroke;
> import java.awt.datatransfer.*;
> import java.awt.dnd.*;
> 
> 
> public class Copy implements ActionListener, ItemListener
> {
>    EventPanel EventDisplay;
>    EventPanel EventDisplay2;
>  
>    public Copy (){}
> 
>    //*******************************
>    // Creates items in the Menu Bar
>    //*******************************
>    //
>    public JMenuBar createMenuBar()
>    {
>       //Create the menu bar.
>       JMenuBar menuBar;
>       menuBar = new JMenuBar();
> 
>       // Add the "Edit" menu title.
>       JMenu menu = new JMenu("Edit");
> 
>       // Add Copy menu item.
>       JMenuItem Copy = new JMenuItem("Copy");
> 
>       Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
> ActionEvent.CTRL_MASK));
> 
>       menu.add(Copy); 
>       
>       menuBar.add(menu);
> 
>       Copy.addActionListener(new ActionListener() { 
> 	public void actionPerformed(ActionEvent h) 
> 	{ 
>          JTextArea textArea =
> (JTextArea)EventDisplay.scrollPane.getViewport().getView();
>          textArea.copy();
> 	
>          JTextArea textArea2 =
> (JTextArea)EventDisplay2.scrollPane.getViewport().getView();
>          textArea2.copy();
> 	}
> 	});
> 
>       return menuBar;
>    }
> 
>    //*******************************************************
>    // Generic function for setting the Grid Bag constraints 
>    // when defining panel area and size.
>    //*******************************************************
>    //
>    public void setGBconstraints(GridBagConstraints GBc, int gridX, int
> gridY, int weightX, int weightY, int gwidth, int gheight)
>    {
>       GBc.gridx = gridX;
>       GBc.gridy = gridY;
>       GBc.weightx = weightX;
>       GBc.weighty = weightY;
>       GBc.gridwidth = gwidth;
>       GBc.gridheight = gheight;
>    }
> 
>    //****************************
>    // Creates the Content Pane.
>    //****************************
>    //
>    public Container createContentPane() 
>    {
>         //Create the content-pane-to-be.
>         JPanel contentPane = new JPanel(new GridBagLayout());
>         JPanel filterPane = new JPanel(new GridBagLayout());
>         JPanel eventsPane = new JPanel(new GridBagLayout());
>         JPanel eventsPane2 = new JPanel(new GridBagLayout());
> 
>         GridBagConstraints GBconstraints = new GridBagConstraints();
>         GBconstraints.insets = new Insets(0, 5, 0, 5);
>         contentPane.setOpaque(true);
> 
>         Border line = BorderFactory.createLineBorder(Color.black);
>         Font fnt = new Font("Times New Roman", Font.BOLD, 14);
> 
>         //
>         // Add Event panel.
>         //
>         EventDisplay = new EventPanel();
>         GBconstraints.fill = GridBagConstraints.BOTH;
>         JScrollPane sp = EventDisplay.GetScrollPane();
>         setGBconstraints(GBconstraints, 0, 0, 100, 10, 1, 3);
>         eventsPane.add(sp, GBconstraints);
> 
>         TitledBorder Events = new
> TitledBorder(line,"Events",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
>         eventsPane.setBorder(Events);
>         setGBconstraints(GBconstraints, 0, 0, 100, 100, 1, 3);
>         contentPane.add(eventsPane, GBconstraints);
> 
>         //
>         // Add Event2 panel.
>         //
>         EventDisplay2 = new EventPanel();
>         GBconstraints.fill = GridBagConstraints.BOTH;
>         JScrollPane sp2 = EventDisplay2.GetScrollPane();
>         setGBconstraints(GBconstraints, 0, 1, 100, 10, 1, 3);
>         eventsPane2.add(sp2, GBconstraints);
> 
>         TitledBorder Events2 = new
> TitledBorder(line,"Events2",TitledBorder.LEADING,TitledBorder.TOP,fnt,Color.red);
>         eventsPane2.setBorder(Events2);
>         setGBconstraints(GBconstraints, 0, 4, 100, 100, 1, 3);
>         contentPane.add(eventsPane2, GBconstraints);
> 
>         return contentPane;
>     }
> 
>    public void actionPerformed(ActionEvent e)
>    {
>    }
> 
>    public void itemStateChanged(ItemEvent e)
>    {
>    }
> 
>    //*****************************************************
>     // Create the GUI and show it.  For thread safety,
>     // this method should be invoked from the
>     // event-dispatching thread.
>     //*****************************************************
>     //
>     private static void createAndShowGUI()
>     {
>         //Make sure we have nice window decorations.
>         JFrame.setDefaultLookAndFeelDecorated(true);
>  
>         //Create and set up the window.
>         JFrame frame = new JFrame("Copy Window");
>         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>  
>         //Create and set up the content pane.
>         Copy MainWin = new Copy();
>         frame.setContentPane(MainWin.createContentPane());
>         frame.setJMenuBar(MainWin.createMenuBar());
>  
>         //Display the window.
>         frame.setSize(650, 660);
>         frame.setVisible(true);
>     }
> 
> 
>    //*****************************************
>    // Main function for the Copy GUI.
>    //*****************************************
>    //
>    public static void main(final String[] args)
>    {
>       //Schedule a job for the event-dispatching thread:
>       //creating and showing this application's GUI.
>       javax.swing.SwingUtilities.invokeLater(new Runnable() 
>       {
>          public void run()
>          {
>             createAndShowGUI();
>          }
>       });
>      
>    }
> 
>    public class EventPanel extends JPanel 
>    {
>       JTextArea EventDisplay;
>       JScrollPane scrollPane;
>    
>       //*************************************
>       // EventPanel Constructor
>       //*************************************
>       public EventPanel()
>       {
>          //Create a scrolled text area for event messages.
>          //
>          EventDisplay = new JTextArea(500, 100);
>          EventDisplay.setEditable(true);
>          EventDisplay.setLineWrap(false);
>          scrollPane = new JScrollPane(EventDisplay);
>          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
> 
>          // Set the Font for the JTextArea.
>          //
>          Font f = new Font("Monospaced", Font.BOLD, 14);
>          EventDisplay.setFont(f);
> 
> 
>          // JTextArea leaves no space between the edge and text that
> it holds.
>          // This can be fixed by adding an empty border to it.
>          EventDisplay.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
>       }
> 
>       //*****************************************
>       // Returns scroll pane for the Even Panel
>       //*****************************************
>       public JScrollPane GetScrollPane()
>       {
>          return scrollPane;
>       }
>    }
> }
0
Reply eric 11/27/2004 12:55:07 PM

2 Replies
260 Views

(page loaded in 0.125 seconds)

Similiar Articles:













7/22/2012 3:18:08 PM


Reply: