Results 1 to 6 of 6
  1. #1
    dhany is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Question myTextArea.getText() int the multiple Tab (JTabbedPane)

    i created frame with empty tab, new tab created when i click "new tab" button, also TextArea created within new created tab. this is my snipped code
    Java Code:
    private void AddTabButtonActionPerformed(java.awt.event.ActionEvent evt) {
        MyTextArea = new JTextArea();
        int TabCount = MyTabbedPane.getTabCount();
        MyTabbedPane.addTab("Tab "+String.valueOf(TabCount), MyTextArea);
        MyTextArea.setText("This is Tab No. "+String.valueOf(TabCount));
        MyTabbedPane.setSelectedIndex(MyTabbedPane.getTabCount()-1);
    }
    i use JOptionPane to test if data within every TextArea can be found. this is my show dialog.

    Java Code:
    private void ShowTextButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(null,MyTextArea.getText());
    }
    the froblem is i can only get my last TextArea, can anyone show me the way to get data from previously created TextArea?;

    this is my main app
    Java Code:
    package tabtest;
    
    
    public class TabTest {
    
    
        public static void main(String[] args) {
            MainFrame mainframe = new MainFrame();
            mainframe.setVisible(true);
        }
    }
    and this is my MainFrame class
    Java Code:
    package tabtest;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    
    
    public class MainFrame extends javax.swing.JFrame {
    
    
        public MainFrame() {
            initComponents();
        }
    
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            MyTabbedPane = new javax.swing.JTabbedPane();
            AddTabButton = new javax.swing.JButton();
            ShowTextButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            AddTabButton.setText("Add Tab");
            AddTabButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddTabButtonActionPerformed(evt);
                }
            });
    
            ShowTextButton.setText("Show Text");
            ShowTextButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ShowTextButtonActionPerformed(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()
                    .addComponent(AddTabButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ShowTextButton)
                    .addGap(248, 248, 248))
                .addComponent(MyTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(MyTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(AddTabButton)
                        .addComponent(ShowTextButton)))
            );
    
            pack();
        }// </editor-fold>
    JTextArea MyTextArea;
    private void AddTabButtonActionPerformed(java.awt.event.ActionEvent evt) {
        MyTextArea = new JTextArea();
        int TabCount = MyTabbedPane.getTabCount();
        MyTabbedPane.addTab("Tab "+String.valueOf(TabCount), MyTextArea);
        MyTextArea.setText("This is Tab No. "+String.valueOf(TabCount));
        MyTabbedPane.setSelectedIndex(MyTabbedPane.getTabCount()-1);
    }
    
    private void ShowTextButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(null,MyTextArea.getText());
    }
    
        // Variables declaration - do not modify
        private javax.swing.JButton AddTabButton;
        private javax.swing.JTabbedPane MyTabbedPane;
        private javax.swing.JButton ShowTextButton;
        // End of variables declaration
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: myTextArea.getText() int the multiple Tab (JTabbedPane)

    First of all use proper Java variable naming conventions. Variable names DO NOT start with an upper case character.

    You can only get the last text area created because the "myTextArea" variable points to the last tab created.

    Instead you need to use tabbedPane.getSelectedIndex. Once you know the selected index you can get the component at that index using tabbedPane.getComponentAt( index ). You cast the component to a JTextArea and then you can get the text.

  3. #3
    dhany is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default Re: myTextArea.getText() int the multiple Tab (JTabbedPane)

    thanks for ur advise senior..., i didnt get any warning for naming conventions by compiler, but thanks i get it from you, i'll do your suggestion, i tried by myself about getComponentAt() and getSelectedIndex(), i get selected tab index with getSelectedIndex(), but how to get data from selected component ( JTextArea this time) , i want its return any String data
    thanks again if can show me the codes for this function :
    Java Code:
    private void ShowTextButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(null,MyTextArea.getText());
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: myTextArea.getText() int the multiple Tab (JTabbedPane)

    Method names don't start with an uppercase letter either.

    Recommended reading: Code Conventions for the Java(TM) Programming Language: Contents

    db

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: myTextArea.getText() int the multiple Tab (JTabbedPane)

    Quote Originally Posted by dhany
    i dont care about coding conventioning, the realy matter for me is if the codes run and compiled well, i use my own coding style just to recognize if it created by toolkit or by myself.
    Your written code will always be different than code generate by a toolkit even if you follow the standard naming conventions for variables and classes.

    Coding conventions exist for a reason. It enables anybody to read and understand your code. If you aren't willing to do this, then I for one am not willing to read your questions in the future because I am not willing to take the extra time necessary to read and understand your code.

  6. #6
    dhany is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default Re: myTextArea.getText() int the multiple Tab (JTabbedPane)

    thanks for your advise

Similar Threads

  1. getText() and set
    By felito in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 01:18 AM
  2. Help with getText()
    By geekchick in forum New To Java
    Replies: 17
    Last Post: 08-06-2010, 05:36 PM
  3. print multiple panel from JTabbedPane
    By koddy in forum New To Java
    Replies: 5
    Last Post: 06-14-2010, 08:45 AM
  4. getText(); problem
    By The_Sponzy_Paradox in forum Java Applets
    Replies: 5
    Last Post: 04-22-2010, 03:07 AM
  5. how to getText() properly?
    By javamula in forum New To Java
    Replies: 12
    Last Post: 09-16-2009, 05:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •