|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
combobox among other things
okay the combobox doesn't seem to work very well at the moment and I can't see the reason. When you try to select something from the combobox that is shown in the queryArea, only the first item is shown and it seems that when I try to select another item that on the first one stays the same. If anyone can help me with this I would appreciate it. if you need more information or a better explanation just let me know thanks.
Code:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NorthWindJDBC extends JFrame
{
static final String JDBC_DRIVER ="sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATABASE_URL = "jdbc:odbc:NorthWind";
static final String DEFAULT_QUERY = "SELECT * FROM Products";
String [] names = { "All Employees","All Products by Category", // labels of queries for combobox
"Maxilaku Products","A specific Employee","Condiment Products" };
JTextArea queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);
JComboBox combobox = new JComboBox(names);
JButton submitButton = new JButton ("Submit Query");
ResultSetTableModel tableModel;
public NorthWindJDBC()
{
super("Display Query Results");
try
{
tableModel = new ResultSetTableModel(JDBC_DRIVER, // where the results of the query is displayed
DATABASE_URL, DEFAULT_QUERY);
queryArea.setWrapStyleWord(true);// where user can type in a query
queryArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane (queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Box box = Box.createHorizontalBox();//the box that holds the combobox and submitbutton
box.add(scrollPane);
box.add(combobox);
box.add(submitButton);
JTable dataTable = new JTable(tableModel);// again datatable to display data
Container c = getContentPane();
c.setLayout(new BorderLayout(5, 5));
c.add("North", box);
c.add("Center", new JScrollPane(dataTable));
String query = null;
int selection = combobox.getSelectedIndex();
//this is where the queries are for the labels in the combobox
{
switch(selection)
{
case 0:
query = "SELECT * FROM Employees";
break;
case 1:
query = "SELECT * FROM Products SORT BY Category";
break;
case 2:
query = "SELECT * FROM Products WHERE Supplier = 'Maxilaku'";
break;
case 3:
query = "SELECT * FROM Orders WHERE Employee = 'Peacock, Margaret'";
break;
case 4:
query = "SELECT * FROM Products WHERE Category = 'Condiments'";
break;
}
}
final String newQuery;
newQuery = query;
combobox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
combobox = (JComboBox)evt.getSource();
// the action takes place when the user clicks on the labels in the combobox
queryArea.setText(newQuery);
}
});
submitButton.addActionListener(// the submitbutton actionlistener
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
try
{
tableModel.setQuery(queryArea.getText());
}
catch (SQLException sqlex)
{
JOptionPane.showMessageDialog(null,
sqlex.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
setSize(500,400);
setVisible(true);
}
catch (ClassNotFoundException cnf)
{
JOptionPane.showMessageDialog(null, cnf.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException sqlex)
{
JOptionPane.showMessageDialog(null,
sqlex.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDataBase();
System.exit(1);
}
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
tableModel.disconnectFromDataBase();
System.exit(0);
}
});
}
public static void main(String[] args)
{
new NorthWindJDBC();
}
}
|
|
#2
|
|||
|
|||
|
Hi. My comboBox is doing the same things as above. I am making a application to input customer information. I have created a street database to fill the comboBox lists. When you select a suburb from the suburb comboBox the list in the street comboBox is filtered so only the streets in that suburb are listed. The code filtered the list but you can not select any items. Instead the first item on the list is selected. I noticed that item1 of the street comboBox selects its self before you make a choice on the suburbBox.
I placed the same code on a button action and it works fine. You select a item from suburb press the button and the list filters perfectly so you can select a street. (see code). I think there must be something out of place with the actionListener code but I don't know how to fix it. Please help us. Thanks. Code:
package customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dx.sql.dataset.*;
import com.borland.dbswing.*;
import com.borland.dx.dataset.*;
import com.borland.dx.text.VariantFormatter;
public class Frame1 extends JFrame {
Variant v = new Variant();
String columnName = "";
String columnValue = "";
VariantFormatter formatter;
JPanel contentPane;
Database database1 = new Database();
QueryDataSet queryDataSet1 = new QueryDataSet();
QueryDataSet queryDataSet2 = new QueryDataSet();
JdbComboBox jdbComboBox1 = new JdbComboBox();
JdbComboBox jdbComboBox2 = new JdbComboBox();
JButton jButton1 = new JButton();
QueryDataSet queryDataSet3 = new QueryDataSet();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(568, 475));
this.setTitle("InputCustomer");
database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor(
"jdbc:borland:dslocal:C:\\Database\\CRESWICK.jds", "Lancaster", "", false,
"com.borland.datastore.jdbc.DataStoreDriver"));
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(databas e1, "SELECT * FROM CUSTOMER", null, true, Load.ALL));
queryDataSet2.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(databas e1, "SELECT * FROM STREETINDEX ", null, true, Load.ALL));
jdbComboBox1.setEditable(true);
jdbComboBox1.setColumnName("MSTNAME");
jdbComboBox1.setDataSet(queryDataSet1);
jdbComboBox1.setBounds(new Rectangle(101, 112, 152, 19));
jdbComboBox1.addActionListener(new Frame1_jdbComboBox1_actionAdapter(this));
jdbComboBox2.setEditable(true);
jdbComboBox2.setColumnName("MSUBURB");
jdbComboBox2.setDataSet(queryDataSet1);
jdbComboBox2.setBounds(new Rectangle(100, 158, 152, 19));
jdbComboBox2.addActionListener(new Frame1_jdbComboBox2_actionAdapter(this));
jButton1.setBounds(new Rectangle(135, 220, 75, 24));
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
queryDataSet3.setSort(new com.borland.dx.dataset.SortDescriptor("sort3", new String[] {"INSUBURB"}, new boolean[] {false, }, null, Sort.UNIQUE));
queryDataSet3.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(databas e1, "SELECT INSUBURB FROM STREETINDEX"));
queryDataSet2.addRowFilterListener(new Frame1_queryDataSet2_rowFilterAdapter(this));
contentPane.add(jdbComboBox2, null);
contentPane.add(jdbComboBox1, null);
contentPane.add(jButton1, null);
queryDataSet1.open();
queryDataSet2.open();
queryDataSet3.open();
queryDataSet1.insertRow(true);
updateList("", "","INSTREET", jdbComboBox1);
suburbList();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jdbComboBox1_actionPerformed(ActionEvent e) {
// This code works OK here to update comboBox2.
// updateList("INSTREET", (String) jdbComboBox1.getSelectedItem(),"INSUBURB", jdbComboBox2);
}
void jdbComboBox2_actionPerformed(ActionEvent e) {
// This code updates comboBox1 but you can't select from the list!
// I Don't know why?
// updateList("INSUBURB", (String) jdbComboBox2.getSelectedItem(),"INSTREET", jdbComboBox1);
}
void jButton1_actionPerformed(ActionEvent e) {
// This code is the same as above but it works on a button!
updateList("INSUBURB", (String) jdbComboBox2.getSelectedItem(),"INSTREET", jdbComboBox1);
}
//Updates the combobox drop down lists
void updateList (String sortColumn,String sortValue,String listColumn,JdbComboBox box){
try {
columnName = sortColumn;
columnValue = sortValue;
Column column = queryDataSet2.getColumn(columnName);
formatter = column.getFormatter();
queryDataSet2.refilter();
}
catch (Exception ex) {
System.err.println("Filter failed");
}
// Gets the String for the list from a queryDataSet
queryDataSet2.first();
String itlist [] = new String [queryDataSet2.getRowCount()];
for (int n = 0; n < queryDataSet2.getRowCount(); n++) {
itlist [n] = (queryDataSet2.getString(listColumn));
queryDataSet2.next();}
box.setItems(itlist);
}
// Gets the Suburb list for comboBox2
void suburbList (){
queryDataSet3.first();
String itlist [] = new String [queryDataSet3.getRowCount()];
for (int n = 0; n < queryDataSet3.getRowCount(); n++) {
itlist [n] = (queryDataSet3.getString("INSUBURB"));
queryDataSet3.next();}
jdbComboBox2.setItems(itlist);
}
//Filters queryDataSet2
void queryDataSet2_filterRow(ReadRow row, RowFilterResponse response) {
try {
if (formatter == null || columnName == null ||
columnValue == null || columnName.length() == 0 ||
columnValue.length() == 0)
response.add();
else {
row.getVariant(columnName, v);
String s = formatter.format(v);
if (columnValue.equals(s))
response.add();
else response.ignore();
}
}
catch (Exception e) {
System.err.println("Filter failed");
}
}
}
class Frame1_jdbComboBox1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jdbComboBox1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jdbComboBox1_actionPerformed(e);
}
}
class Frame1_jdbComboBox2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jdbComboBox2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jdbComboBox2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame1_queryDataSet2_rowFilterAdapter implements com.borland.dx.dataset.RowFilterListener {
Frame1 adaptee;
Frame1_queryDataSet2_rowFilterAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void filterRow(ReadRow readRow, RowFilterResponse rowFilterResponse) {
adaptee.queryDataSet2_filterRow(readRow, rowFilterResponse);
}
}
|
|
#3
|
|||
|
|||
|
[Do not use queryArea.setText()//
instead use queryArea.addItem()-->use this code Code:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NorthWindJDBC extends JFrame
{
static final String JDBC_DRIVER ="sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATABASE_URL = "jdbc:odbc:NorthWind";
static final String DEFAULT_QUERY = "SELECT * FROM Products";
String [] names = { "All Employees","All Products by Category", // labels of queries for combobox
"Maxilaku Products","A specific Employee","Condiment Products" };
JTextArea queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);
JComboBox combobox = new JComboBox(names);
JButton submitButton = new JButton ("Submit Query");
ResultSetTableModel tableModel;
public NorthWindJDBC()
{
super("Display Query Results");
try
{
tableModel = new ResultSetTableModel(JDBC_DRIVER, // where the results of the query is displayed
DATABASE_URL, DEFAULT_QUERY);
queryArea.setWrapStyleWord(true);// where user can type in a query
queryArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane (queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Box box = Box.createHorizontalBox();//the box that holds the combobox and submitbutton
box.add(scrollPane);
box.add(combobox);
box.add(submitButton);
JTable dataTable = new JTable(tableModel);// again datatable to display data
Container c = getContentPane();
c.setLayout(new BorderLayout(5, 5));
c.add("North", box);
c.add("Center", new JScrollPane(dataTable));
String query = null;
int selection = combobox.getSelectedIndex();
//this is where the queries are for the labels in the combobox
{
switch(selection)
{
case 0:
query = "SELECT * FROM Employees";
break;
case 1:
query = "SELECT * FROM Products SORT BY Category";
break;
case 2:
query = "SELECT * FROM Products WHERE Supplier = 'Maxilaku'";
break;
case 3:
query = "SELECT * FROM Orders WHERE Employee = 'Peacock, Margaret'";
break;
case 4:
query = "SELECT * FROM Products WHERE Category = 'Condiments'";
break;
}
}
final String newQuery;
newQuery = query;
combobox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
combobox = (JComboBox)evt.getSource();
// the action takes place when the user clicks on the labels in the combobox
queryArea.addItem(newQuery);
}
});
submitButton.addActionListener(// the submitbutton actionlistener
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
try
{
tableModel.setQuery(queryArea.getText());
}
catch (SQLException sqlex)
{
JOptionPane.showMessageDialog(null,
sqlex.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
setSize(500,400);
setVisible(true);
}
catch (ClassNotFoundException cnf)
{
JOptionPane.showMessageDialog(null, cnf.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException sqlex)
{
JOptionPane.showMessageDialog(null,
sqlex.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDataBase();
System.exit(1);
}
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
tableModel.disconnectFromDataBase();
System.exit(0);
}
});
}
public static void main(String[] args)
{
new NorthWindJDBC();
}
}
|
|
#4
|
|||
|
|||
|
try this code hope it will properly your combobox!!!!!!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > combobox among other things |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|