I'm designing GUI of a download manager in java, in the main frame of the program (which is a JFrame) there are several buttons, one of them is Settings button which opens into a JDialog, in Settings section user can choose between several default look and feels (like steel, Nimbus etc).
the problem is, I want to apply the change of look and feel to both Setting section and mainframe but as Setting section is a different frame and class, the change just takes effect in Settings frame, I don't know how to make this change affect the main panel.
Code (part of Settings class):
UIManager.LookAndFeelInfo[] LAFInfo = UIManager.getInstalledLookAndFeels();
String [] LookAndFeels = new String[LAFInfo.length];
for (int i = 0; i < LAFInfo.length; i++) {
LookAndFeels [i] = LAFInfo[i].getName();
JComboBox<String> lookAndFeelChoices = new JComboBox<>(LookAndFeels);
lookAndFeelChoices.setBounds(200,220,200,30);
settings.add(lookAndFeelChoices);
lookAndFeelChoices.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
try {
UIManager.setLookAndFeel(LAFInfo[lookAndFeelChoices.getSelectedIndex()].getClassName());
SwingUtilities.updateComponentTreeUI(Settings.super.getContentPane())
} catch (Exception ei) {
System.out.println(ei);
}
}
});
}
Here is an example on how to achieve that:
pass the calling window to your dialog, and update it using SwingUtilities.updateComponentTreeUI
public class Main {
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton open=new JButton("Open Look and feel chooser");
open.addActionListener(e -> {
new LookAndFeelDialog(frame);
});
frame.setLayout(new FlowLayout());
frame.getContentPane().add(open);
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(400, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
And the JDialog
class LookAndFeelDialog extends JDialog {
public LookAndFeelDialog(Frame callingFrame) {
super(callingFrame, "Pick a look and feel");
setPreferredSize(new Dimension(300,100));
UIManager.LookAndFeelInfo[] LAFInfo = UIManager.getInstalledLookAndFeels();
String [] LookAndFeels = new String[LAFInfo.length];
for (int i = 0; i < LAFInfo.length; i++) {
LookAndFeels [i] = LAFInfo[i].getName();
JComboBox<String> lookAndFeelChoices = new JComboBox<>(LookAndFeels);
add(lookAndFeelChoices);
lookAndFeelChoices.addItemListener(e -> {
try {
UIManager.setLookAndFeel(LAFInfo[lookAndFeelChoices.getSelectedIndex()].getClassName());
// update current window
SwingUtilities.updateComponentTreeUI(getContentPane());
// update calling frame
SwingUtilities.updateComponentTreeUI(callingFrame);
} catch (Exception ei) {
ei.printStackTrace();
}
});
}
setLocationRelativeTo(callingFrame);
pack();
setVisible(true);
}
}
Related
I have a JFrame containing various components and I would like to add a translucent grey overlay over the top while the application is initializing various things. Ideally it would prevent interaction with the underlying components and would be able to display some "Loading..." text or a spinning wheel or something similar.
Is there a simple way to do this using Java and Swing?
Take a look at JRootPane and JLayeredPane http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#layeredpane
What you're asking about specifically sounds like a Glass Pane.
http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
The Glass Pane prevents interaction with underlying components and can be used to display something on top of your JFrame.
As #David said, you can use the glass pane for displaying some loading text or image above the rest of the application.
As for the grey overlay: why don't you use the built in ability to disable components as long as your application is loading? Disabled components will get grayed out automatically and cannot be interacted with by the user.
Something like this:
public class LoadingFrame extends JFrame{
JButton button;
public LoadingFrame() {
button = new JButton("ENTER");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Application entered");
}
});
setLayout(new BorderLayout());
add(button, BorderLayout.CENTER);
}
public void startLoading(){
final Component glassPane = getGlassPane();
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
final JLabel label = new JLabel();
panel.add(label, BorderLayout.SOUTH);
setGlassPane(panel);
panel.setVisible(true);
panel.setOpaque(false);
button.setEnabled(false);
Thread thread = new Thread(){
#Override
public void run() {
for (int i = 5; i > 0; i--) {
label.setText("Loading ... " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// loading finished
setGlassPane(glassPane);
button.setEnabled(true);
}
};
thread.start();
}
public static void main(String[] args) {
LoadingFrame frame = new LoadingFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.startLoading();
frame.setVisible(true);
}
}
As part of my Diploma in Software Dev we have to create a Java GUI to manage a Soccer League. My group has produced numerous JPanels that in the click-through prototype we put into JTabbedPane. This worked fine up until now where I'm moving them into separate files and into a MVC layout.
I'm using a window class to hold the top level JFrame and menubar and adding in the tabbed panes to that. This works fine in the constructor but when I remove them from the constructor and try to add them from the Bootstrap via the Window.attachTabbedPanel(String name, JPanel panel) it doesn't display but querying tabbedPane.getTabCount() display an incrementing number of tabs.
Here's a stripped back set of code:
The Bootstrap file:
public class Bootstrap {
private Window mainWindow;
public Bootstrap() {
//Window class is our containing JFrame and JMenuBar
mainWindow = new Window();
//Load up our view classes
TestTab tab1 = new TestTab();
TestTab tab2 = new TestTab();
//Attach them
mainWindow.attachTabbedPanel("Tab1", tab1.getScreen());
mainWindow.attachTabbedPanel("Tab2", tab2.getScreen());
} // Bootstrap()
public gui.Window getWindow(){
return mainWindow;
}
} // Bootstrap
This is called by the Main file:
public class Main {
public static void main(String[] args) {
Bootstrap RunMVC = new Bootstrap();
gui.Window mainWindow = RunMVC.getWindow();
mainWindow.run();
} // main()
} // Main
The problem starts here at the Window class, I've added in a In Constructor tab to check I haven't stuffed up the tabbedPane but it works fine at that point.
public class Window {
private JFrame frame;
private JMenuBar menuBarMain;
private JMenu mnFile;
private JTabbedPane tabbedPane;
private int count;
/**
* Create the application.
*/
public Window() {
//Build the frame
frame = new JFrame();
frame.setBounds(100, 100, 1280, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
//End Build frame
TestTab conTab = new TestTab();
//Add the tabbed pane to hold the top level screens
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "name_1");
tabbedPane.addTab("In Consructor", conTab.getScreen());
count = 1;
}
public void attachTabbedPanel(String name, JPanel panel){
System.out.println("Window: adding Jpanel name: "+name);
System.out.println("panel is a: "+panel);
tabbedPane.addTab(name, panel);
tabbedPane.updateUI();
System.out.println("Number of tabs: "+tabbedPane.getTabCount());
System.out.println("Last Tab .isEnabledAt() "+tabbedPane.isEnabledAt(count++));
tabbedPane.updateUI();
}
/**
* Launch the window.
*/
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
And lastly the Panel:
public class TestTab {
private JPanel screen;
private JLabel lblSeason;
private JButton btnEdit;
private JLabel lblRounds;
public JPanel getScreen() {
return screen;
}
/**
* Initialize the contents of the frame.
*/
public TestTab() {
screen = new JPanel();
screen.setLayout(new MigLayout("", "[8%,right][10%,left][8%,right][10%,left][grow][50%]", "[][][grow]"));
lblSeason = new JLabel("Test");
screen.add(lblSeason, "flowx,cell 0 0");
btnEdit = new JButton("Edit Test");
screen.add(btnEdit, "cell 5 0,alignx right");
lblRounds = new JLabel("More Testing");
screen.add(lblRounds, "cell 0 1,alignx left");
}
}
Your error is here:
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
You are using new instance of window instead of using created earlier, try to use this code
public void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
updateUI() does not do what you think it does. It should never be called directly - it is called from the superclass constructor to allow the Swing look and feel delegate to initialize itself.
Eliminating the call to updateUI() may solve your problem; or if the tabbed pane is already on screen, you may need to force a repaint/revalidate - the incantation for that is invalidate(); revalidate(); repaint();.
I want to show how merge sort perform visually using JFrame. What I want to do is to make visible subsequent JLabel with some time delay. I tried many way but all of them appears at same moment with no intermediate delay.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// jLabel1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(false);
jLabel7.setVisible(false);
final Timer t=new Timer((4000), null);
final int delay=2000;
final ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jLabel1.setVisible(true);
t.getDelay();
jLabel2.setVisible(true);
t.setDelay(3000);
jLabel3.setVisible(true);
t.setDelay(2000);
jLabel4.setVisible(true);
t.setDelay(2000);
jLabel5.setVisible(true);
t.setDelay(2000);
jLabel6.setVisible(true);
t.setDelay(2000);
}
};
new Timer(delay, taskPerformer).start();
But when I click button all the lables appear at same momenet though I have kept delay.
You need to update the icons in the timer's action listener, as shown here. You can implement the Icon interface to render icons having a size proportional to an element's comparative value, as shown here.
Addendum: Can you please be little bit specific?
You want to animate the intermediate steps of sorting a List<Number> of size N in some initially random order. Number subclasses implement Comparable<T>, so compareTo() is already done. A GridLayout(1, 0) of JLabel each having an Icon can be used to display the values. DecRenderer shows how to create icons with a proportional size; you'll want to vary the height over the interval [0, N). GrayIcons & Mad's example show how to animate the display of the icons in some order.
There are a number of reasons why this won't work. Firstly, javax.swing.Timer doesn't work this way. It waits in the background until the given delay has past and then calls the registered ActionListeners actionPerformed method.
Secondly, if it did work this way, it would block the Event Dispatching Thread, preventing it from processing repaint requests.
I think you will find How to use Swing Timers of use.
public class BlinkOut {
public static void main(String[] args) {
new BlinkOut();
}
public BlinkOut() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel[] labels;
private int[] delays;
private Timer timer;
private int index;
public TestPane() {
setLayout(new GridLayout(0, 1));
labels = new JLabel[7];
for (int index = 0; index < 7; index++) {
labels[index] = new JLabel("Label " + (index + 1));
add(labels[index]);
}
delays = new int[] {2000, 3000, 2000, 2000, 2000, 2000, 2000};
JButton hide = new JButton("Hide");
add(hide);
hide.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Click");
index = 0;
labels[index].setVisible(false);
timer.setDelay(delays[index]);
timer.start();
}
});
timer = new Timer(delays[0], new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Tick");
timer.stop();
index++;
if (index < 7) {
labels[index].setVisible(false);
timer.setDelay(delays[index]);
timer.start();
}
}
});
timer.setRepeats(false);
timer.setCoalesce(true);
}
}
}
I am working on an application that executes some functions that run for long. To let the user aware that the processing is taking place, I needed a label that can display some label that can represent that. So, I created a small widget for such a label.
The program below runs find and I get the output as I wanted.
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* This is an extension to a JLabel that can be used to display an ongoing progress.
* #author Ankit Gupta
*/
public class ProgressLabel extends JLabel {
/**
* The prefix label to which periods are added.
*/
private String startLabel;
/**
* The label to display end of an operation.
*/
private String endLabel;
/**
* Flag to indicate whether the animation is running or not.
*/
private boolean running = false;
//list to hold intermediate labels
List<String> intermediateLabels;
public ProgressLabel(String slbl, String elbl) {
this.startLabel = slbl;
this.endLabel = elbl;
//initialize all the labels to be used once as creating them again and again is expensive
intermediateLabels = new ArrayList<String>();
intermediateLabels.add(startLabel+".");
intermediateLabels.add(startLabel+"..");
intermediateLabels.add(startLabel+"...");
intermediateLabels.add(startLabel+"....");
}
public void done(){
running = false;
}
public void start(){
running = true;
new LabelUpdateThread().start();
}
private class LabelUpdateThread extends Thread{
int i;
public LabelUpdateThread(){
i=0;
}
#Override
public void run(){
while(running){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
setText(intermediateLabels.get((i++)%3));
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
}
setText(endLabel);
}
}
public static void main(String []args) throws InterruptedException{
final JFrame frame = new JFrame("Testing ProgressLabel");
JPanel panel = new JPanel();
ProgressLabel progressLabel = new CZProgressLabel("Searching", "Done");
panel.add(progressLabel);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,500));
frame.pack();
progressLabel.start();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
frame.setVisible(true);
}
});
Thread.sleep(5000);
progressLabel.done();
}
}
However, when I tried to include this in the application, it did not work as expected. I created a small panel with a button and in the actionPerfomed() code for the button I used the ProgressLabel's start() and done() methods as before but this time, the label just did not update to Done until the length process finished. Here is another piece of code using the ProgressLabel with actionPerformed() :
public class SearchPanel extends JPanel {
private JTextArea queryBox;
private JButton searchBtn;
private ProgressLabel progressLabel;
private JSeparator queryAreaSeparator;
public SearchPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//First Row
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridx = 0;
queryBox = new JTextArea();
queryBox.setRows(25);
queryBox.setColumns(25);
this.add(queryBox, gbc);
//Second Row
gbc.gridy = 1;
gbc.gridwidth = 1;
progressLabel = new ProgressLabel("Searching", "Done");
this.add(progressLabel, gbc);
gbc.gridx = 1;
searchBtn = new JButton("Search");
this.add(searchBtn, gbc);
searchBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
progressLabel.start();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
//the above sleep() call will be replace by some time-consuming process. It is there just for testing now
progressLabel.done();
}
});
gbc.gridx = 0;
}
/**
* function to test CZSemanticSearchLabel
*/
public static void main(String[] args) throws InterruptedException {
final JFrame frame = new JFrame();
CZSemanticSearchPanel panel = new CZSemanticSearchPanel();
frame.add(panel);
frame.setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Thread.sleep(10000);
frame.dispose();
final JFrame frame1 = new JFrame("Testing ProgressLabel");
JPanel panel1 = new JPanel();
CZProgressLabel progressLabel = new CZProgressLabel("Searching", "Done");
panel1.add(progressLabel);
frame1.add(panel1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setPreferredSize(new Dimension(500, 500));
frame1.pack();
progressLabel.start();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame1.setVisible(true);
}
});
Thread.sleep(5000);
progressLabel.done();
}
}
I believe that I have screwed something with Swing's Event dispatch model. But, I cannot figure what? Can someone tell me what is wrong with this code and how do I correct it?
I don't know about your actual code, but your sample code is flawed...
In your ActionListener you are doing this...
progressLabel.start();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
//the above sleep() call will be replace by some time-consuming process. It is there just for testing now
progressLabel.done();
This will STOP the Event Dispatching Thread, preventing any repaint requests from the been handled (ie no screen updates) for 10 seconds...this will also make your application look like it's "hung".
I updated you ActionListener to read like this (note I added a isRunning method which returns the running member from the label)
if (progressLabel.isRunning()) {
progressLabel.done();
} else {
progressLabel.start();
}
And it works fine.
You might like to read through Currency in Swing for some more ideas.
Also, as already suggested, SwingWorker may be a better approach
Instead of implementing this yourself with threading, you can use SwingWorker: Simple Background Tasks that is made for such things, and the linked example is very similar to your problem.
Your start() doesn't execute your LabelUpdateThread().run() but your LabelUpdateThread().start().
Problem: Code does not turn the button back on (Example gives it 5 seconds for you to press)
Example code:
public static void main(String[] args) throws InterruptedException
{
Example call = new Example();
Thread.sleep(5000);
call.ButtonSwitch(1);
}
NOTE: this is the smallest coding i could make to show my problem
public class Example extends JFrame implements ActionListener {
static Example frame2 = new Example();
GridLayout experimentLayout = new GridLayout(0,1);
JPanel Game = new JPanel();
JButton button1 = new JButton("Press");
public Example()
{
Create();
}
public void Set()
{
setResizable(false);
}
public static void Create() {
/* Use an appropriate Look and Feel */
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
//Create and set up the window.
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame2.addComponentsToPane(frame2.getContentPane());
//Display the window.
frame2.pack();
frame2.setVisible(true);
}
public void addComponentsToPane(final Container pane)
{
Game.setLayout(experimentLayout);
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(2,3));
//Set up components preferred size
JButton b = new JButton("Just fake button");
Dimension buttonSize = b.getPreferredSize();
Game.setPreferredSize(new Dimension((int)(buttonSize.getWidth() * 2),
(int)(buttonSize.getHeight() * 1)* 4));
Game.add(button1);
button1.addActionListener(this);
//Process the Apply gaps button press
pane.add(Game, BorderLayout.NORTH);
pane.add(new JSeparator(), BorderLayout.CENTER);
pane.add(controls, BorderLayout.SOUTH);
}
//Turns button off On Click
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
button1.setEnabled(false);
}
}
//This does not turn the button on but tries to
public void ButtonSwitch(int num)
{
if (num == 1)
{
System.out.println("This is called");
button1.setEnabled(true);
}
}
}
I want to make the method Enable the button, but if this is not possible a way to do this in action listener without user input would be the second option (which would look like the Button switch method placed inside the ActionListener)
The problem comes from the bad design of the class. The point is that you are not calling setEnabled(true) and setEnabled(false) on the same button1. In your main,
Example call = new Example();
Thread.sleep(5000);
call.ButtonSwitch(1);
the last line invokes setEnabled(true) on the button of call, while the actionPerformed invokes the setEnabled(false) on the button of frame2.
Regardless, you are doing it wrong:
Don't mix the main (entry) thread with the EDT.
Don't hold a member of the same class type as the containing class (unless there's a special reason to do so).
Here is a real MCVE of a working code:
public class Example extends JFrame {
JButton button = new JButton("Press");
Timer timer = new Timer(5000, e -> button.setEnabled(true));
public Example() {
add(button);
button.addActionListener(e -> {
button.setEnabled(false);
timer.start();
});
timer.setRepeats(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> new Example());
}
}
Notes:
Method and non-final variable names start with a lowercase.
Don't use setPreferredSize, override getPreferredSize instead.