Aligned Columns in java.awt.List

Aligned Columns in java.awt.List

You can use, or extend, java.awt.List to create a simple multi-column list. In order to do so, you need to use a fixed character width font set like Courier.

Suppose you have a java.awt.List object, called multiList sitting on your GUI, and you want your multiList to have three columns each of length 20 characters:

 //define some data membersprivate final static String PAD_CHARACTER = " "; //spaceprivate int[] widths ={20,20,20};private String[] padStrings = new String[widths.length];

Now, create as many padding strings as your columns, and each as wide as your widths:

     for(int i=0; i

Say you want to insert ("999999","JOHN","DOE") as a row of data, and you want them to be bold:

     String[] entries = new String[3];    entries[0] = "JOHN";    entries[1] =  "DOE";    entries[2] = "999 WHICHEVER STREET";    multiList.setFont(new Font("Courier", Font.BOLD, 12));    insertRow(multiList,entries);

Let's insert another row of data just for fun:

     entries[0] = "JANETTE";    entries[1] =  "DOVE";    entries[2] = "999 SAME STREET";    insertRow(multiList,entries);

Note that before calling insertRow(...) method, you should check if the length of your entries corresponds to the width of each column, and if not take appropriate action.

Voila, you have your multiList all aligned for three columns.

The following is the code for method insertRow(...):

     public void insertRow(List aList, String[] rowItems)     {        StringBuffer sb = new StringBuffer();        for(int item=0; item
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.