0

hello goodevening to all i have a problem on my program with the ScrollPane in my JList i cant put an JScrollPane in my list because i am using a panel instead of Container this is my code so far its all runnable the problem is if you enter a high number in the number of times the some output will not be able to see because of the size of my list . so this is the code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MultCen extends JFrame implements ActionListener
{
    public static void main(String args [])
    {
        MultCen e = new MultCen();
        e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        e.setVisible(true);
        e.setSize(300,450);
    }

    JTextField t1 = new JTextField();
    JTextField t2 = new JTextField();
    JButton b = new JButton("Okay");
    JButton c = new JButton("Clear");
    JList list = new JList();
    JLabel lab = new JLabel();
    DefaultListModel m = new DefaultListModel();

    public MultCen()
    {
        JPanel panel = new JPanel();
        panel.setLayout(null);



        JLabel l = new JLabel("Enter a number :");
        JLabel l1 = new JLabel("How many times :");



        l.setBounds(10,10,130,30);
        l1.setBounds(10,40,130,30);
        t1.setBounds(140,10,130,25);
        t2.setBounds(140,40,130,25);
        b.setBounds(60,90,75,30);
        c.setBounds(150,90,75,30);
        list.setBounds(30,140,220,220);

        panel.add(t1);
        panel.add(t2);
        panel.add(l);
        panel.add(l1);
        panel.add(list);
        panel.add(b);
        panel.add(c);

        getContentPane().add(panel);


        b.addActionListener(this);
        c.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b)
        {
            int t3 = Integer.parseInt(t1.getText());
            int t4 = Integer.parseInt(t2.getText());
            m.addElement("The multiplication Table of "+t3);
            for (int cc =1 ; cc <=t4; cc++ )
            {

                lab.setText(t3+"*"+cc+" = "+(t3*cc));
                m.addElement(lab.getText());
                list.setModel(m);


            }



        }

        if(e.getSource() == c)
        {
            t1.setText("");
            t2.setText("");
            m.removeAllElements();
        }
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Batusai
  • 149
  • 1
  • 1
  • 12

2 Answers2

2

JScrollPane does not work with null Layout. Use BoxLayout or any other resizeable layout instead. This is the limitation of setLayout(null).

Masudul
  • 21,823
  • 5
  • 43
  • 58
1

Use Layout managers. You've asked a lot of questions here and I'm sure a few you have been advised not to use null layout. Again here is the tutorial Laying out components Within a container. Learn to use them so you don't run into the million possible problems on the road ahead. This kind of problem being one of them.

here's an example of how you could achieve the same thing with layout managers, and some empty borders for white space.

With Layout Manager

enter image description here

Without Layout Manger

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MultCen extends JFrame {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MultCen e = new MultCen();
                e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                e.setVisible(true);
                e.pack();
            }
        });

    }

    JTextField t1 = new JTextField(10);
    JTextField t2 = new JTextField(10);
    JButton b = new JButton("Okay");
    JButton c = new JButton("Clear");
    JLabel lab = new JLabel();
    DefaultListModel m = new DefaultListModel();

    public MultCen() {

        JPanel topPanel = new JPanel(new GridLayout(2, 2, 0, 5));
        JLabel l = new JLabel("Enter a number :");
        JLabel l1 = new JLabel("How many times :");
        topPanel.add(l);
        topPanel.add(t1);
        topPanel.add(l1);
        topPanel.add(t2);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(b);
        buttonPanel.add(c);
        buttonPanel.setBorder(new EmptyBorder(10, 0, 10, 0));

        JList list = new JList();
        JScrollPane scroll = new JScrollPane(list);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(300, 300));

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.CENTER);
        panel.add(scroll, BorderLayout.SOUTH);
        panel.setBorder(new EmptyBorder(10, 15, 10, 15));

        getContentPane().add(panel);

    }
}

Side Notes

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720