2013-04-05 49 views
1

我在使用帶有RMI的表時遇到了一個很奇怪的問題。 客戶端是一個時隙預訂系統的實現,我已經將 實現爲一個表。JTextField和RMI的GUI問題Java

我面臨兩個問題。 第一個在更改完成後導致表更新。 溶液似乎

private void cleanUp() { 

    panel.removeAll(); 
    panel.setVisible(false); 
    panel.revalidate(); 
    showTable(); 
} 

這確實出現了工作。 (或者可能導致我的問題,我不知道)

我現在的問題是用方法 內的JTextField調用實際預訂。

private JTextField txtClientname; 

txtClientname = new JTextField(); 
txtClientname.setText("ClientName"); 

然後在確認按鈕偵聽 -

callBookingSlot(buttonAction, txtClientname.getText()); 

真正奇怪的是,這部作品最初一次。通過工作 我的意思是從JTextField中提取正確的值到表中。 第一次,它將把用戶輸入到該字段的值放入。 任何後續行爲,它只會放在字符串「ClientName」

任何人有任何想法?這個問題似乎不是RMI相關的,我 嘗試沒有RMI,從文本字段仍然 行爲採取同樣的方式。 我知道我可能應該看fireTableUpdated等,我是,但它會是 偉大的,如果這是那些容易修復之一。

編輯 - 更多信息

import java.awt.EventQueue; 
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.JTable; 
import javax.swing.JTextField; 
import javax.swing.ListSelectionModel; 

public class StackOverFlowGUI { 

private static JFrame frame; 
private static JTable table; 
private static JPanel panel; 
private JTextField txtClientname; 
private static JFrame bookingPopup = new JFrame(); 

public static void main(String[] args) { 

    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      StackOverFlowGUI window = new StackOverFlowGUI(); 
      window.frame.setVisible(true); 
     } 
    }); 
} 

public StackOverFlowGUI() { 
    initialize(); 
} 

private void initialize() { 
    panel = new JPanel(); 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 700, 751); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 
    panel.setBounds(10, 11, 674, 576); 

    frame.getContentPane().add(panel); 
    showTable(); 
} 

private void showTable() { 

    table = new JTable(); 
    panel.add(table); 
    panel.setVisible(true); 
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    table.setCellSelectionEnabled(true); 
    showBookingPopup(2, 2); 
} 

private void showBookingPopup(int row, int col) { 

    bookingPopup.setBounds(100, 100, 220, 185); 
    bookingPopup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    bookingPopup.getContentPane().setLayout(null); 
    txtClientname = new JTextField(); 
    txtClientname.setText("ClientName"); 
    txtClientname.setBounds(10, 11, 184, 20); 
    bookingPopup.getContentPane().add(txtClientname); 
    txtClientname.setColumns(10); 
    bookingPopup.setVisible(true); 

    JPanel panel = new JPanel(); 
    panel.setBounds(10, 65, 184, 33); 
    bookingPopup.getContentPane().add(panel); 

    JButton btnSubmit = new JButton("Submit"); 
    btnSubmit.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      //Here - works first time 
      System.out.println(txtClientname.getText()); 
      //Continues to work if I don't call cleanUp - but then main window will not update 
      cleanUp(); 
     } 
    }); 
    btnSubmit.setBounds(10, 113, 89, 23); 
    bookingPopup.getContentPane().add(btnSubmit); 

} 

private void cleanUp() { 

    panel.removeAll(); 
    panel.setVisible(false); 
    panel.revalidate(); 
    showTable(); 
} 

}

+1

你應該看發佈的SSCCE。 – camickr 2013-04-05 02:58:57

+1

你能否提供顯示問題的[SSCCE](http://sscce.org/)(最好沒有RMI) – MadProgrammer 2013-04-05 02:59:00

+1

我同意 - 我認爲我們需要看到更多代碼才能更好地理解發生了什麼事情。我懷疑是線程問題,也可能是一個參考問題。 – 2013-04-05 02:59:27

回答

1

您發佈的代碼中有幾個主要問題包括:

  • 使用靜態字段,其中實例字段應該用
  • 使用null佈局和setBounds(...)並避免使用Swing佈局管理器
  • 使用多個JFrames,您應該使用JDialogs
  • 但最重要的是,不必要地重新創建變量和組件。如果您需要清除字段,請清除字段,但不要簡單地丟棄它們。

例如:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class DataIntoTable extends JPanel { 

    private static final String[] COL_NAMES = {"First Name", "LastName"}; 
    private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0); 
    private JTable table = new JTable(model); 
    private JTextField firstNameField = new JTextField(10); 
    private JTextField lastNameField = new JTextField(10); 
    private JPanel dialogPanel = new JPanel(); 

    public DataIntoTable(final JFrame frame) { 
     setLayout(new BorderLayout()); 
     add(new JScrollPane(table)); 

     dialogPanel.add(new JLabel("First Name:")); 
     dialogPanel.add(firstNameField); 
     dialogPanel.add(new JLabel("Second Name:")); 
     dialogPanel.add(lastNameField); 

     dialogPanel.add(new JButton(new AbstractAction("Submit") { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      String[] rowData = {firstNameField.getText(), lastNameField.getText()}; 
      model.addRow(rowData); 
      firstNameField.setText(""); 
      lastNameField.setText(""); 
     } 
     })); 

     JDialog dialog = new JDialog(frame, "Enter Name", false); 
     dialog.getContentPane().add(dialogPanel); 
     dialog.pack(); 
     dialog.setLocationRelativeTo(null); 
     dialog.setVisible(true); 

    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Data Into Table"); 
     DataIntoTable mainPanel = new DataIntoTable(frame); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

非常有趣,謝謝! – Saf 2013-04-06 16:38:13

+0

@Saf:不客氣! – 2013-04-06 16:46:59