What I want is entirely WindowsLookAndFeel, and I have this code in my JFrame.
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
However, what I got it part Windows, part Metal (the cross platform style which looks awful. I think I know where the problem is:
The abnormal part(metal part) is a JPanel, and it is not originally built in the JFrame. I have a button. If pressed, the JPanel will be added to the JFrame.
if (((JToggleButton) e.getSource()).isSelected()) {
getContentPane().add(Console.getInstance(), BorderLayout.EAST);
} else {
remove(Console.getInstance());
}
revalidate();
pack();
But still, I don't know why this happens, and how to solve it.
You need to define the look and feel outside of the EDT. Create a class having a main method. In that method set the LAF and then display the app:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
jFrame.pack();
jFrame.setVisible(true);
}
});
Related
I'm aware this is something that has been asked and I've used some of the solutions/examples provided in the forum but they seem to be unable to work for me.
Essentially I have JInternalFrames which contain JTextAreas and I would like it it that when the JInternalFrame is visible focus i.e. the cursor is placed on the JTextArea as opposed to the user having to manually place the cursor by clicking on the JTextArea.
Below are my attempted solutions:
Solution 1:
textAreaUName.requestFocus(true);
Solution 2:
textAreaUName.requestFocus(true);
textAreaUName.requestFocusInWindow();
Apologies once again if you have seen the question being asked severally.
Thanks for any ideas.
This will set the focus on a JComponent which is not null and focusable.
public final void requestFocus(final JComponent component)
{
Runnable r = new Runnable() {
#Override
public void run() {
while (!component.isFocusOwner()) {
component.requestFocusInWindow();
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
if(component!=null&&component.isFocusable())
{
Executors.newCachedThreadPool().execute(r);
}
}
I am creating an application in Java and I would like that when you minimize to an icon, the application will have to "hide" in the system tray.
The code I use is this: (the significant part of the code)
myFrame = new JFrame();
myFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent e) {
PutTray();
}
#Override
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
}
});
This is a "PutTray" function:
private void PutTray()
{
try
{
tray.add(trayIcon); // Initialized elsewhere
myFrame.setVisible(false);
} catch (AWTException e) {
System.err.println(e);
}
}
To restore (via option in the pop-up menu when you press the icon minimized):
MenuItem show = new MenuItem("Show");
show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myFrame.setVisible(true);
myFrame.setState(JFrame.NORMAL);
tray.remove(trayIcon);
}
});
The code works perfectly on Windows 8, but it does not work on Linux (Kali Linux and even Ubuntu).
Why Windows yes and Linux no?
EDIT:
On Linux, after you press the command to show the application, it appears for a very small moment, and then minimizes again. Basically is triggered the event "windowDeiconified" and immediately after the event "windowIconified" without taking the time to do something else and then the application is shown in the system tray.
As Dan Getz suggests, I also thought the order of setVisible and setState should be inverted since the javadoc for setState says:
If the frame is not visible on the
* screen, the events may or may not be
* generated.
but this didn't help.
The one thing that did help though was replacing setVisible(false) with dispose(). They are similar in that you can use setVisible(true) to reopen a disposed window. You can read more about it here: JDialog setVisible(false) vs dispose()
I'll try to find an explanation and come back with it :)
SSCCE to simulate OP problem:
public class Test {
private JFrame myFrame;
public Test() {
myFrame = new JFrame();
myFrame.setVisible(true);
myFrame.setSize(300, 300);
myFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent e) {
PutTray();
}
});
}
private void PutTray() {
myFrame.setVisible(false); //replace with dispose(); and it's ok
Timer t = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
myFrame.setVisible(true);
}
});
t.setRepeats(false);
t.start();
}
public static void main(String[] args) {
new Test();
}
}
I think you are getting it wrong!
Maybe you are confused about deiconified and visibility
windowIconified()
will be called when we click minimize button
and
windowDeiconified()
is called when we restore it from taskbar and not system tray!
In order to restore from system tray you need to use this
trayIcon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
window.setVisible(true);
}
});
Basically i don't think the difference between dispose() & setVisible() will bother you in this specific problem
Still, my recommendation is to use setVisible() here
I want to create a JFrame instance and on the click of its minimize button, I would like to hide it to the System Tray which is usually the taskbar of windows.
I'd come to know that by using SystemTray class in java.awt package I can do so but neither I'm getting any tutorial on it nor any working program example.
I'd asked this question here to either get the link to tutorial site for SystemTray class or if any body knows how to trap the window minimizing event, a working example.
The WindowListener interface and JFrame's addWindowListener() method should help you determine when the frame has been minimised.
This will trap the window minimized event and will create a tray icon. It will also remove the window from the taskbar and it will add a listener on the tray icon so that a mouseclick would restore the window. The code is a bit scrappy but should be good enough for your learning purposes:
public class Qwe extends JFrame {
public static void main(String[] args) {
final Qwe qwe = new Qwe();
qwe.addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == ICONIFIED) {
try {
final TrayIcon trayIcon = new TrayIcon(new ImageIcon("/usr/share/icons/gnome/16x16/emotes/face-plain.png").getImage());
trayIcon.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
qwe.setVisible(true);
SystemTray.getSystemTray().remove(trayIcon);
}
});
SystemTray.getSystemTray().add(trayIcon);
qwe.setVisible(false);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
});
qwe.setSize(200, 200);
qwe.setVisible(true);
}
}
best way would be create follows
1) SystemTray
2) add JPopopMenu to the SystemTray's Icon
3) set DefaultCloseOperation for TopLevelContainer (in your case JFrame)
by using WindowListener setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
in other cases always works setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
notice don't forget declare System.exit(1) to the SystemTray's JpopupMenu, from JMenuItem or another Action/Event, because in this form currenet JVM never gone from Native OS until PC power-off or restart
private void windowStateChanged(java.awt.event.WindowEvent evt) {
// Use getExtendedstate here.
}
WindowStateListener docs
Frame.getExtendedState() docs
frame.addWindowListener(new WindowAdapter() {#Override
public void windowIconified(WindowEvent e) {}
});
Is there a way to close a JDialog through code such that the Window event listeners will still be notified? I've tried just setting visible to false and disposing, but neither seem to do it.
Closing a window (with dispose()) and hiding it (with setVisible(false)) are different operations, and produce different events -- and closing it from the operating system is yet another different operation that produces yet a different event.
All three will produce windowDeactivated to tell you the window's lost focus, but dispose() will then produce windowClosed, while closing from the OS will first produce windowClosing. If you want to handle both of these the same way, you can set the window to be disposed when closed:
window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
In general, setVisible(false) implies that you might want to use the window again, so it doesn't post any window events (apart from windowDeactivated). If you want to detect the hiding of a window, you need to use a ComponentListener;
window.addComponentListener(new ComponentAdapter() {
#Override
public void componentHidden(ComponentEvent e) {
System.out.println("componentHidden()");
}
})
Note though that this will pretty much only work for explicit setVisible() calls. If you need to detect hiding more generally, you can use a HierarchyListener, but it's probably more trouble than it's worth.
window.addHierarchyListener(new HierarchyListener() {
#Override
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("valid: " + window.isValid());
System.out.println("showing: " + window.isShowing());
}
});
Note that when you dispose a window you'll get a couple of HierarchyEvents, first for hiding and then for invalidation, but when you hide it with setVisible() it's still valid, so you won't get the invalidation.
I don't seem to have your problem. When I use the code below windowDeactivated() is called for either setVisible( false ) or dispose() and windowClosed() is also called for dispose().
ClosingDialog.java:
public class ClosingDialog extends JDialog {
public ClosingDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
JPanel contentPanel = (JPanel) this.getContentPane();
JButton setVisButton = new JButton("setVisible( false )");
setVisButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClosingDialog.this.setVisible(false);
}
});
JButton disposeButton = new JButton("dispose()");
disposeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClosingDialog.this.dispose();
}
});
contentPanel.setLayout(new FlowLayout());
contentPanel.add(setVisButton);
contentPanel.add(disposeButton);
this.addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
}
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconified");
}
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");
}
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
});
this.setSize(300, 300);
}
}
Dispatch a windowClosing event to the Window. Check out the ExitAction example from the Closing an Application entry.
Untested suggestion:
Have you tried getWindowListeners() and then iterating around to fire windowClosed() to each of the WindowListeners?
EDIT: the above suggestion is wrong. Keeping it for posterity.
I'm afraid calling dialog.dispose() works fine for me in my simple example.
I wanted to fire a windowClosing event from the code (just as if the user clicked the X), because I have an extra close button in the JDialog and want the same WindowListener (that I implemented using a WindowAdapter) to be run when the X is clicked and when the button is clicked. Running dispose() only fires windowClosed, not windowClosing, and I want a message to appear before the window is closed, for confirmation. I also didn't manage to fire windowClosing via JDialog's method processWindowEvent since it is protected.
Here is how I got it working though:
WindowAdapter adapter = (WindowAdapter)jdialog.getWindowListeners()[0];
adapter.windowClosing(new WindowEvent((Window)jdialog, WindowEvent.WINDOW_CLOSING));
Hope that helps someone.
So I am creating this applet which I want to have full on swing components in it. I have looked at all the docs, I have made the applet, and I can get something to show up in it if I override the update(Graphics g) method, but simply adding components to the contentPane doesn't seem to be doing it! What am I doing wrong?
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.steephill.kindlab.LabApp;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class ClientApplet extends JApplet {
ClientTreePanel treePanel;
public void destroy() {
// Put your code here
}
public String getAppletInfo() {
return "KindLab Client Applet";
}
public void init() {
try {
LabApp.initializeHibernate();
if (!LabApp.authenticate("user", "pass")) {
JOptionPane.showMessageDialog(this, "authentication failed");
} else {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "error intitializing applet\r\n" + ex.getMessage());
}
}
protected void createGUI() {
treePanel = new ClientTreePanel();
treePanel.setVisible(true);
getContentPane().add(new JLabel("TESTING!"));
getContentPane().add(treePanel);
System.out.println("THIS DOES RUN");
}
public void start() {
// Put your code here
}
public void stop() {
// Put your code here
}
/* if I uncomment this method, it WORKS and I get "Hello World!"
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello World!",25,25);
}
*/
}
Please, help! And thank you!
Joshua
I see several problems with your code here:
you do not call pack() at the end of your GUI setup
you add several components to the applet's content pane, but without any layout constraints. The default content pane usually is a BorderLayout, so adding two components without any constraints will probably only put the ClientTreePanel on top.
Since you do not call pack(), the layout will not be calculated, which for your case probably results in nothing being displayed (you did not provide the code for ClientTreePanel).
You shouldn't have to call pack() - the layout will be calculated when the component first becomes realized, which happens when you call pack - but also when the component first becomes visible.
The "adding components without constraints" is on the right track - you should change the code adding components to the content pane to :
getContentPane().add(new JLabel("TESTING!"), BorderLayout.NORTH);
getContentPane().add(treePanel, BorderLayout.CENTER);
The other problem is why your ClientTreePanel component isn't showing up - it could be sizing, or a layout problem, or other things - but without seeing that code, it would just be guesses.
Try to delete your paint method and you'll see it works. The problem might be the fact that since you've got a paint method all the changes are made through it. It's weird though because it does display a JButton.