Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

java - JCombobox value retrieve from My Sql

I use the following code for retrieve the data from mysql Student data Base.It was work properly.It will show the student Name in combo box.The value is retrieved from Db.

My problem is the first value(first student Name which is stored in Database column) displayed in the Jcombobox name field Automatically and it was show when I click Combo Box arrow..It means when I execute this following code I see the first value(first Name) in combobox's name field nearby arrow .But I need "Student Name" Title in there.If i assign that design time but that same name displayed once again when i click that arrow.how to solve this?

My another problem is how to get select "combo box items "when I type combo box's name field.It means if I type S means the combo box items selected which is started with S.

package designstudent;
import java.awt.*;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.*;



public class FrmeA extends javax.swing.JFrame  {

    FrmA1 f1=new FrmA1();
    Statement TmpFlxTSt=null;
    ResultSet TmpFlxTRs=null;
    GContnStr GCS=new GContnStr();

public FrmeA() {
    initComponents();
}

public void combo() {

    try {
        GCS.GContnStr();
        TmpFlxTSt= GCS.GCotnStr.createStatement();
        String select = "Select StudName from studentmaster";
        TmpFlxTRs = TmpFlxTSt.executeQuery(select);

        while (TmpFlxTRs.next()) {
            cbx.addItem(TmpFlxTRs.getString("StudName"));
            Object comboitem=cbx.getSelectedItem();
        }

        TmpFlxTRs.close();
        TmpFlxTSt.close();
    }
    catch(Exception e) {
        System.out.println(e);
    }

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    CmdA = new javax.swing.JButton();
    CmdE = new javax.swing.JButton();
    CmdD = new javax.swing.JButton();
    cmdQ = new javax.swing.JButton();
    jPanel3 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    FlxT = new javax.swing.JTable();
    cbx = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jPanel2.setBorder(
        javax.swing.BorderFactory.createTitledBorder(
            javax.swing.BorderFactory.createMatteBorder(
                1, 1, 1, 1, new java.awt.Color(0, 0, 255
            )
        ), "Commands")
    );

    CmdA.setFont(new java.awt.Font("Arial", 0, 10));
    CmdA.setText("+");
    CmdA.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CmdAActionPerformed(evt);
        }
    });

    CmdE.setFont(new java.awt.Font("Arial", 0, 10));
    CmdE.setText("E");

    CmdD.setFont(new java.awt.Font("Arial", 0, 10));
    CmdD.setText("-");

    cmdQ.setFont(new java.awt.Font("Arial", 0, 10));
    cmdQ.setText("Q");
    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(
        javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(
              CmdA,javax.swing.GroupLayout.PREFERRED_SIZE,64,javax.swing.GroupLayout.PREFERRED_SIZE
                 )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CmdE, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CmdD, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdQ, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CmdA, CmdE, cmdQ});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CmdA, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CmdE, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                    .addComponent(CmdD, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                    .addComponent(cmdQ, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 0, 255)));

        FlxT.setFont(new java.awt.Font("Arial", 0, 10));
        FlxT.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "StudName", "StudClass", "StudMrk1", "StudMrk2", "StudTot", "StudRes", "StudNO", "StudId"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(FlxT);
        FlxT.getColumnModel().getColumn(0).setResizable(false);
        FlxT.getColumnModel().getColumn(0).setPreferredWidth(150);
        FlxT.getColumnModel().getColumn(1).setResizable(false);
        FlxT.getColumnModel().getColumn(1).setPreferredWidth(70);
        FlxT.getColumnModel().getColumn(2).setResizable(false);
        FlxT.getColumnModel().getColumn(2).setPreferredWidth(70);
        FlxT.getColumnModel().getColumn(3).setResizable(false);
        FlxT.getColumnModel().getColumn(3).setPreferredWidth(70);
        FlxT.getColumnModel().getColumn(4).setResizable(false);
        FlxT.getColumnModel().getColumn(4).setPreferredWidth(70);
        FlxT.getColumnModel().getColumn(5).setResizable(false);
        FlxT.getColumnModel().getColumn(5).setPreferredWidth(70);
        FlxT.getColumnModel().getColumn(6).setMinWidth(0);
        FlxT.getColumnModel().getColumn(6).setPreferredWidth(0);
        FlxT.getColumnModel().getColumn(6).setMaxWidth(0);
        FlxT.getColumnModel().getColumn(7).setMinWidth(0);
        FlxT.getColumnModel().getColumn(7).setPreferredWidth(0);
        FlxT.getColumnModel().getColumn(7).setMaxWidth(0);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1015, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 249, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(54, Short.MAX_VALUE)))
        );

        cbx.setEditable(true);
        cbx.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        cbx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(657, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(cbx, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(791, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cbx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(777, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(190, 190, 190)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(503, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1047, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .add

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have look at AutoCompete JComboBox / JTextField, notice you have to save both Java classes, examples here, please don't use Netbeans generated code, because in most cases is too hard overrive basic methods, use Standard Swing JComponents instead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...