Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying an empty JList

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Can anyone tell me how I can display a empty Jlist with a set width and height. I'll be adding entires to the list throughtout the running of the application using a DefaultListModel.

Any help is appreciated
 
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.util.*;
public class jl extends Applet
{
Vector v = new Vector();
JList myJL = new JList(v);
public jl() {}
public void init()
{
add(myJL);
setBackground(Color.black);
myJL.setVisible(true);
v.add("@123456789");
myJL.setFixedCellHeight(50);
myJL.setFixedCellWidth(200);
}
public void start()
{
v.remove("@123456789");
}
}
 
I've looked over your code and tried a few different things to see if I could get the JList to display initially without any contents. I have not been successful. I've included the code that I'm working on in this posting. Hopefully that may give you a better idea of what I'm trying to do. I appreciate any and all help. Thanks

Code:
package src.gui;
import javax.swing.*;
import java.util.Vector;
import java.awt.*;
import javax.swing.border.TitledBorder;
import java.sql.*;
import java.awt.event.*;

/**
 *  This class is used
 */
public class ClientFrame extends JFrame{
    
    private DefaultListModel tablesDefaultListModel = null;
    private JButton connectButton = new JButton("Connect to Database");
    private JButton convertButton = new JButton("Convert tables to XML");
    private Vector v = new Vector();
    private JList tablesJList = new JList(v);
     
    public ClientFrame() {
        final int WIDTH = 600;
        final int HEIGHT = 300;
        setSize(WIDTH, HEIGHT);
        
        JPanel tablesPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        TitledBorder titledBorder = new TitledBorder("Available Tables");
      
        tablesJList.setBorder(titledBorder);
        tablesJList.setFixedCellWidth(550);
        
        Container contentPane = getContentPane();
        setBackground(Color.white); 
        setForeground(Color.black);
        
        tablesPanel.add(tablesJList);
        tablesJList.setVisible(true);
        tablesDefaultListModel = new DefaultListModel();
        tablesJList.setModel(tablesDefaultListModel);
        
        contentPane.add(tablesPanel, "North");
        ActionListener listener = new ButtonListener();
        connectButton.addActionListener(listener);
        convertButton.addActionListener(listener);
        buttonPanel.add(connectButton);
        buttonPanel.add(convertButton);
        contentPane.add(buttonPanel, "South");
        
    }

    public static void main(String[] args) {
        ClientFrame frame = new ClientFrame();
        frame.setTitle("Interface");
        frame.show();
    }
    
    
    
    protected void processWindowEvent (WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            closeWindow();
        }
    }
        
    private void closeWindow() {
        dispose();
    }
    
    public class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            Object src = e.getSource();
            if(src == connectButton) {
                Connection conObject = Connect.getConnection();
                ResultSet result = Connect.retrieveTables(conObject);
                String table = null;
                try {
                    while(result.next()) {
                        table = result.getString(1);
                        if (!tablesDefaultListModel.contains(table)) {
                            tablesDefaultListModel.addElement(table);
                        }
                    }
                    tablesJList.setModel(tablesDefaultListModel);
                }
                catch(SQLException exception) {
                    System.out.println("An exception was raised when traversing the " +
                    		"result set");
                    System.err.println(exception);
                }
            }
        }

    }

}
 
// you should use JComboBox for reducing display problem because swing display components may have some diplay problem when they are used with awt display components.
You use JFrame and this means you are using Swing.
// my code don't use inner class here, inner class may be used for more complicated task
Code:
import javax.swing.*;
import java.util.Vector;
import java.awt.*;
import javax.swing.border.TitledBorder;
import java.sql.*;
import java.awt.event.*;

/**
 *  This class is used
 */
public class ClientFrame extends JFrame{
    
    //private DefaultListModel tablesDefaultListModel = null;
    private JButton connectButton = new JButton("Connect to Database");
    private JButton convertButton = new JButton("Convert tables to XML");
    private Vector v = new Vector();
    private JComboBox tablesJList = new JComboBox(v);
    public ClientFrame() {
        final int WIDTH = 600;
        final int HEIGHT = 300;
        setSize(WIDTH, HEIGHT);
        
        JPanel tablesPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
tablesPanel.setBackground(Color.red);
        TitledBorder titledBorder = new TitledBorder("Available Tables");
      
        tablesJList.setBorder(titledBorder);
        tablesJList.setPreferredSize(new Dimension(550,50));
        //tablesJList.setFixedCellWidth(550);
        
        Container contentPane = getContentPane();
        setBackground(Color.white); 
        setForeground(Color.black);
        
        tablesPanel.add(tablesJList);
        //tablesDefaultListModel = new DefaultListModel();
        //tablesJList.setModel(tablesDefaultListModel);
        
        contentPane.add(tablesPanel, "North");
        ActionListener listener = new ButtonListener(tablesJList);
        connectButton.addActionListener(listener);
        convertButton.addActionListener(listener);
        buttonPanel.add(connectButton);
        buttonPanel.add(convertButton);
        contentPane.add(buttonPanel, "South");
    }

    public static void main(String[] args) {
        ClientFrame frame = new ClientFrame();
        frame.setTitle("Interface");
        frame.setVisible(true);
    }
    
    
    
    protected void processWindowEvent (WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            closeWindow();
        }
    }
        
    private void closeWindow() {
        dispose();
    }    
}

class ButtonListener implements ActionListener{
    JComboBox pointToJCombo;
       public ButtonListener(JComboBox tempCombo)
       {
        pointToJCombo = tempCombo;
       }
        public void actionPerformed(ActionEvent e) {
        pointToJCombo.addItem("hello");
/*
            Object src = e.getSource();
            if(src == connectButton) {
                Connection conObject = Connect.getConnection();
                ResultSet result = Connect.retrieveTables(conObject);
                String table = null;
                try {
                    while(result.next()) {
                        table = result.getString(1);
                        if (!tablesDefaultListModel.contains(table)) {
                            tablesDefaultListModel.addElement(table);
                        }
                    }
                    tablesJList.setModel(tablesDefaultListModel);
                }
                catch(SQLException exception) {
                    System.out.println("An exception was raised when traversing the " +
                            "result set");
                    System.err.println(exception);
                }
            }
*/
        }
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top