Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 300,131 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,889 people online right now. Registration is fast and FREE... Join Now!





Multi-Column JList

Auto Aligning columns of a JList which act as a single row

Submitted By: William_Wilson
Actions:
Rating:
Views: 9,446

Language: Java

Last Modified: March 29, 2006
Instructions: self contained code example, the pattern should be evident

Snippet


  1. import java.io.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import javax.swing.*;
  6. import java.awt.event.*; 
  7. import javax.swing.event.*;
  8.  
  9. public class MultiColumn{
  10.      
  11.      JFrame           jf;
  12.      JScrollPane scroll;
  13.      JList          list;
  14.      String[][]     columnData;
  15.      
  16.      public MultiColumn(){
  17.           //create the frame and JList JPanel
  18.           jf = new aFrame();
  19.           //create element List array
  20.           addElements();
  21.           //set list for JList
  22.           list.setListData(columnData);
  23.           //create Renderer and dislpay
  24.           list.setCellRenderer(new MyCellRenderer());
  25.      }
  26.      
  27.      public void addElements(){
  28.           //first number is number of rows, second is number of columns
  29.           columnData = new String[4][3];
  30.           //static setting of String[][]
  31.           columnData[0][0] = "William";
  32.           columnData[0][1] = "A";
  33.           columnData[0][2] = "Wilson";
  34.           //dynamic setting of String[][]
  35.           for(int i=1;i<4;i++){
  36.                columnData[i][0] = "William";
  37.                columnData[i][1] = String.valueOf(i*13);
  38.                Calendar c = new GregorianCalendar();
  39.                columnData[i][2] = ((Date)c.getTime()).toString();
  40.           }
  41.      }
  42.      
  43.      public class aFrame extends JFrame{
  44.           public aFrame(){
  45.                super("Multi-Column JList Example");
  46.                getContentPane().add(new PanelBuilder());
  47.          
  48.                // display rules
  49.                setResizable(true);
  50.                setLocation(250,50);
  51.                setBackground(Color.lightGray);
  52.              setDefaultCloseOperation(EXIT_ON_CLOSE);
  53.              setSize(650, 400);
  54.              setVisible(true);
  55.           }
  56.           private class PanelBuilder extends JPanel{
  57.                public PanelBuilder(){
  58.                     GridBagLayout layout = new GridBagLayout();
  59.                     GridBagConstraints layoutConstraints = new GridBagConstraints();
  60.                     setLayout(layout);
  61.                    
  62.                     scroll     = new JScrollPane();
  63.                    list      = new JList();
  64.                    layoutConstraints.gridx      = 0; layoutConstraints.gridy = 0;
  65.                     layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
  66.                     layoutConstraints.fill           = GridBagConstraints.BOTH;
  67.                     layoutConstraints.insets      = new Insets(1, 1, 1, 1);
  68.                     layoutConstraints.anchor      = GridBagConstraints.CENTER;
  69.                     layoutConstraints.weightx      = 1.0; layoutConstraints.weighty = 1.0;
  70.                     scroll = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  71.                     JScrollPane. HORIZONTAL_SCROLLBAR_NEVER);
  72.                     layout.setConstraints(scroll, layoutConstraints);
  73.                    add(scroll);
  74.                }
  75.           }
  76.          
  77.      }
  78.      
  79.      static class MyCellRenderer extends JPanel implements ListCellRenderer{
  80.           JLabel left, middle, right;
  81.           MyCellRenderer() {
  82.                setLayout(new GridLayout(1, 3));
  83.                left      = new JLabel();
  84.                middle     = new JLabel();
  85.                right      = new JLabel();
  86.                left.setOpaque(true);
  87.                middle.setOpaque(true);
  88.                right.setOpaque(true);
  89.                add(left);
  90.                add(middle);
  91.                add(right);
  92.           }
  93.          
  94.           public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
  95.                String leftData      = ((String[])value)[0];
  96.                String middleData      = ((String[])value)[1];
  97.                String rightData      = ((String[])value)[2];
  98.                left.setText(leftData);
  99.                middle.setText(middleData);
  100.                right.setText(rightData);
  101.                if(isSelected){
  102.                     left.setBackground(list.getSelectionBackground());
  103.                     left.setForeground(list.getSelectionForeground());
  104.                     middle.setBackground(list.getSelectionBackground());
  105.                     middle.setForeground(list.getSelectionForeground());
  106.                     right.setBackground(list.getSelectionBackground());
  107.                     right.setForeground(list.getSelectionForeground());
  108.                }
  109.                else{
  110.                     left.setBackground(list.getBackground());
  111.                     left.setForeground(list.getForeground());
  112.                     middle.setBackground(list.getBackground());
  113.                     middle.setForeground(list.getForeground());
  114.                     right.setBackground(list.getBackground());
  115.                     right.setForeground(list.getForeground());
  116.                }
  117.                setEnabled(list.isEnabled());
  118.                setFont(list.getFont());
  119.                return this;
  120.           }
  121.      }
  122.      
  123.      
  124.      public static void main(String args[]){
  125.           MultiColumn jf = new MultiColumn();
  126.      }
  127.      
  128. }
  129. /*I have found this useful on countless occassions, enjoy*/
  130. /*William_Wilson. §*/

Copy & Paste


Comments


dagway 2008-07-03 16:11:20

can i have the copy of how to make the row and colom


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month