Reputation: 193
I have a JList
inside of a JScrollPane
and have set the font to Tahoma
, plain, 20 and for some reason its still displaying as very small text and I am not to sure why this is?
Just so that this isn't a massive piece of code I have slimmed it down as much as I can the frame that this is stored in uses a GridBagLayout
ArrayUtil.ArrayToListModel
converts any List<T>;
to ListMode<T>;
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{0};
gridBagLayout.columnWeights = new double[]{1.0};
gridBagLayout.rowWeights = new double[]{1.0};
frame.getContentPane().setLayout(gridBagLayout);
JScrollPane showlistScrollPane = new JScrollPane();
GridBagConstraints gbc_showlistScrollPane = new GridBagConstraints();
gbc_showlistScrollPane.insets = new Insets(0, 0, 0, 5);
gbc_showlistScrollPane.fill = GridBagConstraints.BOTH;
gbc_showlistScrollPane.gridx = 0;
gbc_showlistScrollPane.gridy = 0;
frame.getContentPane().add(showlistScrollPane, gbc_showlistScrollPane);
ArrayList<String> randomData = new ArrayList<String>();
randomData.add("Random drhgiudrhgiudhri");
randomData.add("Data dsirhgodhroih");
ListModel<String> pagesList = ArrayUtil.ArrayToListModel(randomData);
showlist = new JList<String>(pagesList);
showlist.setFont(new Font("Tahoma", Font.PLAIN, 20));
showlistScrollPane.setViewportView(showlist);
No matter how big or small I make the font for showlist
it never changes from its default.
Upvotes: 0
Views: 427
Reputation: 347184
The means by which content is displayed within a JList
is not defined by the JList
but is defined by the ListCellRenderer
assigned to the JList
, so
showlist.setFont(new Font("Tahoma", Font.PLAIN, 20));
Will have no effect on the content displayed within the JList
Take a look at Writing a Custom Cell Renderer for more details
As an example...
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test1 {
public static void main(String[] args) {
new Test1();
}
public Test1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JList list = new JList(new String[]{"Happy list", "of happy things", "bunnies", "unicorns", "sharks"});
list.setCellRenderer(new BigFontListCellRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(list));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BigFontListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setFont(new Font("Tahoma", Font.PLAIN, 20));
return this;
}
}
}
Upvotes: 3