0

I've created a JFrame (with automatically generated code) that has a 'free design' layout. In the JFrame I have a background panel with a 'null' layout. The background panel contains another panel called cardLayoutPanel which has a 'cardlayout' design. In the card layout panel I have a jScrollPane that has a panel called firstNoteView which has a 'free design' layout. In the firstNoteView theres another panel called NewNotePanel and it has a 'null' layout.

When I run the file to run the program the jScrollPane (in the card layout panel) works, but when I add my own panels (using my own code) to the firstNoteView - in the jScrollPane, the jScrollPane doesn't work.

Heres the code:

package interfaces;

import java.awt.Dimension;
import javax.swing.JPanel;

public class ScrollablePanel extends javax.swing.JFrame
{

 Notes notes = new Notes ();

int numberOfNotes = 9;
int currentNoteNumber = 0;


//co-ordinates for first text area + used to calculate the rest of the      text area locations 
int x = 40;
int y = 100;

// panel sizes
int length = 250;
int breadth = 220;

int xgap = 310;
int ygap = 310;


// method to declare text areas for notes
public void createPanels () {

Dimension preferredSize = jScrollPane1.getPreferredSize();
JPanel[] panel = new JPanel [numberOfNotes];

System.out.println(numberOfNotes);

for (int construct = 0; construct < numberOfNotes; construct++)
    panel [construct] = new JPanel ();


for (int setLocation = 1, xCordinate = 1, pHeight = 1; setLocation < numberOfNotes; setLocation++, xCordinate++) {

            if ((setLocation < 4) && (setLocation != 0))
                panel[setLocation].setBounds(x + (xgap * (setLocation)), y, length, breadth);

            if ((setLocation % 4 == 0) && (setLocation != 0)) {
                y = y + ygap;

                x = 40;
                xCordinate = 0;

                firstNoteView.setBounds(200, 50, 1250, 850+(ygap*pHeight));

                pHeight++;

            }


            if (setLocation >= 4)

                panel[setLocation].setBounds(x + (xgap * (xCordinate)), y, length, breadth);

        }


        for (int add = 0; add < numberOfNotes; add++) 
                firstNoteView.add(panel[add]);

        firstNoteView.repaint();


    }

// method to hide neccessary components when for when the program initialises
public void setScreen ()
{
// setting components to be hidden, alphabetical order

firstNoteView.setVisible(true);

// remove all panels in the cardlayout
cardLayoutPanel.removeAll();

// add the firstNoteView Panel to set the default home screen
cardLayoutPanel.add(firstNoteView);
cardLayoutPanel.repaint();

}


public ScrollablePanel()
{
initComponents();

setScreen ();
setNumberOfNotes () ;
createPanels ();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents()
{

BackgroundPanel = new javax.swing.JPanel();
cardLayoutPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
firstNoteView = new javax.swing.JPanel();
NewNotePanel = new javax.swing.JPanel();
lblNewNote = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBounds(new java.awt.Rectangle(0, 23, 1440, 900));
setMinimumSize(new java.awt.Dimension(1440, 900));
setPreferredSize(new java.awt.Dimension(1440, 900));

BackgroundPanel.setBackground(new java.awt.Color(255, 255, 255));
BackgroundPanel.setLayout(null);

cardLayoutPanel.setLayout(new java.awt.CardLayout());

jScrollPane1.setBounds(new java.awt.Rectangle(0, 0, 1090, 0));

firstNoteView.setBackground(new java.awt.Color(0, 153, 204));

NewNotePanel.setLayout(null);

lblNewNote.setText("<html><center>+<br>New Note...</center></html>");
NewNotePanel.add(lblNewNote);
lblNewNote.setBounds(90, 90, 73, 32);

javax.swing.GroupLayout firstNoteViewLayout = new javax.swing.GroupLayout(firstNoteView);
firstNoteView.setLayout(firstNoteViewLayout);
firstNoteViewLayout.setHorizontalGroup(
    firstNoteViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(firstNoteViewLayout.createSequentialGroup()
        .addGap(40, 40, 40)
        .addComponent(NewNotePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(951, Short.MAX_VALUE))
);
firstNoteViewLayout.setVerticalGroup(
    firstNoteViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(firstNoteViewLayout.createSequentialGroup()
        .addGap(100, 100, 100)
        .addComponent(NewNotePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(792, Short.MAX_VALUE))
);

jScrollPane1.setViewportView(firstNoteView);

cardLayoutPanel.add(jScrollPane1, "card2");

BackgroundPanel.add(cardLayoutPanel);
cardLayoutPanel.setBounds(200, 60, 1240, 840);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(BackgroundPanel, 

javax.swing.GroupLayout.DEFAULT_SIZE, 1440, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(BackgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );

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


    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(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {


    java.util.logging.Logger.getLogger(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new ScrollablePanel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel BackgroundPanel;
    private javax.swing.JPanel NewNotePanel;
    private javax.swing.JPanel cardLayoutPanel;
    private javax.swing.JPanel firstNoteView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNewNote;
    // End of variables declaration                   
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yeezus
  • 397
  • 1
  • 3
  • 14
  • `NewNotePanel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. 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). Also, scroll panes do not work with null layouts. – Andrew Thompson Jul 07 '17 at 11:45
  • Alright :). I'll work with layout managers. Thanks. – Yeezus Jul 07 '17 at 12:08

0 Answers0