im using below coding to add values to a jcombobox using another jcombobox and i need to add the values to the jcombobox2 according to the one get selected in the jcombobox1 without appending values so can someone tell me a way to reset or clear the combo-box values when another option selected?
below is my coding and i'm new to java and netbeans so if someone can help i'll be grateful :)
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database1", "root", "senura123");
Statement stat = (Statement) con.createStatement();
String val=jComboBox1.getSelectedItem().toString();
String check; String col;
if ("Vehicles".equals(val)){
check = "select reg_no from vehicle;";
col="reg_no";
}
else if ("Travel Guides".equals(val)){
check = "select username from travelguide;";
col="username";
}
else{
check = "select username from transportofficer";
col="username";
}
ResultSet rslt = stat.executeQuery(check);
while (rslt.next()) {
jComboBox2.addItem(rslt.getString(col));
}
}
See DefaultComboBoxModel.removeAllElements()
Empties the list.
Set a new model into your combobox:
final List<String> values = new ArrayList<String>();
while (rslt.next()) {
values.add(rslt.getString(col));
}
jComboBox2.setModel(new DefaultComboBoxModel(values.toArray()));
See DefaultComboBoxModel.
As a further comment, however, depending on how much latency is involved in your query, you may like to split this work up into EDT and background thread parts using SwingWorker.
Usually it happens because you have an event associated JComboBox. It is solved if you have control item in the JComboBox to act, for example:
jComboBoxExample.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
do_run ();
}
});
public void do_run() {
int n=jComboBoxPerfilDocumentos.getItemCount(); <--THIS IS THE SOLUTION
if (n> 0) {
String x = jComboBoxPerfilDocumentos.getSelectedItem (). ToString ();
}
}
Related
I've been searching through this website for numerous hours now on how to get my button to an a row to an already existing table, this table created by simply clicking the swing Controls, and adding a table and altering the fields through the properties.
The table's variable name is 'table'.
And when confronted with this line of code:
table.getModel().insertRow(table.getRowCount(),new Object[]{nome[i],data[i]});
The 'insertRow' part is redded and I can't seem to fix it.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String direcdate=direc1.getText();
File folder = new File(direcdate);
File[] listOfFiles=folder.listFiles();
String[] nome = new String[250];
String[] data = new String[250];
int i=0;
for (File listOfFile : listOfFiles) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
if (listOfFile.isFile()) {
nome[i]= listOfFile.getName ();
data[i] =sdf.format(listOfFile.lastModified());
i++;
}
else if (listOfFile.isDirectory()) {
nome[i]= "Folder: " + listOfFile.getName ();
data[i] =sdf.format(listOfFile.lastModified());
i++;
}
}
for(int increm=0;increm<i;increm++)
{
table.getModel().insertRow(table.getRowCount(),new Object[]{nome[i],data[i]});
}
}
Any ideas or suggestions?
EDIT: where the table model is located:
public class GAPAC_TESTE extends javax.swing.JFrame {
public GAPAC_TESTE() {
initComponents();
ultimaalt.setText("0");
jTextPane2.setText("Após escolher a diretoria, escolha uma das opções.");
DefaultTableModel model = new javax.swing.table.DefaultTableModel();
table = new javax.swing.JTable(model);
}
table.getModel().
That method return a TableModel. Did you look at the API for the TableModel interface? It does not contain an insertRow(...) method.
The DefaultTableModel has the insertRow(...) method. So assuming your table is using a DefaultTableModel the code would be:
DefaultTableModel model = (DefaultTableMode)table.getModel();
model.insertRow(...);
Don't always write you code in a single statmentment. Break the statement up into multiple statements so you understand exactly which part of the statement causes the problem and it makes sure you assign the variable to the proper class.
If you implement a TableModel, you will be able to exactly determine how data is added and which data types are displayed in your table.
This Code is in a While Loop, and each time I run a new Query it will go through this block after I have chosen what to filter it by, the problem is when I run it a second time and click on a cell in my table it will revert to cells in my previous table/query. I attached an image to show what I mean(I need 10 reputation for that so nevermind on the picture), I filtered the table by procsessState = -1 and when I clicked on some cells it reverted to what the previous table had. Help would be greatly appreciated. The program is around 1000 lines long and I did a terrible job of splitting it into different classes So I just posted where I am almost certain the issue arises.
I declared
final String columnNamesForTable[] = {"Error Message", "ID", "Locked By", "Message Id", "Process State",
"Row Date", "Sender", "Sent Date", "Subject" };
At The top, then I have this a bit later on.
else if (checkBoxCounter != 0)
{
DefaultTableModel tableModel = new DefaultTableModel(columnNamesForTable, 0);
tableModel.fireTableDataChanged();
try
{
Connection conn = DatabaseConnection.getConnection();
System.out.println("Connected");
PreparedStatement statement = conn.prepareStatement(sb.toString(),
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT);
// Change CONCUR_READ_ONLY to CONCUR_UPDATABLE
ResultSet resultSet = statement.executeQuery();
int errorMessageIndex = resultSet.findColumn("ErrorMessage");
int idIndex = resultSet.findColumn("Id");
int lockedByIndex = resultSet.findColumn("LockedBy");
int messageIdIndex = resultSet.findColumn("MessageId");
int processStateIndex = resultSet.findColumn("ProcessState");
int rowDateIndex = resultSet.findColumn("RowDate");
int senderIndex = resultSet.findColumn("Sender");
int sentDateIndex = resultSet.findColumn("SentDate");
int subjectIndex = resultSet.findColumn("Subject");
while (resultSet.next()) {
Object[] rowsForTable = { resultSet.getString(errorMessageIndex),
resultSet.getString(idIndex), resultSet.getString(lockedByIndex),
resultSet.getString(messageIdIndex), resultSet.getString(processStateIndex),
resultSet.getString(rowDateIndex), resultSet.getString(senderIndex),
resultSet.getString(sentDateIndex), resultSet.getString(subjectIndex)};
tableModel.addRow(rowsForTable);
}
resultSet.close();
statement.close();
conn.close();
filterFrame.setVisible(false);
JTable resultsTable = new JTable(tableModel);
JScrollPane pane = new JScrollPane(resultsTable);
displayPnl.add(pane);
pack();
resultsTable.repaint();
isDone= true;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Database error");
ex.printStackTrace();
isDone = true;
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Error loading database driver");
ex.printStackTrace();
isDone = true;
}
}
This Code is in a While Loop
Why would it be in a while loop. Swing is event driven. Code should only be executed when the uses generates some kind of event like clicking on a button, typing text, editing a cell.
I have chosen what to filter it by,
Don't create a whole new table and scroll pane. Just update the TableModel of the existing JTable.
pack();
Why would you pack the frame. The query could have 100's of rows of data. Pick a reasonable size for the table when the frame is created and there is no need to use pack() or repaint(). When you invoke the setModel(...) method of the JTable to replace the current model the table will be repainted automatically.
So all the code you need should be:
//filterFrame.setVisible(false);
//JTable resultsTable = new JTable(tableModel);
//JScrollPane pane = new JScrollPane(resultsTable);
//displayPnl.add(pane);
//pack();
//resultsTable.repaint();
existingTable.setModel( tableModel );
I cant comment I do not have enough reputation.
You fire tabledatachanged before changing data.
TableDataChanged does not always properly update rows , it seems better to fire update rows.
If your table is editable , if you clicked the table you need to release the editor.
I have a small Java swingui app where I display a JList and the user is able to cut, copy, paste and sort the list.
I use a custom TransferHandler to allow drag and drop on this Jlist. Here is the code in building the JList, it basically builds it from an ArrayList. "lstScripts" is the JList.
ListTransferHandler lh = new ListTransferHandler();
...
DefaultListModel listModelScripts = new DefaultListModel();
for(Script s : scripts) {
listModelScripts.addElement(s.getName());
}
this.lstScripts = new JList(listModelScripts);
this.lstScripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.lstScripts.addListSelectionListener(this);
JScrollPane sp = new JScrollPane(this.lstScripts);
sp.setPreferredSize(new Dimension(400,100));
this.lstScripts.setDragEnabled(true);
this.lstScripts.setTransferHandler(lh);
this.lstScripts.setDropMode(DropMode.ON_OR_INSERT);
setMappings(this.lstScripts);
...
On my custom TransferHandler class, I've got the importData routine working so that it handles the copy/paste/cut/sort.
public boolean importData(TransferHandler.TransferSupport info) {
String scriptname = null; // The script name on the list
//If we can't handle the import, bail now.
if (!canImport(info)) {
return false;
}
JList list = (JList)info.getComponent();
DefaultListModel model = (DefaultListModel)list.getModel();
//Fetch the scriptname -- bail if this fails
try {
scriptname = (String)info.getTransferable().getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException ufe) {
System.out.println("importData: unsupported data flavor");
return false;
} catch (IOException ioe) {
System.out.println("importData: I/O exception");
return false;
}
if (info.isDrop()) { //This is a drop
JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
int index = dl.getIndex();
model.add(index, scriptname);
return true;
} else { //This is a paste
int index = list.getSelectedIndex();
// if there is a valid selection,
// insert scriptname after the selection
if (index >= 0) {
model.add(list.getSelectedIndex()+1, scriptname);
// else append to the end of the list
} else {
model.addElement(scriptname);
}
return true;
}
}
So up to here, everything works fine as far as the GUI. But my problem is I need the original JList "lstScripts" to be automatically updated with the user GUI changes. For example, if the user cuts or reorders the list, I want it to show on in "lstScripts".
I'm not seeing how to make this connection between the TransferHandler and original GUI controller where "lstScripts" resides.
#kleopatra - you helped me! sorry I didnt understand how the model was working.
So in the controller, I create the "lstScripts" JList and add it to my panel (this is the first block of my code above).
pnlScripts.add(lstScripts, BorderLayout.WEST);
And as my code above showed, the listScripts JList had a custom transferhandler set as such:
this.lstScripts.setTransferHandler(lh);
So the transferhandler does all the user dnd (drag and drop) stuff. In the controller, I can get the updated list by doing:
DefaultListModel model = (DefaultListModel)lstScripts.getModel();
for (int i = 0; i < model.getSize(); i++){
scriptnames += model.getElementAt(i).toString() + ",";
}
The scriptnames String variable now contains the updated list.
Thanks!
I want to add rows in JTable, but it didn't work well. Could someone help me? Table is displaying normal but not dynamically
//displays all data in Jtable
void refresh()
{
Vector<Vector<String>> data = new Vector<>();
ResultSet rs = st.executeQuery("SELECT * FROM tblInfo");
while(rs.next())
{
Vector<String> d = new Vector<>();
d.add(rs.getString("ID"));
d.add(rs.getString("Name"));
d.add(rs.getString("User"));
d.add(rs.getString("Pass"));
data.add(d);
}
Vector<String> header = new Vector<>();
header.add("ID");
header.add("Name");
header.add("Username");
header.add("Password");
model = new DefaultTableModel(data, header);
table = new JTable(model);
st.close();
rs.close();
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.white);
scroll = new JScrollPane(table);
getContentPane().add(scroll);
st.close();
rs.close();
}
//adding data to database
void addDoctor()
{
st.executeUpdate("INSERT INTO tblInfo(Name) VALUES ('Name')");
st.close();
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
else if(btnAdd == source)
{
addDoctor();
refresh();
}
Thanks for any response. :)
I have edited this code before i've posted.
1) Don't create any Objects inside try - catch - finally block; for Swing GUI, prepare these Objects before, better as local variables.
2) You created a new
model = new DefaultTableModel(data, header);
table = new JTable(model);
and those Object maybe never added to the already visible GUI. Swing GUI doesn't care somehow, and the container doesn't know that you changed (reset, reinitialize) the underlaying model and with JTable. You have to notify Swing GUI for changes, but this isn't the proper of way.
3) Don't to recreate this Object on runtime, reuse Objects that already exist, create JTable and DefaultTableModel only one time.
4) Reset DefaultTableModel by using model.setRowCount(0); and then to add a new rows from JDBC
5) Don't to reinvent the wheel, search for ResultSetTableModel or TableFromDatabase.
6) Move code lines st.close(); & rs.close(); to the finally block.
Use DefaultTableModel.setDataVector() to add a new Vector with the new Data to the existing TableModel/JTable. Or use the insertRow/removeRow methods. Or implement your own AbstractTableModel.
I'm writing a program using jdbc that will be an interface to database(smth like CRUD aplication). I assume that I have to write a class(e.g. DBCLass) that will do all the operations with database(select, update, insert, delete and maybe some other logic that will be reduced to these operations). User interface consists of a set of tables and a few buttons. To use a Jtable I need to implement a class(e.g Model) which is a subclass of AbstractTableModel. So this class will display my data to the user. I need to implement such model for all tables in my database schema. I don't want to write the logic in that classes that display data to the user and I think it is not very good thing to write the logic code in such classes. But it is also incorrect to load all the data from the db table to memory(e.g. ArrayList) and then display it in Model.
So, I want an advise which is the best way to solve such problem.
edit:
A small example:
Statement stmt = ....;
ResaultSet rs = stmt.executeQuery("SELECT * FROM table1");
javadoc says that executeQuery method returns a ResultSet object that contains the data produced by the given query. So If we have a lot of data(which size is more than permited size to our virtual machine), our program will fail.
So my question is still relevant
Download the source for SQuirreL SQl and have a look at the table implementation.
Some things to note:
Database tables aren't Java JTables. A table in a database is in fact a set (curse the fool who used the wrong term) with items and each item has properties (usually called "columns" which isn't a JColumn which explains why it's so hard to map the two).
A set can grow to any size. It has no intrinsic order. You can do lots of set operations on it like: union, difference, sub set.
Therefore, it's not a table, especially not a UI table.
There is no easy UI paradigm which maps "set" to "table". You can
Load N records and page through the results.
You can load more as the user scrolls down
You can count the size of the set and adjust the scrollbar accordingly. As the user scrolls through the data, it is fetched from the DB and displayed.
Pros + cons:
Solution 1 is most simple to implement and the one which users hate most. Why do they need to wait to see data again when the go backwards? This is especially frustrating if each fetch takes 15 seconds. Page ... wait ... page ... oops! There it was! Damn! Wait wait wait ... back ... wait ... aaargh.
Also databases often have a hard time to page data. For some queries, performance can be disastrous.
Solution 2 is simple to implement, especially if you can keep the ResultSet open forever. But 100% of the time, you can't. It will start to fail if you keep it open for a couple of hours or a day. After that time, the DB will think "oh, it's dead, Jim" and close the connection and your user will get a nice error message and you will get an angry user.
So you need to page here, too, but not as often. On the positive side, users need not wait again for data they already have. One huge point: If the set contains millions of rows, users intuitively understand that they need to attack the problem from a different angle as they scroll down. Eventually, they will get tired and ask for a better solution (instead of being angry at you because your stupid program can't display 15 million rows in less than 0.0000000001s).
Solution 3 is worse than #2, again. If the table grows, the UI will become unusable: Even looking at the scroll know will move you to a random place in the table. So it makes your mouse useless. And your users angry.
So I usually try a solution which:
Reads 1000 rows, max. Plus I stop after 100 rows (so the user has at least some data) if reading the rows takes more than 1 second. Usually, the query is slow and reading the result set takes virtually no time, but I like being defensive here.
On top of each column is a filter and an "order by" which can be mapped directly to SQL. That way, I can make sure that if you want the data sorted by a column, it's sorted by all values (and not only those which you can see on the screen).
This allows users to chop huge amounts of data into meaningful sub sets.
Table From Database has a couple of ideas.
Maks,
Here is another example on generic sql to table implementation:
http://www.oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html
It may be a good place to look for your answer.
Also, this local question/answer may help you with ResultSet size: Java JDBC Lazy-Loaded ResultSet
Hope this helps,
Robert
This blog post explains how to lazy load data into a table model: JTable Bound to a Database With Lazy Loading
You want to use the DBTable component from the QuickTable project.
Check this SO answer with sample code usage.
Here's a class that extract data rows and columns from database.
Look at table = new JTable(rows(), columns());
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PrinterException;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import com.swtdesigner.SwingResourceManager;
import java.util.*;
import java.sql.*;
import javax.swing.*;
public class listing extends JDialog {
private JTable table;
public static Vector rows() {
Vector data = new Vector();
String sql= null;
Connection C;
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
C = (Connection) DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE",
"system", "manager");
Statement stmt = C.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM site ");
ResultSetMetaData md = rset.getMetaData();
int columns = md.getColumnCount();
while (rset.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rset.getObject(i));
}
data.addElement(row);
}
rset.close();
stmt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
//System.out.println("lignes : "+data);
return data;
}
public static Vector columns()
{ Connection c ;
Vector cols = new Vector ();
String sql2=null;
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
c = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE",
"system", "manager");
sql2 = "select * from SITE";
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery(sql2);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
//récupération des noms des colonnes dans la table site
for (int i = 1; i <= columns; i++) {
cols.addElement(md.getColumnName(i));
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
//System.out.println("colonnes ::: "+cols);
return cols;
}
public static void main(String args[]) {
try {
listing dialog = new listing();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public listing() {
super();
getContentPane().setLayout(null);
setTitle("Listing de la table \"SITE\"");
setBounds(100, 100, 500, 363);
setResizable(false);
final JLabel laTablesiteLabel = new JLabel();
laTablesiteLabel.setText("La table \"SITE\" contient . . . ");
laTablesiteLabel.setBounds(10, 34, 274, 16);
getContentPane().add(laTablesiteLabel);
final JLabel label = new JLabel();
label.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/130.png"));
label.setBounds(402, 18, 49, 48);
getContentPane().add(label);
final JButton okButton = new JButton();
okButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
dispose();
setVisible(false);
}
});
okButton.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/33-32x32.png"));
okButton.setText("Ok");
okButton.setBounds(10, 291, 148, 32);
getContentPane().add(okButton);
final JButton refeshButton_1 = new JButton();
refeshButton_1.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/48-32x32.png"));
refeshButton_1.setText("Actualiser");
refeshButton_1.setBounds(171, 291, 148, 32);
getContentPane().add(refeshButton_1);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 85, 474, 187);
getContentPane().add(scrollPane);
table = new JTable(rows(), columns()); // chargement de JTable
scrollPane.setViewportView(table);
final JButton printButton_1_1 = new JButton();
printButton_1_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
try {
table.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
});
printButton_1_1.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/Printer_h.png"));
printButton_1_1.setText("Imprimer");
printButton_1_1.setBounds(336, 291, 148, 32);
getContentPane().add(printButton_1_1);
}
}