0

I have two files, one is Main.java and the second is frame.java.

I'm creating a desktop application so I want to add scrollpane as needed vertically or horizontally in Main.java file.

Frame.java throws the JPanel object which is being catched by Main.java and dynamically loaded into JFrame.

So anyone please tell me, how can I add the scrollpane or scrollbar. Which is best, I don't know. Thank you..

Main.java:

package pack;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JPanel pn = null;
        JFrame mainFrame = null;

        frame login = new frame();
        mainFrame = new JFrame("Prem");
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500,500);
        mainFrame.setLocationRelativeTo(null);
        pn=login.getLogin();
        mainFrame.add(pn,BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }

    public Main() {
        super();
    }
}

This is second file which throws the panel object from method frame.java

package pack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.AbstractAction;
import java.awt.*;
import java.awt.event.*;
public class frame {

    JPanel pane = null,pane1=null;
    JTextField userText=null,passText=null;
    JLabel userLabel =null,passLabel=null,errorLabel=null;
    JButton submitLogin = null;
    public frame()
    {
        pane = new JPanel();
        pane.setLayout(null);
    }

    public JPanel getLogin()
    {
        userLabel = new JLabel("UserName");
        pane.add(userLabel);
        userLabel.setBounds(5,10,100, 30);

        userText = new JTextField();
        pane.add(userText);
        userText.setBounds(110,10,120,30);

        passLabel = new JLabel("PassWord");
        pane.add(passLabel);
        passLabel.setBounds(5,60,100, 30);

        passText = new JTextField();
        pane.add(passText);
        passText.setBounds(110,60,120,30);

        errorLabel = new JLabel("");
        pane.add(errorLabel);
        errorLabel.setBounds(5,150,180,30);

        submitLogin = new JButton("Submit");
        pane.add(submitLogin);
        submitLogin.setBounds(80,110,90,30);
        submitLogin.addActionListener(new AbstractAction(){
            public void actionPerformed(ActionEvent e)
            {
                if(submitLogin.getActionCommand() == "Submit")
                {
                    if(userText.getText().isEmpty() || passText.getText().isEmpty())
                    {   
                        errorLabel.setText("Enter UserName And Password");
                    }
                    else
                    {
                        //connection
                    }
                }
                else
                {
                    System.exit(0);
                }
            }
        });
        return pane;
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0

You have several issues with that code including:

  • You don't show us in your code where you're trying to use a JScrollPane or even where it's needed. If you show us your attempt to use this, we'll get a much better understanding of your problem.
  • You are using a null layout and setBounds(...), something you should avoid at almost all costs, and something which absolutely must be avoided if you want to use a JScrollPane, since JScrollPane's do not work well with null layouts. Instead read up on and use layout managers.
  • You're comparing Strings using the == operator. You don't want to compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.

  • You can find links to the Swing tutorials and other Swing resources here: Swing Info
  • You can find the layout manager tutorial here: Layout Manager Tutorial.
  • You can learn about "nesting" layouts here.
  • You can find specific information on how to use JScrollPanes here: JScrollPane Tutorial.
  • The basic use of them is that you will want to add your scrollable component to the JScrollPane's viewport, and then add the JScrollPane to the GUI. The specifics of how to do this will all depend on your needs, something we don't yet know, but again is very well explained in the tutorials that I've linked to above.
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373