JComboBox error blocking another field display - java

I have a problem with JComboBox. It's blocking another field in a frame meant to display another value that is linked through a foreign key in a database, a value that depends on the combo box already pop out. But somehow, after the user clicks the combo box it is blocking another field. The app is using MySql.
Here's the code:
ComboBox method to fill value from DB
public void comboBoxBerat(){
DbConnection DB = new DbConnection();
DB.DbConnection();
con = DB.con;
stat = DB.stmt;
try {
sql = "SELECT * FROM weight_of_services";
rs = stat.executeQuery(sql);
while (rs.next()) {
jComboBoxBerat.addItem(rs.getString("weight"));
}
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
Action when selection is made from combo box, its get the value that linked with foreign key
private void jComboBoxBeratActionPerformed(java.awt.event.ActionEvent evt) {
DbConnection DB = new DbConnection();
DB.DbConnection();
con = DB.con;
stat = DB.stmt;
String item = (String)jComboBoxBerat.getSelectedItem();
String sql = "SELECT price FROM weight_of_services WHERE weight = ?";
try {
pst = con.prepareStatement(sql);
pst.setString(1, item);
rs = pst.executeQuery();
if (rs.next()) {
String price = rs.getString("price");
txtHargaBerat.setText(price); //put value from combobox to txtField
}
con.close();
} catch (Exception e) {
}
}
The problem after I select from the box its blocking another field.
This is the combo box problem in the frame

It's solved. This because i am using jpanel for customizing color for the frame. Its just so messed up with the stacking one panel on top of the other one. That's why my dropdown list get blocked. So what I did, removed all the panel. Custom and sorting it more carefully and tidy from the start.

Related

Autofill a textfield when clicking a value in jComboBox

Good evening all!
I've been working on a simple warehouse/inventory management system which is connected to a mySql database. One of the tables in that database (OESCODE) has information about which area ("AREA") of the warehouse an article is in, and a corresponding numeric value ("CODE"). Example, small goods would be 1, long goods would be 2 etc etc.
In the GUI, i've a jCombobox that allows you to select the area, and the idea is to have a textfield below display the corresponding numeric value.
Here's a screenshot of what I mean:
Screenshot 1
It's the fields labeled Hal (halCombo) and OES (oesTxt) Code.
The corresponding code:
private void pickHalComboPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
String fill = (String) halCombo.getSelectedItem();
String query = "SELECT * FROM OESCODE WHERE AREA=?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, fill);
rs = pst.executeQuery();
if (rs.next()) {
String code = rs.getString("CODE");
oesTxt.setText(code);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
Here's the catch though: the fields above (labeled Artiekelcode, Omschrijving and Eenheid) do EXACTLY the thing i'm describing above. Example, when i click a value in the Artiekelcode field, the Omschrijving and Eenheid fields get autofilled with the corresponding values (from a different database table, ofcourse).
Here's the code for that:
private void pickArticleComboPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
String fill = (String) pickArticleCombo.getSelectedItem();
String query = "SELECT * FROM STAMDATA WHERE ARTIKELNUMMER=?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, fill);
rs = pst.executeQuery();
if (rs.next()) {
String desc = rs.getString("OMSCHRIJVING");
String eenh = rs.getString("EENHEID");
eenheidTxt.setText(eenh);
pickDescriptionTxt.setText(desc);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
So for the past hours, I've been trying to figure out why it works on the fields above and not on the fields below, since both principles are the same.
Any thoughts would be highly appreciated.
Thanks in advance!

JComboBox only show one item from database

I'm trying to display database item into a JComboBox and this is my code.
public static void checkItemName(){
Connection conn = SQLite.SQLite();
String sql = "select itemname from item";
try{
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
String list = resultSet.getString("itemname");
purcItemName.addItem(list);
conn.close();
}
} catch (SQLException lol){
System.out.println(lol.toString());
}
}
I did declare static JComboBox purcItemName; and purcItemName = new JComboBox();
The method/function will be called then user press login button.
The problem I'm having now is that, it only shows one item while my database has multiple items.
Anyone got an idea why?
Vector v = new Vector();
while (resultSet.next()){
String list = resultSet.getString("itemname");
v.add(list);
}
conn.close();
purcItemName.setModel(new DefaultComboBoxModel(v));
store the data you got from database in a vector object and once that is completed set the vector object in your combobox as a new model. try this one
and don't close the connection inside your loop.
you are closing connection inside resultSet.next() check, put the conn.close() outside, to the end

How can I insert data into JComboBox using JTextField

I have write program that program have 2 frame one frame to add item and save to database
in another frame I have to select the item which is add in first frame in JComboBox I write that code for insert data into database but I don't know how to display in JComboBox
try {
String host = "jdbc:derby://localhost:1527/PROCAT";
String uName = "zain";
String uPass = "zain";
con = DriverManager.getConnection(host, uName, uPass);
String m = "insert into ITEMB (ITEM,ITEMNAME,PRISE) values(?,?,?)";
ps = con.prepareStatement(m);
ps.setInt(1, Integer.parseInt(jTextField3.getText()));
ps.setString(2, jTextField2.getText());
ps.setString(3, jTextField4.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "record saved");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
This for JComboBox:
try
{
String host = "jdbc:derby://localhost:1527/PROCAT";
String uName = "zain";
String uPass = "zain";
con = DriverManager.getConnection(host,uName,uPass);
st = con.createStatement();
rs = st.executeQuery("select ITEMNAME from ITEMB;");
while (rs.next()) {
jCom.addItem(rs.getString("ITEMNAME"));
}
}
catch(Exception ex) {
JOptionPane.showMessageDialog(null,ex);
}
On my understanding, there are 2 frames. One of them is for viewing records and has the JComboBox, the other one is for inserting records and has the JTextField.
The default frame would be the one with the JComboBox. If you want to add a new record into the JComboBox, a frame will appear for adding of records.
Once the INSERT statements are issued, the frame with the JTextField will be closed and the frame with the JComboBox will appear showing the newly added records.
You can provide a Reload button for deleting the items inside the JComboBox and then reloading the data using the SELECT statement.
while (rs.next()) {
jComboBox.addItem(rs.getString("ITEMNAME"));
}
And then, use
jComboBox.repaint();

Populating combobox in CodeNameOne

I'm having a slight problem when populating a combobox from the column of a database. below is my code:
protected void initComboBoxModel(final ComboBox cmp) {
try {
String sql = "SELECT * FROM stockinfo";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
Vector vec = new Vector();
while (rs.next()) {
String item = rs.getString("Parts");
Hashtable h = new Hashtable();
h.put("cmp", item);
vec.addElement(h);
cmp.setModel(new DefaultListModel(vec));
}
} catch (Exception ex) {
Dialog.show("Error", "initComboBoxModel count not populate the combo box.", "OK", null);
}
}
The combobox populates but there is unnecessary text in each option in the combobox, for example:
an option which should say "Hello" says "{cmp = Hello}".
How do i stop this from happening? it occurs for every item in the combobox.
Thanks in advance:)
Marko
The problem you are facing is that you are creating a Vector<HashTable> so when you are populating the combobox the default renderer takes toString() method from each hashtable.
I don't know why you need a hashtable, but that's the problem why you are stuck.
Im not familiar with codeNameOne but in swing JComboBox by default uses a renderer wich uses toString() method to display object data. So you can make your own renderer class to customize the view.
UPDATE
I modify your code and comment code, assuming you are using java 1.5 or above.
protected void initComboBoxModel(final ComboBox cmp) {
try {
String sql = "SELECT * FROM stockinfo";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
Vector<String> vec = new Vector<String>(); // use generics
while (rs.next()) {
String item = rs.getString("Parts");
vec.addElement(item);
}
cmp.setModel(new DefaultListModel(vec)); // here you set the model
} catch (Exception ex) {
Dialog.show("Error", "initComboBoxModel count not populate the combo box.", "OK", null);
}
}
Now it's gonna to work you don't have to use any renderer cause you add Strings so toString method is fine in this case.
It won't work for you on the device, you changed the classpath to add JDBC which you shouldn't have.
You should use a renderer or a MultiList to have the hashtable entries render correctly.

How to populate JList with data from another JList

I have a MySQL database which contains data i would like to populate into a JList in my java program. I have two JList, one which is fill with Events Title and the second is to be fill with Guest Name.
What i would like is when the user click on any of the Events Title, the second JList will show all the Guest Name that belong to that Event.
I have already successfully populate the first JList with all the Events Title. What I'm having trouble with is when the user click on the Events Title, the Guests Name will show twice on the second JList. How can i make it to show only once?
Here is what i got so far...
Java Class
private JList getJListEvents() {
if (jListEvents == null) {
jListEvents = new JList();
Border border = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.black, Color.black), "Events", TitledBorder.LEFT, TitledBorder.TOP);
jListEvents.setBorder(border);
jListEvents.setModel(new DefaultListModel());
jListEvents.setBounds(new Rectangle(15, 60, 361, 421));
Events lEvents = new Events();
lEvents.loadEvents(jListEvents);
jListEvents.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
EventC eventC = new EventC();
//eventC.MonitorRegDetailsInfo(jListEvents, jTextFieldEventName, jTextFieldEventVenue, jTextFieldEventDate, jTextFieldEventTime, jTextAreaEventDesc);
//eventC.MonitorRegPackageInfo(jListEvents, jTextFieldBallroom, jTextFieldBallroomPrice, jTextFieldMeal, jTextFieldMealPrice, jTextFieldEntertainment, jTextFieldEntertainmentPrice);
eventC.MonitorRegGuest(jListEvents, jListGuest);
}
});
}
return jListEvents;
}
Controller Class
public void MonitorRegGuest(JList l, JList l2){
String event = l.getSelectedValue().toString();
Events retrieveGuest = new Events(event);
retrieveGuest.loadGuests(l2);
}
Class with all the sql statement
public void loadGuests(JList l){
ResultSet rs = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
MySQLController db = new MySQLController();
db.getConnection();
String sqlQuery = "SELECT MemberID FROM event WHERE EventName = '" + EventName + "'";
try {
rs = db.readRequest(sqlQuery);
while(rs.next()){
MemberID = rs.getString("MemberID");
}
} catch (SQLException e) {
e.printStackTrace();
}
String sqlQuery2 = "SELECT GuestListID FROM guestlist WHERE MemberID = '" + MemberID + "'";
try {
rs2 = db.readRequest(sqlQuery2);
while(rs2.next()){
GuestListID = rs2.getString("GuestListID");
}
} catch (SQLException e) {
e.printStackTrace();
}
String sqlQuery3 = "SELECT Name FROM guestcontact WHERE GuestListID = '" + GuestListID + "'";
try {
rs3 = db.readRequest(sqlQuery3);
while(rs3.next()){
((DefaultListModel)l.getModel()).addElement(rs3.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
db.terminate();
}
Thanks in advance!
Please for the time being forget about the SQL. Then follow the steps below:
Start with the example --- the corresponding Java Web Start link.
Play with the sample, to see how to add elements to a list.
Then all you need to do is grab a reference to the one you want to copy and for each element copy it then add to the 2nd list.
Enjoy Java.
EDIT:
I am still looking at your code and I am still confused of what is going on (Please consider making a SSCCE). But let me guess, as something is telling me that when you are calling the ListSelectionListener you are not using getIsValueAdjusting() of the ListSelectionEvent, right? Therefore, this might be as well it, for more read here.
As far as I can see, you aren't clearing the list before populate it, you could try to clear the model first, like:
String sqlQuery3 = "SELECT Name FROM guestcontact WHERE GuestListID = '" + GuestListID + "'";
try {
rs3 = db.readRequest(sqlQuery3);
DefaultListModel lm = (DefaultListModel)l;
lm.clear();
while(rs3.next()){
(lm.getModel()).addElement(rs3.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}

Categories

Resources