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","F ive","Six" ,
"Seven","Eight","Nine","Te n","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(n ew PrintListener( ));
add(pane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Simple List Example");
frame.setDefaultCloseOpera tion(JFram e.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(ActionEven t e) {
int selected[] = list.getSelectedIndices( );
System.out.println("Select ed Elements: ");
for (int i=0; i < selected.length; i++) {
String element =
(String)list.getModel( ).getElementAt(selected[i] );
System.out.println(" " + element);
}
}
}
}
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"
"Seven","Eight","Nine","Te
JList list;
public SimpleList( ) {
this.setLayout(new BorderLayout( ));
list = new JList(label);
JScrollPane pane = new JScrollPane(list);
JButton button = new JButton("Print");
button.addActionListener(n
add(pane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Simple List Example");
frame.setDefaultCloseOpera
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(ActionEven
int selected[] = list.getSelectedIndices( );
System.out.println("Select
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());
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try implementing this and then post back with your code and any specific questions and/or errors/issues you are getting...
ASKER
Sorry I don't know how to do what you were saying.
But I tried this, but would not recognize "newstring2" as array.
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);
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.
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);
}
}
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
Best regards,
Kevin