表格中的Java组合框覆盖写入数据

时间:2016-09-26 19:17:16

标签: java swing jcombobox ucanaccess

我努力为这张票找到一个更好的头衔,但那是我能想到的最好的。

我遇到了JTable内部的JComboBox问题。该程序是原始版本的缩小版本。它从数据库中提取数据并将该信息放入组合框中。从数据库中提取数据时,它将覆盖该字段。当从变量输入数据时,问题不存在。我想也许数据库的延迟很小就是问题,所以我加了一些延迟。我一路走到20秒,但仍然没有得到相同的结果。有一个名为DB的变量。将其设置为true,它将从db中提取数据,false将使用局部变量。

如果数据库为true,请转到最后一条记录并单击其中,然后在上述字段中单击一次。他们都是2但现在他们是1.做同样的事情,但将最后一个字段设置为100并按照上述步骤操作。然后所有记录都是100.这不是预期的行为。除非用户更改值,否则该字段应保持不变。如果DB为false,它将按预期运行。我已经尝试过各种各样的东西来隔离这个问题。原始数据来自db的事实应该无关紧要,它只是一个字符串。我认为还有另一个问题与同一个问题有关。如果字段为空白,则组合将覆盖已清除的字段,并且不允许再次清除该字段。

我准备了一个jar文件here,它包含所有源代码和db文件。我不确定是否有更好的方法来附加jar文件。您将需要从jar文件中提取代码,否则将无法正常工作。

以下是源代码:

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;
import java.util.ArrayList;
import java.util.Iterator;

public class ErrorDemo {
    private String[][] comm = new String[3][6];
    private int index = 0;
    private JTable table;
    private DefaultTableModel myData;
    public String headers[] = new String[35];
    private boolean DB = true;
    // private boolean DB = false;

    public ErrorDemo () {
        Visual();
        StartQuery();
    }

    public void Visual () {
        final JFrame frame = new JFrame( "Automation" );
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );

        myData = new DefaultTableModel();
        table = new JTable(  );
        table.setModel( myData );
        table.setFillsViewportHeight(true);

        JScrollPane scroll1 = new JScrollPane(table);
        scroll1.setVisible( true );
        frame.add(scroll1);

        frame.repaint();
        frame.setVisible( true );
    }

    public void StartQuery () {
        // set the data to be placed into the fields
        index = 2;
        headers[1] = "Feild 1";
        headers[2] = "Feild 2";
        comm[1][1] = "aaaaaaaaaaaaaaaa2";
        comm[1][2] = "aaaaaaaaaaaaaaaa2";
        comm[1][3] = "aaaaaaaaaaaaaaaa2";
        comm[1][4] = "aaaaaaaaaaaaaaaa2";
        comm[1][5] = "aaaaaaaaaaaaaaaa2";
        comm[2][1] = "aaaaaaaaaaaaaaaa2";
        comm[2][2] = "aaaaaaaaaaaaaaaa2";
        comm[2][3] = "aaaaaaaaaaaaaaaa2";
        comm[2][4] = "aaaaaaaaaaaaaaaa2";
        comm[2][5] = "aaaaaaaaaaaaaaaa2";

        //set the name of the headers
        for ( int i = 1 ; i <= index ; i++ ) 
            myData.addColumn( headers[i] );
        //place the data into the table
        Vector v = new Vector();
        for ( int i = 1; i <= 5; i++ ) {
            v = new Vector();
            v.addElement( comm[1][i] );
            v.addElement( comm[2][i] );
            myData.addRow( v );
        }

        //place the data into the combo box then place the combo box into the table
        TableColumn Column1 = table.getColumnModel().getColumn( 0 );
        JComboBox comboBox = new JComboBox();
        String[] output = new String[61];
        // if DB is true it pulls the data from the the database else it will use a static string.
        if ( DB ) {
            String comm1 = "SELECT data FROM Table1 ORDER BY data; ";
            // change the following path to where ever to put it.
            DBDemo dbl1 = new DBDemo( "U:/Users/Goff/java/rcc automation/GreatBeyond/db1.accdb" );
            dbl1.GetQuery( comm1, 1, comboBox );
        } else {
            // gofflib STL = new gofflib();
            // STL.sleeper( 20000 ); //a test to see if adding delay would produce the issue, it did not.
            output = new String [] { "", "aaaaaaaaaaaaaaaa1", "aaaaaaaaaaaaaaaa2", "aaaaaaaaaaaaaaaa3", "aaaaaaaaaaaaaaaa4", "aaaaaaaaaaaaaaaa5", "aaaaaaaaaaaaaaaa6", "aaaaaaaaaaaaaaaa7", "aaaaaaaaaaaaaaaa8", "aaaaaaaaaaaaaaaa9", "aaaaaaaaaaaaaaaa10", "aaaaaaaaaaaaaaaa11", "aaaaaaaaaaaaaaaa12", "aaaaaaaaaaaaaaaa13", "aaaaaaaaaaaaaaaa14", "aaaaaaaaaaaaaaaa15", "aaaaaaaaaaaaaaaa16", "aaaaaaaaaaaaaaaa17", "aaaaaaaaaaaaaaaa18", "aaaaaaaaaaaaaaaa19", "aaaaaaaaaaaaaaaa20", "aaaaaaaaaaaaaaaa21", "aaaaaaaaaaaaaaaa22", "aaaaaaaaaaaaaaaa23", "aaaaaaaaaaaaaaaa24", "aaaaaaaaaaaaaaaa25", "aaaaaaaaaaaaaaaa26", "aaaaaaaaaaaaaaaa27", "aaaaaaaaaaaaaaaa28", "aaaaaaaaaaaaaaaa29", "aaaaaaaaaaaaaaaa30", "aaaaaaaaaaaaaaaa31", "aaaaaaaaaaaaaaaa32", "aaaaaaaaaaaaaaaa33", "aaaaaaaaaaaaaaaa34", "aaaaaaaaaaaaaaaa35", "aaaaaaaaaaaaaaaa36", "aaaaaaaaaaaaaaaa37", "aaaaaaaaaaaaaaaa38", "aaaaaaaaaaaaaaaa39", "aaaaaaaaaaaaaaaa40", "aaaaaaaaaaaaaaaa41", "aaaaaaaaaaaaaaaa42", "aaaaaaaaaaaaaaaa43", "aaaaaaaaaaaaaaaa44", "aaaaaaaaaaaaaaaa45", "aaaaaaaaaaaaaaaa46", "aaaaaaaaaaaaaaaa47", "aaaaaaaaaaaaaaaa48", "aaaaaaaaaaaaaaaa49", "aaaaaaaaaaaaaaaa50", "aaaaaaaaaaaaaaaa51", "aaaaaaaaaaaaaaaa52", "aaaaaaaaaaaaaaaa53", "aaaaaaaaaaaaaaaa54", "aaaaaaaaaaaaaaaa55", "aaaaaaaaaaaaaaaa56", "aaaaaaaaaaaaaaaa57", "aaaaaaaaaaaaaaaa58", "aaaaaaaaaaaaaaaa59", "aaaaaaaaaaaaaaaa60", "aaaaaaaaaaaaaaaa61", "aaaaaaaaaaaaaaaa62", "aaaaaaaaaaaaaaaa63", "aaaaaaaaaaaaaaaa64", "aaaaaaaaaaaaaaaa65", "aaaaaaaaaaaaaaaa66", "aaaaaaaaaaaaaaaa67", "aaaaaaaaaaaaaaaa68", "aaaaaaaaaaaaaaaa69", "aaaaaaaaaaaaaaaa70", "aaaaaaaaaaaaaaaa71", "aaaaaaaaaaaaaaaa72", "aaaaaaaaaaaaaaaa73", "aaaaaaaaaaaaaaaa74", "aaaaaaaaaaaaaaaa75", "aaaaaaaaaaaaaaaa76", "aaaaaaaaaaaaaaaa77", "aaaaaaaaaaaaaaaa78", "aaaaaaaaaaaaaaaa79", "aaaaaaaaaaaaaaaa80", "aaaaaaaaaaaaaaaa81", "aaaaaaaaaaaaaaaa82", "aaaaaaaaaaaaaaaa83", "aaaaaaaaaaaaaaaa84", "aaaaaaaaaaaaaaaa85", "aaaaaaaaaaaaaaaa86", "aaaaaaaaaaaaaaaa87", "aaaaaaaaaaaaaaaa88", "aaaaaaaaaaaaaaaa89", "aaaaaaaaaaaaaaaa90", "aaaaaaaaaaaaaaaa91", "aaaaaaaaaaaaaaaa92", "aaaaaaaaaaaaaaaa93", "aaaaaaaaaaaaaaaa94", "aaaaaaaaaaaaaaaa95", "aaaaaaaaaaaaaaaa96", "aaaaaaaaaaaaaaaa97", "aaaaaaaaaaaaaaaa98", "aaaaaaaaaaaaaaaa99", "aaaaaaaaaaaaaaaa100" };
            for ( int i = 1; i <= 100; i++ ) {
                comboBox.addItem( output[i] );
            }
        }
        Column1.setCellEditor(new DefaultCellEditor(comboBox));

        table.repaint();
        table.revalidate();
    }

    public static void main(String[] args) {
        new ErrorDemo();
    }
}

这是DBDemo

import java.sql.*;
import net.ucanaccess.jdbc.JackcessOpenerInterface;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import net.ucanaccess.converters.TypesMap.AccessType;
import net.ucanaccess.ext.FunctionType;
import net.ucanaccess.jdbc.UcanaccessConnection;
import net.ucanaccess.jdbc.UcanaccessDriver;
import org.apache.commons.lang.builder.*;
import org.apache.commons.logging.*;
import org.hsqldb.jdbc.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.*;

public class DBDemo {
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private String db;
    private String comm = "";
    private int index = 0;
    private String Path = "";

    public DBDemo ( String arg1 ) {
        Path = arg1;
    }

    public void GetQuery ( String comm, int index, JComboBox comb ) {
        try {
            String output = "";
            String path = new java.io.File(Path).getAbsolutePath();
            db = "jdbc:ucanaccess://" + path;
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            con = DriverManager.getConnection(db);
            st = con.createStatement();
            output = "";
            rs = st.executeQuery( comm );
            while (rs.next()) {
                for ( int i = 1; i <= index; i++ ) {
                    Object o = rs.getObject(i);
                    String t;
                    t = ( o == null ? "" : o.toString() );
                    output += t + " ";
                }
                comb.addItem( output );
                output = "";
            }
            rs.close();
            st.close();
            con.close();
        } catch( NullPointerException | SQLException | ClassNotFoundException ex ){
            ex.printStackTrace();
        }
    }
}

我试图让代码尽可能简洁。

如果DB = true或DB = false,则将相同的数据处理为字符串,然后将其放入组合框中。这怎么会引起不同的行为?

更新:这是使用自定义单元格渲染器的新代码。

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;
import java.util.ArrayList;
import java.util.Iterator;

public class ErrorDemo {
    private String[][] comm = new String[3][6];
    private int index = 0;
    private JTable table;
    private DefaultTableModel myData;
    public String headers[] = new String[35];
    private boolean DB = true;
    // private boolean DB = false;

    public ErrorDemo () {
        Visual();
        StartQuery();
    }

    public void Visual () {
        final JFrame frame = new JFrame( "Automation" );
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );

        myData = new DefaultTableModel();
        table = new JTable(  );
        table.setModel( myData );
        table.setFillsViewportHeight(true);

        JScrollPane scroll1 = new JScrollPane(table);
        scroll1.setVisible( true );
        frame.add(scroll1);

        frame.repaint();
        frame.setVisible( true );
    }

    public void StartQuery () {
        // set the data to be placed into the fields
        index = 2;
        headers[1] = "Feild 1";
        headers[2] = "Feild 2";
        comm[1][1] = "aaaaaaaaaaaaaaaa2";
        comm[1][2] = "aaaaaaaaaaaaaaaa2";
        comm[1][3] = "aaaaaaaaaaaaaaaa2";
        comm[1][4] = "aaaaaaaaaaaaaaaa2";
        comm[1][5] = "aaaaaaaaaaaaaaaa2";
        comm[2][1] = "aaaaaaaaaaaaaaaa2";
        comm[2][2] = "aaaaaaaaaaaaaaaa2";
        comm[2][3] = "aaaaaaaaaaaaaaaa2";
        comm[2][4] = "aaaaaaaaaaaaaaaa2";
        comm[2][5] = "aaaaaaaaaaaaaaaa2";

        //set the name of the headers
        for ( int i = 1 ; i <= index ; i++ ) 
            myData.addColumn( headers[i] );
        //place the data into the table
        Vector v = new Vector();
        for ( int i = 1; i <= 5; i++ ) {
            v = new Vector();
            v.addElement( comm[1][i] );
            v.addElement( comm[2][i] );
            myData.addRow( v );
        }

        //place the data into the combo box then place the combo box into the table
        TableColumn Column1 = table.getColumnModel().getColumn( 0 );
        // JComboBox comboBox = new JComboBox();
        MyComboBoxRenderer comboBox = new MyComboBoxRenderer();
        String[] output = new String[61];
        // if DB is true it pulls the data from the the database else it will use a static string.
        if ( DB ) {
            String comm1 = "SELECT data FROM Table1 ORDER BY data; ";
            // change the following path to where ever to put it.
            DBDemo dbl1 = new DBDemo( "U:/Users/Goff/java/rcc automation/GreatBeyond/db1.accdb" );
            dbl1.GetQuery( comm1, 1, comboBox );
        } else {
            gofflib STL = new gofflib();
            // STL.sleeper( 20000 ); //a test to see if adding delay would produce the issue, it did not.
            output = new String [] { "", "aaaaaaaaaaaaaaaa1", "aaaaaaaaaaaaaaaa2", "aaaaaaaaaaaaaaaa3", "aaaaaaaaaaaaaaaa4", "aaaaaaaaaaaaaaaa5", "aaaaaaaaaaaaaaaa6", "aaaaaaaaaaaaaaaa7", "aaaaaaaaaaaaaaaa8", "aaaaaaaaaaaaaaaa9", "aaaaaaaaaaaaaaaa10", "aaaaaaaaaaaaaaaa11", "aaaaaaaaaaaaaaaa12", "aaaaaaaaaaaaaaaa13", "aaaaaaaaaaaaaaaa14", "aaaaaaaaaaaaaaaa15", "aaaaaaaaaaaaaaaa16", "aaaaaaaaaaaaaaaa17", "aaaaaaaaaaaaaaaa18", "aaaaaaaaaaaaaaaa19", "aaaaaaaaaaaaaaaa20", "aaaaaaaaaaaaaaaa21", "aaaaaaaaaaaaaaaa22", "aaaaaaaaaaaaaaaa23", "aaaaaaaaaaaaaaaa24", "aaaaaaaaaaaaaaaa25", "aaaaaaaaaaaaaaaa26", "aaaaaaaaaaaaaaaa27", "aaaaaaaaaaaaaaaa28", "aaaaaaaaaaaaaaaa29", "aaaaaaaaaaaaaaaa30", "aaaaaaaaaaaaaaaa31", "aaaaaaaaaaaaaaaa32", "aaaaaaaaaaaaaaaa33", "aaaaaaaaaaaaaaaa34", "aaaaaaaaaaaaaaaa35", "aaaaaaaaaaaaaaaa36", "aaaaaaaaaaaaaaaa37", "aaaaaaaaaaaaaaaa38", "aaaaaaaaaaaaaaaa39", "aaaaaaaaaaaaaaaa40", "aaaaaaaaaaaaaaaa41", "aaaaaaaaaaaaaaaa42", "aaaaaaaaaaaaaaaa43", "aaaaaaaaaaaaaaaa44", "aaaaaaaaaaaaaaaa45", "aaaaaaaaaaaaaaaa46", "aaaaaaaaaaaaaaaa47", "aaaaaaaaaaaaaaaa48", "aaaaaaaaaaaaaaaa49", "aaaaaaaaaaaaaaaa50", "aaaaaaaaaaaaaaaa51", "aaaaaaaaaaaaaaaa52", "aaaaaaaaaaaaaaaa53", "aaaaaaaaaaaaaaaa54", "aaaaaaaaaaaaaaaa55", "aaaaaaaaaaaaaaaa56", "aaaaaaaaaaaaaaaa57", "aaaaaaaaaaaaaaaa58", "aaaaaaaaaaaaaaaa59", "aaaaaaaaaaaaaaaa60", "aaaaaaaaaaaaaaaa61", "aaaaaaaaaaaaaaaa62", "aaaaaaaaaaaaaaaa63", "aaaaaaaaaaaaaaaa64", "aaaaaaaaaaaaaaaa65", "aaaaaaaaaaaaaaaa66", "aaaaaaaaaaaaaaaa67", "aaaaaaaaaaaaaaaa68", "aaaaaaaaaaaaaaaa69", "aaaaaaaaaaaaaaaa70", "aaaaaaaaaaaaaaaa71", "aaaaaaaaaaaaaaaa72", "aaaaaaaaaaaaaaaa73", "aaaaaaaaaaaaaaaa74", "aaaaaaaaaaaaaaaa75", "aaaaaaaaaaaaaaaa76", "aaaaaaaaaaaaaaaa77", "aaaaaaaaaaaaaaaa78", "aaaaaaaaaaaaaaaa79", "aaaaaaaaaaaaaaaa80", "aaaaaaaaaaaaaaaa81", "aaaaaaaaaaaaaaaa82", "aaaaaaaaaaaaaaaa83", "aaaaaaaaaaaaaaaa84", "aaaaaaaaaaaaaaaa85", "aaaaaaaaaaaaaaaa86", "aaaaaaaaaaaaaaaa87", "aaaaaaaaaaaaaaaa88", "aaaaaaaaaaaaaaaa89", "aaaaaaaaaaaaaaaa90", "aaaaaaaaaaaaaaaa91", "aaaaaaaaaaaaaaaa92", "aaaaaaaaaaaaaaaa93", "aaaaaaaaaaaaaaaa94", "aaaaaaaaaaaaaaaa95", "aaaaaaaaaaaaaaaa96", "aaaaaaaaaaaaaaaa97", "aaaaaaaaaaaaaaaa98", "aaaaaaaaaaaaaaaa99", "aaaaaaaaaaaaaaaa100" };
            for ( int i = 1; i <= 100; i++ ) {
                comboBox.addItem( output[i] );
            }
        }
        // Column1.setCellEditor(new DefaultCellEditor(comboBox));
        Column1.setCellEditor( new MyComboBoxEditor(comboBox) );
        // Column1.setCellRenderer(new DefaultTableCellRenderer());
        Column1.setCellRenderer( comboBox );

        table.repaint();
        table.revalidate();
    }

    public static void main(String[] args) {
        new ErrorDemo();
    }
}

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
  // public MyComboBoxRenderer(String[] items) {
  public MyComboBoxRenderer() {
    // super(items);
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      super.setBackground(table.getSelectionBackground());
    } else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }
    setSelectedItem(value);
    return this;
  }
}

class MyComboBoxEditor extends DefaultCellEditor {
    // public MyComboBoxEditor(String[] items) {
    public MyComboBoxEditor( JComboBox items ) {
    // public MyComboBoxEditor( MyComboBoxRenderer items ) {
        // super(new JComboBox(items));
        super( items );
    }
}

使用新代码并且DB = true,该字段在完成加载时被覆盖。我可以看到它开始于&#34; aaaaaaaaaaaaaaaa2&#34;然后当出现下拉按钮时,它变为&#34; aaaaaaaaaaaaaaaa1&#34;。如果我更改一个值,然后单击另一个框和另一个框,那么这些框现在与第一个框相同。

使用新代码并且DB = false,initail值保持不变,但是当我更改一个字段并单击另一个字段时,它将被覆盖。

1 个答案:

答案 0 :(得分:0)

试着看看这个例子:

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    DefaultTableModel model = (DefaultTableModel) table.getModel();

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };

    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyComboBoxEditor(values));
    col.setCellRenderer(new MyComboBoxRenderer(values));
  }
}

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
  public MyComboBoxRenderer(String[] items) {
    super(items);
  }

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    if (isSelected) {
      setForeground(table.getSelectionForeground());
      super.setBackground(table.getSelectionBackground());
    } else {
      setForeground(table.getForeground());
      setBackground(table.getBackground());
    }
    setSelectedItem(value);
    return this;
  }
}

class MyComboBoxEditor extends DefaultCellEditor {
  public MyComboBoxEditor(String[] items) {
    super(new JComboBox(items));
  }
}

你真正需要的是使用TableCellRenderer,因为它使用了JTable中的JComboBox。