I have this table and Id like to make a header just like the one that goes from 0 to 10, but vertical, and that goes from A to J (image below)
Something else I want to know is how I change the color of the square depending if the number is >10.
I have this table and Id like to make a header just like the one that goes from 0 to 10, but vertical, and that goes from A to J (image below)
Something else I want to know is how I change the color of the square depending if the number is >10.
You can add any component as the row header view of the scroll pane.
In this case you might be able to use a JList with a custom renderer:
import java.awt.*;
import javax.swing.*;
public class ListRowHeader extends JPanel
{
public ListRowHeader()
{
JTable table = new JTable(10, 10);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
String[] letters = {"A", "B", "C", "D","E", "F", "G", "H", "I", "J"};
JList<String> list = new JList<>( letters );
list.setFixedCellWidth(50);
list.setFixedCellHeight( table.getRowHeight() );
list.setCellRenderer( new RowHeaderRenderer() );
scrollPane.setRowHeaderView( list );
}
private static class RowHeaderRenderer extends DefaultListCellRenderer
{
public RowHeaderRenderer()
{
setHorizontalAlignment(JLabel.CENTER);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setFont(UIManager.getFont("TableHeader.font"));
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setBackground(UIManager.getColor("TableHeader.background"));
return this;
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("ListRowHeader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ListRowHeader(), BorderLayout.LINE_START);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
}
}