Results 1 to 3 of 3

Thread: Jlist Columns

  1. #1
    DenRed is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Jlist Columns

    I am working on a program that is pulling simple data from MYSQL and I can not get JList to show all the columns in the record. I have tried two dimensional arrays but when I do that I get weird output does anyone have a direction they could point me in. Some of the code may not make sense since I started with a sample program that was dropping tables.

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.*;

    public class AppletSQL1 extends JApplet implements ActionListener
    {
    private JList tableList;
    private JList tableList2;

    private JButton dropButton;

    private Connection conn;

    public void init()
    {
    conn = null;
    try
    {
    String userName = "root";
    String password = "spider";
    String url = "jdbc:mysql://localhost/testDB";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
    System.out.println("connected");

    }
    catch (Exception e)
    {
    System.out.println("conn failed");
    }

    Container c = getContentPane();
    tableList = new JList();
    tableList2 = new JList();
    loadTables();
    c.add(new JScrollPane(tableList, tableList2), BorderLayout.NORTH);
    //c.add(new JScrollPane(tableList2), BorderLayout.CENTER);

    dropButton = new JButton("Drop Table");
    dropButton.addActionListener(this);
    c.add(dropButton, BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent e)
    {
    try
    {
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("drop table "
    + tableList.getSelectedValue());
    }
    catch (SQLException actionException)
    {
    }
    }

    private void loadTables()
    {
    String[] arrayList = new String[10];
    //String[] arrayList2 = new String[10];
    int i=0;
    try
    {
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("SELECT * FROM Coffeetypes1");

    while (rs.next())
    {
    arrayList[i]=(rs.getString(1));
    //arrayList2[i]=(rs.getString(2));
    i++;
    }
    rs.close();
    }
    catch (SQLException e)
    {
    }

    tableList.setListData(arrayList);
    //tableList2.setListData(arrayList2);
    }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    A JTable (not a JList) should be used for this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    ...and you're eating your exceptions.
    At the very least do printStackTrace(), otherwise you'll never know what happened when something goes wrong.

Similar Threads

  1. Can't add columns in table viewer
    By feiqu0325 in forum SWT / JFace
    Replies: 1
    Last Post: 03-24-2009, 12:46 PM
  2. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 05:24 PM
  3. Using Columns With JTextField
    By The Evil Genius in forum AWT / Swing
    Replies: 1
    Last Post: 03-17-2008, 01:01 AM
  4. Problems with JSF (columns)
    By Peter in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 07-04-2007, 06:43 AM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •