JlayeredPane in netbeans

Discussion in 'Java' started by mukesh tiwari, Apr 1, 2011.

  1. Hello all, I am trying to put two components Jeditorpane and Jtextarea
    in Jlayeredpane. I am using Netbeans. I added jeditorpane and
    jtextarea in jlayeredpane and two buttons. When i click on button1
    then it should show message "Hello world doing nice".

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
    // TODO add your handling code here:
    jTextArea1.setText("");
    jTextArea1.setOpaque(true);
    jLayeredPane1.moveToFront(jEditorPane1);
    jEditorPane1.setText("Hello world doing nice");
    }
    and when click on button2 then it should show message "Hello world not
    doing good".

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {
    // TODO add your handling code here:
    jEditorPane1.setText("");
    jEditorPane1.setOpaque(true);
    jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
    }
    But when i click on button1 then it shows message "Hello world doing
    nice" but when i click on button2 then it does not show message "Hello
    world not doing good" as it should move the component to front. Could
    some one please tell me how to solve this. Here is source code which
    is partially generated by netbeans and partially written by me. Thank
    you.

    public class test extends javax.swing.JFrame {

    /** Creates new form test */
    public test() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLayeredPane1 = new javax.swing.JLayeredPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jEditorPane1 = new javax.swing.JEditorPane();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLayeredPane1.add(jTextArea1);// i added this component using
    custom code property
    jLayeredPane1.add(jEditorPane1); //i added this component using
    custom code property

    jEditorPane1.setText("");//i added this component using custom
    code property
    jScrollPane1.setViewportView(jEditorPane1);

    jScrollPane1.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane1,
    javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setText("");//i added this component using custom code
    property
    jScrollPane2.setViewportView(jTextArea1);

    jScrollPane2.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane2,
    javax.swing.JLayeredPane.DEFAULT_LAYER);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new
    javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jLayeredPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 505,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jButton1)
    .addGap(143, 143, 143)
    .addComponent(jButton2)))
    .addContainerGap(68, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(42, 42, 42)
    .addComponent(jLayeredPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 228,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(33, 33, 33)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton1)
    .addComponent(jButton2))
    .addContainerGap(43, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jTextArea1.setText("");
    jTextArea1.setOpaque(true);
    jLayeredPane1.moveToFront(jEditorPane1);
    jEditorPane1.setText("Hello world doing nice");
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jEditorPane1.setText("");
    jEditorPane1.setOpaque(true);
    jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new test().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration
    }
    mukesh tiwari, Apr 1, 2011
    #1
    1. Advertising

  2. mukesh tiwari

    markspace Guest

    On 4/1/2011 12:23 PM, mukesh tiwari wrote:
    > Hello all, I am trying to put two components Jeditorpane and Jtextarea
    > in Jlayeredpane. I am using Netbeans. I added jeditorpane and
    > jtextarea in jlayeredpane and two buttons. When i click on button1
    > then it should show message "Hello world doing nice".
    >
    > private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    > {
    > // TODO add your handling code here:
    > jTextArea1.setText("");
    > jTextArea1.setOpaque(true);
    > jLayeredPane1.moveToFront(jEditorPane1);
    > jEditorPane1.setText("Hello world doing nice");
    > }
    > and when click on button2 then it should show message "Hello world not
    > doing good".
    >
    > private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    > {
    > // TODO add your handling code here:
    > jEditorPane1.setText("");
    > jEditorPane1.setOpaque(true);
    > jLayeredPane1.moveToFront(jTextArea1);
    > jTextArea1.setText("Hello world not doing good");
    > }



    I simplified these methods to remove unnecessary calls. The result is
    better and the text now displays. However the formatting is still a bit
    odd, it looks like some of the text is offset slightly from the origin.
    I'm not sure why that is.

    Here are the improved event handlers.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    // jTextArea1.setText("");
    // jTextArea1.setOpaque(true);
    // jLayeredPane1.moveToFront(jEditorPane1);
    jEditorPane1.setText("Hello world doing nice");
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    // jEditorPane1.setText("");
    // jEditorPane1.setOpaque(true);
    // jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
    }
    markspace, Apr 1, 2011
    #2
    1. Advertising

  3. On Apr 2, 3:03 am, markspace <-@.> wrote:
    > On 4/1/2011 12:23 PM, mukesh tiwari wrote:
    >
    >
    >
    >
    >
    > > Hello all, I am trying to put two components Jeditorpane and Jtextarea
    > > in Jlayeredpane. I am using Netbeans. I added jeditorpane and
    > > jtextarea in jlayeredpane and two buttons. When i click on button1
    > > then it should show message "Hello world doing nice".

    >
    > >   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    > > {
    > >      // TODO add your handling code here:
    > >      jTextArea1.setText("");
    > >      jTextArea1.setOpaque(true);
    > >      jLayeredPane1.moveToFront(jEditorPane1);
    > >      jEditorPane1.setText("Hello world doing nice");
    > > }
    > > and when click on button2 then it should show message "Hello world not
    > > doing good".

    >
    > >   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    > > {
    > >      // TODO add your handling code here:
    > >      jEditorPane1.setText("");
    > >      jEditorPane1.setOpaque(true);
    > >      jLayeredPane1.moveToFront(jTextArea1);
    > >      jTextArea1.setText("Hello world not doing good");
    > > }

    >
    > I simplified these methods to remove unnecessary calls.  The result is
    > better and the text now displays.  However the formatting is still a bit
    > odd, it looks like some of the text is offset slightly from the origin.
    >   I'm not sure why that is.
    >
    > Here are the improved event handlers.
    >
    > private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    >      // TODO add your handling code here:
    > //    jTextArea1.setText("");
    > //    jTextArea1.setOpaque(true);
    > //    jLayeredPane1.moveToFront(jEditorPane1);
    >      jEditorPane1.setText("Hello world doing nice");
    >
    > }
    >
    > private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    >      // TODO add your handling code here:
    > //    jEditorPane1.setText("");
    > //    jEditorPane1.setOpaque(true);
    > //    jLayeredPane1.moveToFront(jTextArea1);
    >      jTextArea1.setText("Hello world not doing good");
    >
    >
    >
    > }


    Hi markspace
    Thank you for your reply but this is not fulfilling the purpose
    because texts are deformed in second case. I don't know if i
    understand the JlayeredPane fully but what in my mind is , i added
    two components [ jeditorpane and jtextarea ] in jLayeredpane in same
    Default_Layer and use the function moveTofront and moveToback to move
    these components in same layer.When jeditorpane will be in front
    position then it will hide all the contents of jtextarea and vice-
    versa. Its like dynamically changing the components position in layer
    but i don't think this is happening. Thanks again for your valuable
    insights.
    Regards
    Mukesh Tiwari
    mukesh tiwari, Apr 2, 2011
    #3
  4. In article
    <>,
    mukesh tiwari <> wrote:

    > Thank you for your reply, but this is not fulfilling the purpose
    > because texts are deformed in second case. I don't know if I
    > understand the JLayeredPane fully, but what in my mind is [this]: I
    > added two components [ jeditorpane and jtextarea ] in jLayeredpane in
    > same Default_Layer and use the function moveTofront and moveToback to
    > move these components in same layer. When jeditorpane will be in
    > front position then it will hide all the contents of jtextarea and
    > vice- versa. It's like dynamically changing the components position
    > in layer, but I don't think this is happening.


    LayeredPane is intended to show overlapping components simultaneously,
    as might be seen in JDesktopPane:

    <http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html>

    To show "different components at different times," CardLayout might be
    a better choice:

    <http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html#card>

    As an aside, I found it helpful to explore individual components using
    manual layouts before advancing to multiple, nested components in a GUI
    editor:

    <http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html>
    <http://download.oracle.com/javase/tutorial/uiswing/components/index.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Apr 2, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wes Batson

    background images w/ JLayeredPane

    Wes Batson, Sep 30, 2003, in forum: Java
    Replies:
    0
    Views:
    1,277
    Wes Batson
    Sep 30, 2003
  2. Pimousse
    Replies:
    2
    Views:
    3,619
    Roedy Green
    Apr 21, 2004
  3. Ilias Lazaridis
    Replies:
    0
    Views:
    895
    Ilias Lazaridis
    Dec 12, 2004
  4. alex_us01
    Replies:
    3
    Views:
    5,650
    Roedy Green
    Sep 30, 2005
  5. Thomas G. Marshall
    Replies:
    2
    Views:
    814
    Thomas G. Marshall
    Oct 16, 2006
Loading...

Share This Page