Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: setText on a JLabel in Swing issues
    primarykey
    data
    text
    <p>I have an issue setting a <code>JLabel</code>'s text using a method in the class creating the GUI from a different class calling that method. The method to set the <code>JLabel</code> is called outside the GUI but when called from inside the GUI class it works. I have tested the <code>getText()</code> method on the label after it is called from outside the GUI class and it shows that the label has been updated. I get that it is probably a paint issue or update issue with Swing but I'm at a loss of what to do. I have tried <code>repaint()</code> and <code>revalidate()</code> on the label and then panel that it is within. Here is my current code:</p> <pre><code>public void setStatusLabel(String statusEntered) { //Shows the variable statusEntered has been received System.out.println(statusEntered); //Not working status_label.setText(statusEntered); //Used this to check if the label receives the data. It does. String status = status_label.getText(); System.out.println(status); } </code></pre> <p>And the context in which I am calling it. Setups a database connection</p> <pre><code>//GUI Class reference MainWindow mainwindow = new MainWindow(); public void connect(){ Connection conn = null; try { String userName = "root"; String password = ""; String url = "jdbc:mysql://localhost:3306"; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, userName, password); //This works System.out.println("Connection Established"); //The issue is with this guy mainwindow.setStatusLabel("Connection"); } catch(Exception e) { System.err.println("Failed to connect to database"); mainwindow.setStatusLabel("No connection"); } } </code></pre> <p>Any help with this would be awesome or if you have some links to suggestions, that would be awesome too! Thanks for the help.</p> <p>Here is my main:</p> <pre><code>public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainWindow().setVisible(true); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload