我有两个JFrame,第一个用于使用JTable显示SQL表,第二个用于更新SQL表上的数据。在第一帧,有一个用于显示第二帧的按钮,它有单选按钮。但是,我无法将其设置为true
。我想要发生的是根据我从Label中获得的值来将单选按钮设置为true,其中Label的值来自数据库。这就是我所做的:
第一个JFRAME:
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String hours = null;
try
{
DbUpdate up = new DbUpdate();
// To connect on SQL and get the JTable's value
int get = (int)jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
String query= "SELECT * FROM roominfo WHERE CustomerNo = '"+get+"' " ;
String url = "jdbc:mysql://localhost:3306/adve";
Connection conn = DriverManager.getConnection(url,"root","sa");
Statement st = conn.createStatement();
rs = st.executeQuery(query);
while(rs.next())
{
hours= rs.getString("Hours"); // This is where I can get the value for hours and to be passed on a label
}
up.jLabel12.setText(hours); //I set on a Jlabel for the next frame
}
catch(SQLException e){
JOptionPane.showMessageDialog(null, "Please select a record to update");
}
}
第二个JFRAME:
public void set(){ //THIS SUPPOSE TO SET THE BUTTONS BASED ON THE VALUE OF THE LABEL
if (jLabel12.getText().equals("12-Hours")) { // if 12-Hours, Rdn12 should be true or selected
Rdn12.isSelected();
Rdn12.setSelected(true);
Rdn24.setSelected(false);
}
else if (jLabel12.getText().equals("24-Hours")) { // if 24-Hours, Rdn24 should be true or selected
Rdn12.setSelected(false);
Rdn24.setSelected(true);
}
jTextField1.setEditable(false);
jLabel20.setVisible(false);
}
但是,仍然无法选择单选按钮。我究竟做错了什么?有什么建议?请帮忙。
答案 0 :(得分:2)
更新:我不知道,我是否完全明白你的问题。这个应用程序是你想要的吗?
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class DatabaseRadioButtons extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DatabaseRadioButtons().setVisible(true);
}
});
}
JTable table = new JTable();
JButton showSecondFrame = new JButton("Show second frame");
SecondFrame secondFrame = new SecondFrame();
public DatabaseRadioButtons() {
table.setModel(new DefaultTableModel(new Object[][] {
new Object[] { "first", "entry" },
new Object[] { "second", "entry" } }, new Object[] { "column",
"names" }));
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return; // ignore this event, if we expect another event
// right after this one
int selectedRow = table.getSelectedRow();
refreshRadioButtonsAccordingToDatabaseValues(selectedRow);
}
});
showSecondFrame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
secondFrame.setVisible(true);
}
});
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
panel.add(showSecondFrame, BorderLayout.SOUTH);
setContentPane(panel);
setSize(400, 300);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
protected void refreshRadioButtonsAccordingToDatabaseValues(int selectedRow) {
String databaseValue;
// put you database SELECT here, instead of this fixed value
System.out.println("Make Database select for row " + selectedRow);
databaseValue = selectedRow == 0 ? "12-Hours" : "24-Hours";
// Choose what to do, according to database values
if (databaseValue.equals("12-Hours")) {
// you can change the fields of the second frame directly in here
secondFrame.Rdn12.isSelected();
secondFrame.Rdn12.setSelected(true);
secondFrame.Rdn24.setSelected(false);
} else if (databaseValue.equals("24-Hours")) {
secondFrame.Rdn12.setSelected(false);
secondFrame.Rdn24.setSelected(true);
}
}
}
class SecondFrame extends JFrame {
JRadioButton Rdn12 = new JRadioButton("Radio 12");
JRadioButton Rdn24 = new JRadioButton("Radio 24");
public SecondFrame() {
setSize(400, 300);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setLocation(100, 100);
JPanel panel = new JPanel(new GridLayout(10, 1));
panel.add(Rdn12);
panel.add(Rdn24);
setContentPane(panel);
}
}
您可以在第二帧中更改标签的值,但不一定非必要。正如我的示例所示,您可以在第一帧的代码中更改第二帧的复选框。
更新refreshRadioButtonsAccordingToDatabaseValues()
方法以从数据库中读取实际数据。
答案 1 :(得分:1)
你应该这样做:
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String hours = null;
try
{
DbUpdate up = new DbUpdate();
int get = (int)jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
String query= "SELECT * FROM roominfo WHERE CustomerNo = '"+get+"' " ;
String url = "jdbc:mysql://localhost:3306/adv";
Connection conn = DriverManager.getConnection(url,"root","sa");
Statement st = conn.createStatement();
rs = st.executeQuery(query);
while(rs.next())
{
hours= rs.getString("Hours");
}
if (hours.equals("12-Hours")) {
// you can change the fields of the second frame directly in here
up.Rdn12.isSelected();
up.Rdn12.setSelected(true);
up.Rdn24.setSelected(false);
} else if (hours.equals("24-Hours")) {
up.Rdn24.isSelected();
up.Rdn12.setSelected(false);
up.Rdn24.setSelected(true);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Please select a record to update");
}
}