My problem is to update jTextField1 (update province) in class Province.java. The first GUI showing up is AddAddress which contains 4 fields ( jTextField ) to be filled in : province ( jTextField1), district(2), ward(3) . Each time double-click on jTextField1 or 2 or 3 or 4 a new JFrame appears showing province, district or ward list. After clicking, it returns back to the first GUI and the province/district/ward clicked is updated.
Asked
Active
Viewed 74 times
0
-
private void jList1MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: JList theList = (JList) evt.getSource(); if(evt.getClickCount() == 2){ int index = theList.locationToIndex(evt.getPoint()); Object object = theList.getModel().getElementAt(index); } this.setVisible(false); new AddAddress().setVisible(true); } – ledinhhoangson May 05 '16 at 15:46
-
In class AddProvince . I use jpa to create class from the sql database – ledinhhoangson May 05 '16 at 15:47
1 Answers
1
Do not use JFrame
in situations like that. Actually using multiple frames in one application is a bad practice. Please read here: The Use of Multiple JFrames: Good or Bad Practice?.
I suggest you to use a JDialog
to select province etc.
Here is a simple example:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
public class JDialogDemo extends JFrame {
public JDialogDemo() {
final MyDialog dialog = new MyDialog(this);
final JTextField provinceField = new JTextField(10);
JButton button = new JButton("Show Dialog");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.pack();
dialog.setLocationRelativeTo(JDialogDemo.this);
dialog.setVisible(true);
provinceField.setText(dialog.getSelectedProvince());
}
});
setLayout(new FlowLayout());
add(provinceField);
add(button);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
new JDialogDemo();
}
}
class MyDialog extends JDialog {
private String selectedProvince;
public MyDialog(Frame owner) {
super(owner, true);
setTitle("Provinces");
final JList<String> list = new JList<String>(new String[]{"province1", "province2", "province3"});
add(list);
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedProvince = list.getSelectedValue();
setVisible(false);
}
});
add(okButton, BorderLayout.SOUTH);
}
public String getSelectedProvince() {
return selectedProvince;
}
}
Edit:
Edit your jTextField1MouseClicked
method
private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseClicked
// TODO add your handling code here:
if(evt.getClickCount() == 2){
AddProvince provinceDialog = new AddProvince();
provinceDialog.setVisible(true);
jTextField1.setText(provinceDialog.getSelectedProvince());
}
}
Then make AddProvince
extends from JDialog
. And add a method which return the selected province.
-
-
-
-
I'm a newbie to Java. Can u give me some material about creating Java apps ? I am new to this – ledinhhoangson May 05 '16 at 15:55
-
I think the oracle tutorials are good resources for learn java with swing. https://docs.oracle.com/javase/tutorial/uiswing/index.html – rdonuk May 05 '16 at 15:59
-
Also you can learn how to use swing components like `JDialog` in [here](https://docs.oracle.com/javase/tutorial/uiswing/components/index.html) – rdonuk May 05 '16 at 16:00
-
-
It's my project. I got how to make jdialog but i'm still in trouble with my own problem. Some content in gui is in vietnamese but if u look at the layout, u can understand . – ledinhhoangson May 05 '16 at 16:28
-