Java JTextField无法在Jpanel中输入文本

时间:2017-11-03 10:05:58

标签: java swing

关于在我的Jpanel中使用JTextField,我遇到了麻烦。 主文件是Jframe,里面有一个Jpanel,当我按下一个按钮时,它将Jpanel更改为另一个带有JTextField和Jtable的,JTextField是唯一一个不工作的人,我不能把文字放在里面......这就像是不可编辑的。这是我从主面板到其他面板的代码

 private void newClienteActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        // TODO add your handling code here:
        mainPanel.setVisible(false);
        UserJPanel users;
        users = new UserJPanel(this, mainPanel);
        visiblePanel.setVisible(false);
        visiblePanel = users;
        users.setSize(800, 600);
        getContentPane().setLayout(null);
        getContentPane().add(users);
        users.setVisible(true);
        users.repaint();

    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}                  

它会打开另一个jpanel,这是jTextField无法工作的那个

public class UserJPanel extends javax.swing.JPanel {

private JPanel previousPanel = null;
JFrame parent = null;
private Contactos c;
private final JPanel visiblePanel = null;
Contactos users = new Contactos();
ContactosService userService = new ContactosService();

public UserJPanel(JFrame fparent, JPanel pPanel) throws IOException {
    initComponents();
    this.parent = fparent;
    this.previousPanel = pPanel;
    limparJTable();
    preencherTabelaUsers();
}

public void limparJTable() {
    DefaultTableModel model2 = (javax.swing.table.DefaultTableModel) tabela1.getModel();
    model2.setRowCount(0);
}

public void preencherTabelaUsers() throws IOException{
    final RowPopup pop = new RowPopup(this.tabela1);
    this.tabela1.setRowHeight(this.tabela1.getRowHeight() + 10);
    DefaultTableModel tabelaFuncoes = (DefaultTableModel)this.tabela1.getModel();
    final TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(tabelaFuncoes);
    this.tabela1.setRowSorter(sorter);
    tabelaFuncoes.setNumRows(0);
    try {
        List<Contactos> usersDados = this.userService.findAll();
        usersDados.stream().forEach(b -> {
            tabelaFuncoes.addRow(new Object[]{b.getIdcontactos(), b.getNome(), b.getApelido(), b.getCc(), b.getNif(), b.getTelefone(),b.getEmail()});
        }
        );
    }
    catch (Exception erro) {
        JOptionPane.showMessageDialog(null, erro);
    }
}


/**
 * 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() {

    UsersPanel = new javax.swing.JPanel();
    filterFxText = new javax.swing.JTextField();
    searchBT = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    tabela1 = new javax.swing.JTable();

    setMaximumSize(new java.awt.Dimension(750, 500));
    setPreferredSize(new java.awt.Dimension(750, 500));

    UsersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados Clientes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
    UsersPanel.setMaximumSize(new java.awt.Dimension(700, 500));
    UsersPanel.setMinimumSize(new java.awt.Dimension(700, 500));
    UsersPanel.setPreferredSize(new java.awt.Dimension(700, 500));

    filterFxText.setText("Insira um nome");

    searchBT.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/search_16_16.png"))); // NOI18N
    searchBT.setToolTipText("Procurar");

    tabela1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null}
        },
        new String [] {
            "ID", "Nome", "Apelido", "CC", "NIF", "Telefone", "Email"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, true, true, true, true, true, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    tabela1.setColumnSelectionAllowed(true);
    jScrollPane1.setViewportView(tabela1);

    javax.swing.GroupLayout UsersPanelLayout = new javax.swing.GroupLayout(UsersPanel);
    UsersPanel.setLayout(UsersPanelLayout);
    UsersPanelLayout.setHorizontalGroup(
        UsersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(UsersPanelLayout.createSequentialGroup()
            .addGroup(UsersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(UsersPanelLayout.createSequentialGroup()
                    .addComponent(filterFxText, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(searchBT, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                .addComponent(jScrollPane1))
            .addContainerGap())
    );
    UsersPanelLayout.setVerticalGroup(
        UsersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(UsersPanelLayout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(UsersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(filterFxText)
                .addComponent(searchBT, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(31, 31, 31))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(UsersPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(UsersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    UsersPanel.getAccessibleContext().setAccessibleParent(UsersPanel);
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JPanel UsersPanel;
private javax.swing.JTextField filterFxText;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton searchBT;
private javax.swing.JTable tabela1;
// End of variables declaration                   

}

用红色矩形检查图像,我可以插入任何文本

enter image description here

任何帮助?

更新

唯一不工作的部分是jTextField,Jtable工作正常,检查图像

enter image description here

检查jTextField,我已经删除了toolTipText,现在是空的,但是当程序运行时它是不可能的... 关于布局,当我将元素放入Jpane

时,会自动生成initcomponents()方法

那有什么不对?

0 个答案:

没有答案