Search code examples
javaswingjtextfieldpromptswingx

Why is my left TextField in FlowLayout not showing text prompt but the right one does?


Hi why is my left JTextField not showing the text prompt but my right one does? The JTextField i am talking about is called gname . The JTextFieldwhich is working is called fid .

The Programm is supposed to search for both JTextField values in a sql database as soon as someone presses JButtonSearch. I am using the the Prompt Api included in swingx library.

Database

my problem

Edit: After further testing i found out when i press the Search button both prompts are visible. I dont know why this happens or how.

Here is my Code:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import org.jdesktop.swingx.prompt.PromptSupport;

import com.mysql.jdbc.PreparedStatement;

public class errorbrowser implements ActionListener{

    JFrame frame = new JFrame();
    JPanel contentPanel = new JPanel(new CardLayout());

    private String db_host = "localhost";
    private String db_port = "3306";
    private String db_user = "root";
    private String db_pass = "password";
    private String db_base = "errormanager";

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private java.sql.Connection connection = null;

    errorbrowser(){



        JPanel searchsite = new JPanel(new BorderLayout(5,6));
        searchsite.setBackground(Color.DARK_GRAY);

        JPanel searchbar = new JPanel(new FlowLayout());
        searchbar.setBackground(Color.DARK_GRAY);
    //  searchbar.setBorder(new LineBorder(Color.WHITE, 3));

        JTextField gname = new JTextField(10);
        JTextField fid = new JTextField(10);

        PromptSupport.setPrompt("Gerätebezeichnung",gname);
        gname.setOpaque(false);
        gname.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        gname.setForeground(Color.WHITE);
        gname.setFont(new Font("Dialog BOLD",Font.BOLD, 17));

        PromptSupport.setPrompt("FehlerID",fid);
        fid.setOpaque(false);
        fid.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        fid.setForeground(Color.WHITE);
        fid.setFont(new Font("Dialog BOLD", Font.BOLD, 17));

        searchbar.add(gname);
        searchbar.add(fid);
        searchsite.add(searchbar, BorderLayout.NORTH);

        JButton search = new JButton("Search");
        search.setPreferredSize(new Dimension(100,60));
        search.setMaximumSize(new Dimension(100,60));
        search.setMaximumSize(new Dimension(100,60));
        searchsite.add(search,BorderLayout.CENTER);


        search.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String gerätename = gname.getText();
                String fehlerid = fid.getText();
                System.out.println("suche startet");
                try {
                    PreparedStatement st = (PreparedStatement) connection
                            .prepareStatement("Select gerätebezeichnung, fehlerid from rational where gerätebezeichnung=? and fehlerid=?");

                    st.setString(1, gerätename);
                    st.setString(2, fehlerid);

                    ResultSet results = st.executeQuery();
                    if(results.next()) {
                        System.out.println("Fehler gefunden!");
                    }else {
                        System.out.println("Fehler nicht gefunden");
                    }
                }catch(SQLException sqlException) {
                    sqlException.printStackTrace();
                }
            }
            });

        JPanel textbox = new JPanel();

        contentPanel.add(searchsite);

        frame.add(contentPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,400);
        frame.setVisible(true);

    }


    public static void main(String[] args) {

        errorbrowser app = new errorbrowser();
        try {
            //Verbindung herstellen 
            app.createConnection();

        }catch(SQLException e) {
            System.err.println("SQL Fehler - "+ e.getLocalizedMessage());
            e.printStackTrace();
        }catch(ClassNotFoundException e) {
            System.err.println("Der Datenbank treiber wurde nicht gefunden. - " + e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    public void createConnection() throws SQLException, ClassNotFoundException{
        //Treiber initialisieren
        Class.forName(DRIVER);
        //Url für die Verbindung zu der Datenbank
        String mySqlUrl = "jdbc:mysql://" + db_host + ":" + db_port + "/" + db_base;
        //Verbindung herstellen
        connection = DriverManager.getConnection(mySqlUrl,db_user,db_pass);
    }

    public void showAll(String nameOfTable)throws SQLException{
            String query = "Select * FROM " + nameOfTable;
            java.sql.Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            int columns = rs.getMetaData().getColumnCount();
            System.out.println("Alle Einträge zu den Personen ");
            System.out.println();
            // Ich zeige die Tabellenspaltennamen an
            for(int i = 1;i<=columns;i++) {
                System.out.print(rs.getMetaData().getColumnLabel(i)+"\t\t");
            }
                System.out.println();
                System.out.println();
                //Ich zeige den Inhalt der Tabelle an

            while(rs.next()) {
                for(int j = 1;j<=columns;j++) {
                    System.out.print(rs.getString(j)+"\t\t");
                }
                System.out.println();
            }

            //Ich schließe die Streams wieder und gebe die Tabelle wieder frei
            rs.close();
            stmt.close();
        }
        //Ich schließe die Verbindung zu der Datenbank und gebe de Port am PC frei
    public void closeProgramm() throws SQLException{
            this.connection.close();
        }


    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }



}


Solution

  • The prompt is working but focus is on the left TextBox, which is making it not visible.

    You can keep focus out of the TextBox during the start (like keeping it on JButton)

    After setting your frame, you can keep the focus like :

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,400);
        frame.setVisible(true);
        search.requestFocus();//set your focus on button
    }