Link to home
Start Free TrialLog in
Avatar of glynco
glynco

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try implementing this and then post back with your code and any specific questions and/or errors/issues you are getting...
Avatar of glynco
glynco

ASKER

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

Avatar of glynco

ASKER

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

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