0

I want to add Scrollpane with Textarea to Tabbedpane.I Wrote code for that.But,It shows a small textarea not the whole frame.I write this code in createAction() Method.I want tabbedpane with close button and add Scrollpane to tabbedpane.Please check it once.Thank You.

My Code:

public class ClosableTabbedpane extends javax.swing.JFrame {
JTextArea tx;
int i=0;
public ClosableTabbedpane() {

    initComponents();

}

private static JPanel getTitlePanel(final JTabbedPane tabbedPane, final JPanel panel, String title) {
    JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    titlePanel.setOpaque(false);
    JLabel titleLbl = new JLabel(title);
    titleLbl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    titlePanel.add(titleLbl);
    JButton closeButton = new JButton("x");

    closeButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            tabbedPane.remove(panel);
        }
    });
    titlePanel.add(closeButton);
    return titlePanel;
}

@SuppressWarnings("unchecked")                        
private void initComponents() {

    tabbedPane = new javax.swing.JTabbedPane();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    create = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");

    create.setText("Create");
    create.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            createActionPerformed(evt);
        }
    });
    jMenu1.add(create);

    jMenuBar1.add(jMenu1);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
    );

    pack();
}                     

private void createActionPerformed(java.awt.event.ActionEvent evt) {                                       
    i++;
    tx = new JTextArea();
    JPanel panel = new JPanel();
    panel.setBounds(400,400,400,400);
    tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
    JScrollPane  scrollpane=new JScrollPane(tx);
    panel.add(scrollpane);
    panel.setOpaque(false);
    tabbedPane.add(panel);
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Doc"+i));  
}                                      

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ClosableTabbedpane().setVisible(true);
        }
    });
}              
private javax.swing.JMenuItem create;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tabbedPane;               
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3912886
  • 53
  • 2
  • 9
  • `panel.setBounds(400,400,400,400);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 20 '14 at 06:53
  • When I remove the setBounds() method,It shows the same output.Please modify the code with your modifications. – user3912886 Nov 20 '14 at 07:34
  • Please Anybody provide the solution for above Application. – user3912886 Nov 20 '14 at 08:41

1 Answers1

1

To suggest a size for the text area, specify the number of required rows and columns when created. E.G.

tx = new JTextArea(3,40);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • When I run The Application The frame is small in size,then maximize my frame,The textarea size is not expand.Fixed with the given rows and columns. – user3912886 Nov 20 '14 at 08:55
  • `The frame is small in size` It is necessary to `pack()` the frame. – Andrew Thompson Nov 20 '14 at 09:03
  • I Wrote tx=new JTextArea(170,170); The Texarea is added perfectly,But the Scrollpane is not applied.When I cross the end of Textarea line/Document The Scrollpane is not working/showing.Please Check it once. – user3912886 Nov 20 '14 at 09:04