Welcome to ICSE Java Answers. Ask any questions you have related to Java and we will answer them.
Java is a registered trademark of Oracle. This site is in no way related to or endorsed by Oracle.

Set Size

0 votes

HOW TO I SET THE SIZE OF LISTF

 

/*
 * String file="resource//error.txt";
 * FileWriter fileWriter = new FileWriter(file,true);
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.awt.event.*;
public class LISTING extends JFrame implements ActionListener 
{
    JPanel[] row = new JPanel[2];
    JButton[] button = new JButton[1];
    String[] buttonString = {"RELOAD"};
    int[] dimW = {300};
    int[] dimH = {40};
    JTextArea list=new JTextArea();
    static String POST="";
    Dimension Dimension=new Dimension(dimW[0],dimH[0]);
    Font font = new Font("Times new Roman", Font.BOLD, 18);
    LISTING() 
    {
        super("Voting | FEMIN");
        setDesign();
        setSize(500, 500);
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        GridLayout grid = new GridLayout(4,1);
        setLayout(grid);
        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1);
        for(int i = 0; i < row.length; i++)
            row[i] = new JPanel();
        row[0].setLayout(f1);
        for(int i = 1; i < row.length; i++)
            row[i].setLayout(f2);
        
        for(int i = 0; i < button.length; i++) {
            button[i] = new JButton();
            button[i].setText(buttonString[i]);
            button[i].setFont(font);
            button[i].addActionListener(this);
            button[i].setPreferredSize(Dimension);
        }
        
        list.setFont(font);
        list.setOpaque(false);
        //list.setForeground(Color.BLACK);
        
        JScrollPane LISTF = new JScrollPane(list);
        add(LISTF);
        
        row[1].add(button[0]);
        add(row[1]);
        setVisible(true);
        
        getLIST();
    }
    
    public final void setDesign() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {   
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent ae) 
    {
        try{
            list.setText("");
            getLIST();
        }   
        catch(Exception e)
        {
            
        }
    }
    public static void main(String post) 
    {
        POST=post;
        LISTING c = new LISTING();
    }
    
    public  void getLIST()
    {
        try
        {
            String file="Resources/sv.txt";
            Scanner ob=new Scanner(new File(file));
            while(ob.hasNext())
            {
                list.append(ob.nextLine()+"\n");
                ob.nextLine();
                ob.nextLine();
            }
        }
        catch(Exception e){}
    }
}

 

I tried LISTF.setSize(500,500); but its size dint change.

Try running the output of the above code!

asked by Femin Dharamshi (312 points)
edited by Ranjith

1 Answer

+1 vote
 
Best answer

If you want fine grained control on the size and position of components, you can use null layout. (No LayoutManager). For example, if you have a JPanel p1 in which you want to place a Jlabel label of size (200,10) at (50,100), then 

p1.setLayout(null);
label.setPosition(50,100);
label.setSize(200,10);
p1.add(label);

In the code you have, you can create a new JPanel with LayoutManager null and add LISTF to it.

In your code, replace add(LISTF) on line 53 ( add(LISTF)) with

LISTF.setSize(50, 20);
JPanel p = new JPanel(null);
p.add(LISTF);
add(p);

 

answered by Ranjith (2,048 points)
selected by Femin Dharamshi
...