Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the value from Jtable to show on JtextFiled with different interface  RSS feed

 
ahmed yousry
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have 2 interface the first one with Jtable and the other with JTextFields. I want to do if the user will click on the row in the Jtable the value of the row will show on the 2nd Interface JtextFields.So the user can update the data.I'm using NetBeans Drag and drop. Can you please give me some tips How to do it? Thanks for your time and help
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You will need a listener which will let you know when the user selects a row. It will be better UX if you disable multiple row selection.
Once selected you will need to present a UI so that the user can edit the values. This UI should be populated with the existing values from the table

Check out http://docs.oracle.com/javase/tutorial/uiswing/components/table.html especially the section titled User Selections.
It is good UX to have only one JFrame instance in your application. For everything else, you should be using modal dialogs. Check out the API docs for JOptionPane which provides built in methods to create and use modal dialogs in an efficient manner
 
ahmed yousry
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Welcome to the Ranch.

You will need a listener which will let you know when the user selects a row. It will be better UX if you disable multiple row selection.
Once selected you will need to present a UI so that the user can edit the values. This UI should be populated with the existing values from the table

Check out http://docs.oracle.com/javase/tutorial/uiswing/components/table.html especially the section titled User Selections.
It is good UX to have only one JFrame instance in your application. For everything else, you should be using modal dialogs. Check out the API docs for JOptionPane which provides built in methods to create and use modal dialogs in an efficient manner


Dear Maneesh,

Thanks for trying to help me. I succeed to show the Jtable row data on the JtextFields but at the same Window (I meant Jtable and JTextFields at the same window). Now When I try to do with 2 different windows it does not work at all. Can you please give me more tips? Thanks
 
ahmed yousry
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again,

I figured out how to do it. Thanks for help and support and if anyone has the problem please write to me and I have guide him how to do it. Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done!
If you can post the solution here, everyone can benefit.

PS. You don't need to quote the previous conversations as they are already visible above. I have edited out the unnecessary quote
 
ahmed yousry
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'm very new in using forums . Here is the code

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import net.proteanit.sql.DbUtils;

/**
*
* @author ASARA
*/
public class suchen extends javax.swing.JFrame {

private PreparedStatement pst = null;
private Connection conn = null;
private ResultSet rs = null;
private Gerichte g;

/** Creates new form suchen */
public suchen(Gerichte g) {
initComponents();
this.setResizable(false);
conn = ConnectDB.connetDB();
this.g = g;
column_name();

}



/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Suchen Gerichte");

jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel1.setText("Ort");

jTextField1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jButton1.setIcon(new javax.swing.ImageIcon("E:\\Java-Project\\Icons\\Search\\iSync-icon.png")); // NOI18N
jButton1.setText("Suchen Starten");
jButton1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTable1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10", "Title 11"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(556, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 871, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
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)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// Search button

String sql = "select GER_IDENT,GER_LGMERK,GER_PLZBEZ,GER_PLZPF,GER_PLZGROSS,GER_ORTKURZ,GER_ORTVOLL,GER_STRASSE,GER_PF,GER_BEZEICH,GER_HAUSANSCH from GERICHTE where GER_ORTKURZ = ?";
pst = conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
rs = pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

} catch (SQLException ex) {
Logger.getLogger(suchen.class.getName()).log(Level.SEVERE, null, ex);
}
column_name();
}

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// Select from table


try {
int row = jTable1.getSelectedRow();
String table_click = (jTable1.getModel().getValueAt(row, 0).toString());

String sql = "select * from GERICHTE where GER_IDENT = '" + table_click + "'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

if (rs.next()) {
g.setIdent(rs.getString("GER_IDENT"));
g.setBezirk(rs.getString("GER_PLZBEZ"));
g.setPostfach(rs.getString("GER_PLZPF"));
g.setPLZGROSS(rs.getString("GER_PLZGROSS"));
g.setORTKURZ(rs.getString("GER_ORTKURZ"));
g.setORTVOLL(rs.getString("GER_ORTVOLL"));
g.setSTRASSE(rs.getString("GER_STRASSE"));
g.setPF(rs.getString("GER_PF"));
g.setBEZEICH(rs.getString("GER_BEZEICH"));


}

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(suchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(suchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(suchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(suchen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!