package javaapplication1;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaApplication1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "JOptionPane");
}
});
}
}
When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane that I triggered. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar. Looking forward for your answers. Thanks in advance. Or is there any other alternative to this?
That code works for me, though you might try this variant with 2 changes.
It creates and shows the GUI on the EDT.
It uses the content pane of the frame as the parent of the JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaApplication1 {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(frame, "JOptionPane");
JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
}
});
}
};
SwingUtilities.invokeLater(r);
}
}
Update
When I add the following lines to the beginning of the source seen above..
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
..the output & result is as follows.
Running in 1.7
Result: Failure as described in the question.
1.7.0_09
23.5-b02
Running in 1.6
Result: Success with no unusual artifacts or behavior.
1.6.0
1.6.0-b105
Analysis
Note that other results from comments suggest the behavior changed some time between that early 1.6 version, and 1.6.0_25. It seems like a regression bug. The OP should check the bug database & if nothing likely shows up, lodge a new report.
JOptionPane.showInternalMessageDialog(frame.getContentPane(), "JOptionPane");
Related
This is the example i got from http://djproject.sourceforge.net/ns/documentation/Snippets.html with a little bit modification.
package View;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
/**
* #author Christopher Deckers
*/
public class GoogleMaps extends JPanel {
public GoogleMaps() {
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://www.google.com");
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
add(webBrowserPanel, BorderLayout.CENTER);
// Create an additional bar allowing to show/hide the menu bar of the web browser.
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());
menuBarCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
}
});
buttonPanel.add(menuBarCheckBox);
add(buttonPanel, BorderLayout.SOUTH);
}
}
and then in another JFrame, I am trying to open this browser by clicking a button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
GoogleMaps maps = new GoogleMaps();
UIUtils.setPreferredLookAndFeel();
NativeInterface.open();
JFrame frame = new JFrame("DJ Native Swing Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(maps, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
NativeInterface.runEventPump();
}
But then it freeze my GUI. What should I do in this case?
Any pointers will help
NativeInterface.runEventPump() function is a blocking one. It should be run in another thread.
In your case it could be solved like this:
new Thread(new Runnable() {
#Override
public void run() {
if (!NativeInterface.isEventPumpRunning())
NativeInterface.runEventPump();
}
}).start();
I'm trying to make a full-screen GUI using Java which, on the JFrame, has 2 JPanel's, one of which only takes roughly 0.10 of the screen width. When I place these panels on the frame, using the same height, they appear to display with a different height on Linux & Mac OS but they appear okay on Windows. Does anybody have any idea on how to make both panels the same height for Linux & Mac OS? I re-made the problem quickly using WindowBuilder for posting here.
package View;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import controller.ExitActionListener;
import java.awt.event.ActionListener;
public class TestFrame2 {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame2 window = new TestFrame2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFrame2() {
initialize();
}
private void initialize() {
frame = new JFrame();
GraphicsDevice device = frame.getGraphicsConfiguration().getDevice();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setSize((int)(frame.getWidth()*0.1), frame.getHeight());
panel.setBackground(Color.BLACK);
frame.getContentPane().add(panel);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.RED);
panel1.setSize(frame.getWidth(), frame.getHeight());
frame.getContentPane().add(panel1);
// temporary close button
ActionListener exitActionListener = new ExitActionListener();
JButton exit = new JButton("Exit System");
exit.setBounds(150 ,200, 150, 100);
exit.addActionListener(exitActionListener);
panel1.add(exit);
//Set program as full screen
device.setFullScreenWindow(frame);
}
}
I'm learning about threads and I've got a problem with it. I'm trying to make 2 frames, one is a main frame and another will be shown later after clicking on a button. I want to stop the main frame while the new frame is running. Can you guys help me with a very simple example for this? (And the new frame will be closed after clicking on a button too). Just 2 frames with a button on each are enough. Much appreciated!
You should avoid the use of multiple JFrames, use modal dialogs instead. JOptionPane offers a ton of good, easy & flexible methods to do so.
Here's an example. When you click the button the dialog will appear on top of the JFrame. The main JFrame won't be clickable anymore, since JOptionPane.showMessageDialog() produces a modal window.
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Example {
public Example() {
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(frame, "I'm a dialog!");
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
Output:
Accordion can be downloaded here - http://www.javaswingcomponents.com/product/accordion
Here is a sample output of an accordion. I want to remove the numbers on the right side of the tab. How can I do it? Thanks!
Here is the code of the sample:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.javaswingcomponents.accordion.JSCAccordion;
import com.javaswingcomponents.accordion.TabOrientation;
public class SampleAccordion extends JPanel {
static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SampleAccordion codeExample = new SampleAccordion();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel = frame.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(codeExample, BorderLayout.CENTER);
frame.pack();
frame.setSize(500, 300);
frame.setVisible(true);
}
});
}
public SampleAccordion() {
JSCAccordion accordion = new JSCAccordion();
JPanel transparentPanel = new JPanel();
transparentPanel.setOpaque(false);
transparentPanel.setBackground(Color.GRAY);
JPanel opaquePanel = new JPanel();
opaquePanel.setOpaque(true);
opaquePanel.setBackground(Color.GRAY);
accordion.addTab("Tab 1", new JLabel("help me remove 1"));
accordion.addTab("Tab 2", new JLabel("help me remove 2"));
accordion.setTabOrientation(TabOrientation.VERTICAL);
setLayout(new GridLayout(1, 1, 30, 30));
add(accordion);
}
}
You can specify whether you want to see the tab index:
accordion.setTabOrientation(TabOrientation.VERTICAL);
((FormattedTabRenderer) accordion.getTabRenderer()).setShowIndex(false);
(The first line is already in the sample code and is only included as a reference.)
It looks like the accordion supports three pluggable look & feels: basic, steel, and dark steel. I'm not sure whether the tab renderer can be cast to the FormattedTabRenderer abstract class for all PLAFs, but it seems to work fine for steel.
I am trying to create a piano program where you click the key and using an actionlistener is plays the note from the jfugue library. For some reason after about 18 clicks without changing anything the buttons stop working. I cut down the code to analyze why this might happen, thus only two notes.
Thanks in advance for any advice!
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Color;
import javax.swing.JLayeredPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.*;
import org.jfugue.*;
public class ChordPlayer2 extends JComponent{
public ChordPlayer2(){
final Player player = new Player();
JFrame frame = new JFrame();
JButton cButton, csharpButton;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(0, 0);
buttonPanel.setSize(1700, 1000);
csharpButton = new JButton("");
csharpButton.setLocation(100, 150);
csharpButton.setSize(100,520);
buttonPanel.add(csharpButton);
cButton = new JButton("");
cButton.setLocation(0, 150);
cButton.setSize(160, 800);
buttonPanel.add(cButton);
class cClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C");
}
}
class csClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C#");
}
}
ActionListener c = new cClicker();
cButton.addActionListener(c);
ActionListener cs = new csClicker();
csharpButton.addActionListener(cs);
buttonPanel.setOpaque(true);
//return buttonPanel;
frame.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1700, 1000);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//JFrame.setDefaultLookAndFeelDecorated(true);
ChordPlayer2 demo = new ChordPlayer2();
}
}
This is a known bug in JFugue:
https://code.google.com/p/jfugue/issues/detail?id=49
The most recent version is claimed to fix this:
https://code.google.com/p/jfugue/downloads/detail?name=jfugue-4.1.0-20120125.jar&can=2&q=