So I was trying to add elements into my list. But for some reason the words inside the list are very very small.... is there anyway to increase the font? I tried setFont, doesn't work. :(

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentListener;




public class sub extends JFrame implements ActionListener{
	private JList list; 
	private JButton addButton = new JButton("Add"); 
	private JButton delButton = new JButton("Delete");
	private JTextField item = new JTextField(10);
	private String [] s = new String[20];
	
public sub(){
	
      list = new JList(s);
      list.setLayout(new BorderLayout());
      list.setFont(new Font("Courier New", Font.ITALIC, 12));
      
      JScrollPane listScrollPane = new JScrollPane(list);
      
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      list.setSelectedIndex(0);
      list.setVisibleRowCount(5);
      
      addButton.setEnabled(true);
      addButton.addActionListener(this);
      delButton.setEnabled(false);
      delButton.addActionListener(this);
      
      item.addActionListener(this);
      
      
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
      panel.add(delButton);
      panel.add(Box.createHorizontalStrut(5));
      panel.add(new JSeparator(SwingConstants.VERTICAL));
      panel.add(Box.createHorizontalStrut(5));
      panel.add(item);
      panel.add(addButton);
      panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));   
     
      add(listScrollPane, BorderLayout.CENTER);
      add(panel, BorderLayout.PAGE_END);
      
}



public static void main (String[] args){
	sub sub = new sub();
	sub.setVisible(true);
	sub.setDefaultCloseOperation(EXIT_ON_CLOSE);
	sub.setTitle("Bank Account");
	sub.setSize( 250,250 );
	sub.setLocation(600, 400);
}



public void actionPerformed(ActionEvent e) {
	if(e.getSource() == addButton){
		System.out.println("ADD");
		int index = list.getSelectedIndex();
        if (index == -1) { //no selection, so insert at beginning
            index = 0;
        } else {           //add after the selected item
            index++;
        }
        
        s[index] = item.getText();

        item.requestFocusInWindow();
        item.setText("");
        item.setFont(new Font("Courier New", Font.ITALIC, 12));
        list.setSelectedIndex(index);
        list.ensureIndexIsVisible(index);
	}
	
	else if(e.getSource() == delButton){
		System.out.println("DEL");
	}
	
}



}

Passing the empty string array to the constructor of your JList is causing the weird font sizing issue.

If you want to add and remove items from your list, use a DefaulListModel instead of an array.

Edited 5 Years Ago by Ezzaral: n/a

The reason why i used array is because i wanted to use FileIO to save and load the array elements into a txtfile so i can retrieve the info the next time i run the prog.

You can use any means you wish to load and save the elements with a DefaultListModel. You aren't limited to an array for that.

Edited 5 Years Ago by Ezzaral: n/a

DefaultListModel implements Serializable, so you can read/write one with a single Object I/O call, or use XMLEncoder/Decoder to write & read the whole contents as XML string data, also in a single call.

This article has been dead for over six months. Start a new discussion instead.