• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How can I display LISTS from this code?

I have this working code attached in Code Snippet, now I need to show it in LIST with SCROLLS instead of ShowMessageDialog.
I got this sample code from a book but I don't know how to implement it to my old existing code. Anyone?

//  SimpleList.java
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleList extends JPanel {
    String label[] = { "Zero","One","Two","Three","Four","Five","Six",
                       "Seven","Eight","Nine","Ten","Eleven" };
    JList list;

    public SimpleList( ) {
        this.setLayout(new BorderLayout( ));
        list = new JList(label);
        JScrollPane pane = new JScrollPane(list);
        JButton button = new JButton("Print");
        button.addActionListener(new PrintListener( ));

        add(pane, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);
    }

    public static void main(String s[]) {
         JFrame frame = new JFrame("Simple List Example");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new SimpleList( ));
         frame.setSize(250, 200);
         frame.setVisible(true);
    }

    // An inner class to respond to clicks of the Print button
    class PrintListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int selected[] = list.getSelectedIndices( );
            System.out.println("Selected Elements:  ");

            for (int i=0; i < selected.length; i++) {
                String element =
                      (String)list.getModel( ).getElementAt(selected[i]);
                System.out.println("  " + element);
            }
        }
    }
}
import java.sql.*;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
import javax.swing.*;
 
public class NewClass {
public static void main(String args[]) {
            
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy");
Date currentTime = cal.getTime();
String dateString = formatter.format(currentTime);
 
String tabletodisplay;
tabletodisplay = "inventorytable";
 
        try { 
            String url = "jdbc:mysql://localhost:3306/alcochatinventory"; 
            Connection conn = DriverManager.getConnection(url,"root","root"); 
            
PreparedStatement stmtInventory = conn.prepareStatement("SELECT * FROM  `"+tabletodisplay+"` WHERE date1 = ?");
 
ResultSet uprsInventory; 
            stmtInventory.setString(1,dateString);
            uprsInventory=stmtInventory.executeQuery();
            
            ResultSetMetaData uprsInventoryMetaData = uprsInventory.getMetaData();
 
 
    int InventorynumberOfColumns = uprsInventoryMetaData.getColumnCount();
int i1;
i1=0;
    String newstring1, newstring2;
newstring2 = "";
 
while (uprsInventory.next ()) {
for (int i=1; i<=InventorynumberOfColumns; i++) {
String columnName = uprsInventoryMetaData.getColumnLabel(i);
//System.out.print(columnName + ": ");
String columnValue = uprsInventory.getString(i);
    
//System.out.println(columnValue);
if (i1<35){
newstring1 = columnName + ": " + columnValue;
newstring2 = newstring2 + newstring1 + "\n";
i1 = i1 + 1;
//System.out.println("i1: " + i1);
}
 
else  {
    JOptionPane.showMessageDialog(null, newstring2);
    newstring2 = "";    
    i1=0;
    //System.out.println("i1: " + i1);
}
 
if (i==InventorynumberOfColumns) {
        JOptionPane.showMessageDialog(null, newstring2);
    
}
}
}
 
stmtInventory.close();
conn.close();
} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}

Open in new window

0
glynco
Asked:
glynco
  • 3
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
To start with, you need to put your data in an array instead of one string variable.

newstring1 = columnName + ": " + columnValue;
newstring2 = newstring2 + newstring1 + "\n";

This needs to be and array of 'columnName + ": " + columnValue'.  A good way to accomplish this is to use a List<String> and add values, then you use the List as an array.

e.g.

List<String> strList = new ArrayList<String>();
strList.add(columnName + ": " + columnValue);
strList.toArray(); // http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html

If you notice above, the JList is being built using String[] (array) called label:
list = new JList(label);

Therefore, if you use the List<String> to hold you values then you can formulate a JList instead of showing a dialog box by doing this in code:
list = new JList(strList.toArray());

Hope that helps.

Best regards,
Kevin
0
 
Kevin CrossChief Technology OfficerCommented:
Try implementing this and then post back with your code and any specific questions and/or errors/issues you are getting...
0
 
glyncoAuthor Commented:
Sorry I don't know how to do what you were saying.

But I tried this, but would not recognize "newstring2" as array.
newstring1 = "{" +'"' + columnName + '"' + ", " + '"' + columnValue + '"' + "}";
 
newstring2 = newstring2 + newstring1 + ",";
 
    Object rows[][] = newstring2;
    
    Object headers[] = { "Column Name", "Colomn Value" };
    JFrame frame = new JFrame("Scrollless Table");
    JTable table = new JTable(rows, headers);
    frame.getContentPane().add(table, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

Open in new window

0
 
glyncoAuthor Commented:
I also have difficulty in putting all the classes altogether. I tried this putting it in one main class but got errors.

I have not encountered working with more than one class yet.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class NewClass {
public static void main(String args[]) {
    
    //  SimpleList.java
//
 
 
 
    String label[] = { "Zero","One","Two","Three","Four","Five","Six",
                       "Seven","Eight","Nine","Ten","Eleven" };
    JList list;
 
  
        this.setLayout(new BorderLayout( ));
        list = new JList(label);
        JScrollPane pane = new JScrollPane(list);
        JButton button = new JButton("Print");
        button.addActionListener(new PrintListener( ));
 
        add(pane, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);
 
 
 
         JFrame frame = new JFrame("Simple List Example");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new SimpleList( ));
         frame.setSize(250, 200);
         frame.setVisible(true);
 
 
    // An inner class to respond to clicks of the Print button
 
            int selected[] = list.getSelectedIndices( );
            System.out.println("Selected Elements:  ");
 
            for (int i=0; i < selected.length; i++) {
                String element =
                      (String)list.getModel( ).getElementAt(selected[i]);
                System.out.println("  " + element);
            
        
            }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
glynco, sorry I missed your messages last night, but by the accepting of the solution I am hoping you saw what I was proposing there.  Instead of using newstring2, just use a List<String> and then instantiate the JList using the List.toArray() method as JList takes an Object[] for list values.

Best regards,
Kevin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now