Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not edit the text of a JTextField inside a JPanel within a JWindow
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/8Tfzd.png" alt="enter image description here"></p> <p>This is in continuation of my previous question where I asked how to place something in the system tray.<br> After some help from the community, I could do that. However what I am unable to do is to change the text of the <code>JTextField</code> in the <code>JWindow</code>. </p> <p>The <code>JWindow</code> has a <code>JPanel</code> and everything is placed within the <code>JPanel</code>, including the <code>JTextField</code> of <strong>Remind Mt At</strong>. However I am unable to type anything in it even though <code>setEditable(true)</code>.<br> The <code>JTextField</code> receives events properly as it is supposed to be white when the mouse enters and go back to default color when mouse exits. </p> <p>Is there any workaround for this? </p> <hr> <h2> SSCCE </h2> <pre><code>package demo; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.InputStream; import javax.imageio.ImageIO; import javax.sound.sampled.*; import javax.swing.*; import javax.swing.border.BevelBorder; import example.Kernel32; public class SSCCE { JPopupMenu popup = new JPopupMenu(); JMenuItem exit = new JMenuItem("Exit"); JWindow window = new JWindow(); JPanel panel = new JPanel(); int remindMeAt = 55; Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS(); Clip buzzer; AudioInputStream in; JLabel ACLineStatus = new JLabel(); JLabel batteryCharge = new JLabel(); JTextField enterReminder = new JTextField(3); Color defaultColor; String onACPower; String charge; String status; boolean keepLooping = true; boolean doRemind = true; boolean isCharging; boolean aboveThreshold; boolean remindedOnce = false; //------------------------------------------------------------------------------ public static void main(String[] args) { new SSCCE(); } //------------------------------------------------------------------------------ public SSCCE(){ if(SystemTray.isSupported()){ setupGUI(); } } //------------------------------------------------------------------------------ public void setupGUI(){ try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); InputStream in = BatteryBeeperSystemTray.class. getResourceAsStream("/images/battery_smaller.png"); TrayIcon t = new TrayIcon(ImageIO.read(in)); t.setToolTip("BatteryBeeper"); SystemTray.getSystemTray().add(t); }catch(Exception e){ } panel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); defaultColor = window.getBackground(); Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus); onACPower = batteryStatus.getACLineStatusString(); charge = batteryStatus.getBatteryLifePercent(); if(onACPower.equalsIgnoreCase("offline")){ onACPower = "Battery"; }else{ onACPower = "AC Power"; charge = "---"; } ACLineStatus.setText(onACPower); ACLineStatus.setPreferredSize(new Dimension(150,40)); ACLineStatus.setBorder(BorderFactory.createTitledBorder("Operating On")); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 7; gbc.gridheight = 1; gbc.fill = GridBagConstraints.BOTH; panel.add(ACLineStatus,gbc); batteryCharge.setText(charge); batteryCharge.setBorder(BorderFactory.createTitledBorder("Current " + "Charge")); gbc.gridy++; panel.add(batteryCharge,gbc); panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); gbc.gridy++; enterReminder.setEditable(true); enterReminder.requestFocusInWindow(); enterReminder.setText(Integer.toString(remindMeAt)); enterReminder.addMouseListener(new TextHandler()); enterReminder.setBackground(window.getBackground()); enterReminder.setBorder(BorderFactory.createTitledBorder("Remind Me" + " At")); panel.add(enterReminder,gbc); window.add(panel); window.pack(); window.setLocationRelativeTo(null); window.setVisible(true); } //------------------------------------------------------------------------------ public void checkIfReminderChanged(){ //TODO DEFINE } //------------------------------------------------------------------------------ public class TextHandler extends MouseAdapter{ @Override public void mouseEntered(MouseEvent e){ enterReminder.setBackground(Color.WHITE); } //------------------------------------------------------------------------------ @Override public void mouseExited(MouseEvent e){ enterReminder.setBackground(defaultColor); checkIfReminderChanged(); } } //------------------------------------------------------------------------------ } </code></pre> <hr> <h2> What you also need </h2> <pre><code>package example; import com.sun.jna.Native; import com.sun.jna.Structure; import com.sun.jna.win32.StdCallLibrary; import java.util.*; public interface Kernel32 extends StdCallLibrary { public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32", Kernel32.class); /** * @see http://msdn2.microsoft.com/en-us/library/aa373232.aspx */ public class SYSTEM_POWER_STATUS extends Structure { public byte ACLineStatus; public byte BatteryFlag; public byte BatteryLifePercent; public byte Reserved1; public int BatteryLifeTime; public int BatteryFullLifeTime; @Override protected List&lt;String&gt; getFieldOrder() { ArrayList&lt;String&gt; fields = new ArrayList&lt;String&gt;(); fields.add("ACLineStatus"); fields.add("BatteryFlag"); fields.add("BatteryFullLifeTime"); fields.add("BatteryLifePercent"); fields.add("BatteryLifeTime"); fields.add("Reserved1"); return fields; } /** * The AC power status */ public String getACLineStatusString() { switch (ACLineStatus) { case (0): return "Offline"; case (1): return "Online"; default: return "Unknown"; } } /** * The battery charge status */ public String getBatteryFlagString() { switch (BatteryFlag) { case (1): return "High, more than 66 percent"; case (2): return "Low, less than 33 percent"; case (4): return "Critical, less than five percent"; case (8): return "Charging"; case ((byte) 128): return "No system battery"; default: return "Unknown"; } } /** * The percentage of full battery charge remaining */ public String getBatteryLifePercent() { return (BatteryLifePercent == (byte) 255) ? "Unknown" : BatteryLifePercent + "%"; } /** * The number of seconds of battery life remaining */ public String getBatteryLifeTime() { return (BatteryLifeTime == -1) ? "Unknown" : BatteryLifeTime + " seconds"; } /** * The number of seconds of battery life when at full charge */ public String getBatteryFullLifeTime() { return (BatteryFullLifeTime == -1) ? "Unknown" : BatteryFullLifeTime + " seconds"; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("ACLineStatus: " + getACLineStatusString() + "\n"); sb.append("Battery Flag: " + getBatteryFlagString() + "\n"); sb.append("Battery Life: " + getBatteryLifePercent() + "\n"); sb.append("Battery Left: " + getBatteryLifeTime() + "\n"); sb.append("Battery Full: " + getBatteryFullLifeTime() + "\n"); return sb.toString(); } } public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result); } </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