JProgressBar text overflow - java

My program writes text in a JProgressBar. The problem is the text is wider than the JProgressBar's width.
I have already changed the JProgressBar's height to be able to write the text on two lines but I don't want to the change the width.
How to change the JProgressBar's overflow to make the text going back to the next line if it is too wide?
I hope this is clear enough :)
Here is what I would like:
Thanks
EDIT
After #mKorbel reply the result looks like this:
The label works quite fine but why those strips?
My code:
// Construct progress bar
JProgressBar progressBar = new JProgressBar(0, 100);
// Set progressBar color
progressBar.setForeground(new Color(0,176,80));
// Edit progress bar height
Dimension prefSize = progressBar.getPreferredSize();
prefSize.height = 50;
progressBar.setPreferredSize(prefSize);
// Set the layout
progressBar.setLayout(new BorderLayout(5, 5));
// Set progress bar value
progressBar.setValue(38);
// Construct the label
JLabel progressLabel = new JLabel("<html>I have already changed the JProgressBar's height to be able to write the text on two lines but I don't want to the change the width.</html>");
// Set alignment
progressLabel.setHorizontalAlignment(JLabel.CENTER);
progressLabel.setVerticalAlignment(JLabel.CENTER);
// Set the borders
progressLabel.setBorder(new EmptyBorder(15, 15, 15, 15));
// Change the font
font = progressLabel.getFont();
font = font.deriveFont(Font.BOLD, 12);
progressLabel.setFont(font);
// Add label to the progress bar
progressBar.add(progressLabel, BorderLayout.CENTER);
// Add progress bar to the frame
frame.add(progressBar);

the program is developed with Java 6. It seems JLayer is not
available. If I'm wrong, could you provide some code on how to do
this?
could you provide some code on how to do this? --- > JLayer & JProgressBar by #aterai, for more ideas see his blog, for Java6 you can to use JXLayer
or with very similair logics by using GlassPane
some notes
should be used GBC instead of NullLayout
can be nicer with added Icon or transparent background
(by add LayoutManager to JLabel) there can be placed bunch of JComponents with the same effect as for JPanel
for example
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
//https://stackoverflow.com/questions/14560680/jprogressbar-low-values-will-not-be-displayed
public class ProgressSample {
private JFrame frame = new JFrame("GlassPane instead of JLayer");
private JLabel label;
private GridBagConstraints gbc = new GridBagConstraints();
private JProgressBar progressSeven;
public ProgressSample() {
frame.setLayout(new FlowLayout());
frame.add(new JButton("test"));
frame.add(new JCheckBox("test"));
frame.add(new JRadioButton("test"));
// Nothing is displayed if value is lover that 6
JProgressBar progressSix = new JProgressBar(0, 100);
progressSix.setValue(2);
frame.add(progressSix);
// but this works value is higher that 6
progressSeven = new JProgressBar(0, 100);
progressSeven.addComponentListener(new ComponentAdapter() {
#Override
public void componentMoved(ComponentEvent e) {
label.setBounds(
(int) progressSeven.getBounds().getX(),
(int) progressSeven.getBounds().getY(),
label.getPreferredSize().width,
label.getPreferredSize().height);
}
});
progressSeven.setValue(7);
frame.add(progressSeven);
label = new JLabel();
label.setText("<html> Concurency Issues in Swing<br>"
+ " never to use Thread.sleep(int) <br>"
+ " durring EDT, simple to freeze GUI </html>");
label.setPreferredSize(new Dimension(label.getPreferredSize().width, label.getPreferredSize().height));
Container glassPane = (Container) frame.getRootPane().getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(null);
glassPane.add(label, gbc);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
ProgressSample dialogTest = new ProgressSample();
}
}
EDIT
comments
my first thought was using html, but astonishingly (for me) the
progressbar doesn't support it ... by #kleopatra
and
I think my question may not be clear enough. I don't want the text to
exceed the JProgressBar borders. Plus, I don't want to insert manually
line returns (ie no ). I added a picture of what I want. by
#Maxbester
result is to use JProgressBar as Container, put there proper LayoutManager, overlay JProgressBar by JLabel
enhancements, to set EmptyBorder for JLabel, e.g. label.setBorder(new EmptyBorder(15, 15, 15, 15));
EDIT2 (Icon is, can be semi_transparent too, can overlay JProgressBar)
code could be something like as
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class JProgressBarWithJLabel {
private JFrame frame = new JFrame("JLabel in JProgressBar");
private JLabel label;
private JProgressBar progressSeven;
public JProgressBarWithJLabel() {
progressSeven = new JProgressBar(0, 100){
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 60);
}
};
progressSeven.setValue(38);
progressSeven.setLayout(new BorderLayout(5, 5));
label = new JLabel();
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
label.setBorder(new EmptyBorder(15, 15, 15, 15));
label.setText("<html>I have already changed the JProgressBar's height "
+ "to be able to write the text on two lines but I don't want "
+ "to the change the width.</html>");
progressSeven.add(label, BorderLayout.CENTER);
frame.add(progressSeven);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new JProgressBarWithJLabel();
}
});
}
}
EDIT3:
default stripping for WindowsClassicLookAndFeel (Icon isn't semi_transparent)

The available answers didn't satisfy me. Thus I implemented the following alternative solution for my own needs.
import javax.swing.JProgressBar;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
public class MultilineProgressBar extends JProgressBar
{
private static final String FONT_NAME = "Dialog";
private static final int FONT_SIZE = 12;
private static final int INTERLINE_COEFFICIENT = 2;
private static final int NEWLINE_OFFSET = FONT_SIZE * INTERLINE_COEFFICIENT;
private static final int CENTERING_DIVISOR = 2;
#Override
protected void paintComponent(final Graphics graphics)
{
super.paintComponent(graphics);
final String componentString = getString();
int i = componentString.indexOf('\n');
if (i == -1)
return;
// Draw first line of the component's string
String currentString = componentString.substring(0, i);
Rectangle2D stringBounds = getFontMetrics(getFont()).getStringBounds(currentString, graphics);
graphics.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
graphics.setColor(Color.WHITE);
graphics.drawString(currentString,
(int) (getWidth() - stringBounds.getWidth()) / CENTERING_DIVISOR,
(int) (getHeight() - stringBounds.getHeight()) / CENTERING_DIVISOR);
++i;
if (i >= componentString.length())
return;
// Draw second line of the component's string
currentString = componentString.substring(i);
stringBounds = getFontMetrics(getFont()).getStringBounds(currentString, graphics);
graphics.drawString(currentString,
(int) (getWidth() - stringBounds.getWidth()) / CENTERING_DIVISOR,
(int) ((getHeight() - stringBounds.getHeight()) / CENTERING_DIVISOR) + NEWLINE_OFFSET);
}
}

Related

JPanels are disappearing when scrolling

In my code i transfer the JPanel (Bestellpanel) from frame to frame1. After that, everytime i use the frame1 scrollbar it repaints frame1and my JPanel (Bestellpanel) is gone. That means I need a way to stop my JPanel getting overpainted. I read something about super.paint(); and other methods but I have major problems understanding them.
Here is a code example of my problem:
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class weqe {
private static JFrame frame = new JFrame("First Frame");
private static JFrame frame1 = new JFrame("Second Frame");
private static JPanel Bestellpanel = new JPanel();
private static int kunde = 1;
public static void addComponentsToPane(final Container pane) {
pane.setLayout(null);
final Insets insets1 = pane.getInsets();
// Mitn Button
JButton MitnIcon = new JButton("Mitnehmen");
MitnIcon.setFocusPainted(false);
MitnIcon.setVisible(true);
Dimension size2 = MitnIcon.getPreferredSize();
MitnIcon.setBounds(1010 + insets1.left, 700 + insets1.top,
size2.width + 27, size2.height + 50);
pane.add(MitnIcon);
MitnIcon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (kunde == 1) {
frame.getContentPane().remove(Bestellpanel);
Bestellpanel.setLocation(0, 0);
frame1.getContentPane().add(Bestellpanel);
Bestellpanel.repaint();
frame.repaint();
}
}});
// ScrollPane
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(2000,800));
panel1.setVisible(false);
JScrollPane scrollPane = new JScrollPane (panel1,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame1.add(scrollPane);
Bestellpanel.setBounds(930 + insets1.left, 50 + insets1.top,size2.width
+ 30, size2.height + 400);
Bestellpanel.setVisible(true);pane.add(Bestellpanel);
Bestellpanel.setBackground(Color.green);
}
private static void createAndShowGUI() {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
Insets insets1 = frame1.getInsets();
frame.setSize(1200 + insets.left + insets.right,
900 + insets.top + insets.bottom);
frame.setVisible(true);
frame1.setSize(800 + insets1.left + insets1.right,
600 + insets1.top + insets1.bottom);
frame1.setVisible(true);
frame.add(Bestellpanel);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
meinJDialog.setSize(800,800); and panel1.setPreferredSize(new Dimension(2000,800)); most likely are part of your problem, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (The general consensus says yes and to override getPreferred|Maximum|MinimumSize() methods instead)
Instead of removing/adding the JComponents yourself, try out Card Layout
You don't need to manually change component's visibility, again, check the link in point number 2, for this line: Bestellpanel2.setVisible(true);
Please follow the Java naming conventions: FirstWordUpperCaseClass, firstWordLowerCaseVariable, firstWordLowerCaseMethod() and ALL_WORDS_UPPER_CASE_CONSTANT), so, your code is easier to read and understand for you and for us.
If all the above points don't work, then consider posting a valid Minimal, Complete and Verifiable Example (MCVE) or Short, Self Contained, Correct Example (SSCCE) that demonstrates your issue, has no external dependencies or customizations such as background color / image, etc. It should be indented correctly, as said in the comments above.

JButton's ActionListener is responding yet code is apparently not being executed, but when I call method directly, it is. Very strange behavior?

When I add the ActionListener and put code in the actionPerformed, which calls a method, the method is called just fine, yet the code within it, which is supposed to dynamically add various Swing components, doesn't seem to be responding. When I add the line just outside the ActionListener, however, the same code is ran and the components are being added just fine. It is very strange and I'm not sure what the problem is.
When I add something like a System.out.println statement within the action performed method to add an appliance, it is obviously being called, but nothing is being added to the JPanel. When I put that System.out.println within the addAppliance() method, it is being called just fine as well. Like I said, a very strange problem. Here is my entire program so far:
package Main;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
#SuppressWarnings("serial")
public class ElectricPanel extends JPanel
{
private static final int WIDTH = 360;
private static final int HEIGHT = 600;
private ArrayList<JTextField> description;
private ArrayList<JTextField> volts;
private ArrayList<JTextField> current;
private ArrayList<JTextField> power;
private JButton addButton;
private JButton subtractButton;
private JLabel[] labels;
private int numOfAppliances;
public ElectricPanel()
{
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
setLayout(null);
description = new ArrayList<JTextField>();
volts = new ArrayList<JTextField>();
current = new ArrayList<JTextField>();
power = new ArrayList<JTextField>();
labels = new JLabel[]{new JLabel("Description"),
new JLabel("Volts"),
new JLabel("Current"),
new JLabel("Power")};
Dimension size = labels[0].getPreferredSize();
labels[0].setBounds(10, 10, size.width, size.height);
size = labels[1].getPreferredSize();
labels[1].setBounds(125, 10, size.width, size.height);
size = labels[2].getPreferredSize();
labels[2].setBounds(185, 10, size.width, size.height);
size = labels[3].getPreferredSize();
labels[3].setBounds(245, 10, size.width, size.height);
for (int i = 0; i < 4; i++)
{
add(labels[i]);
}
numOfAppliances = 0;
addAppliance();
setupAddAndSubtractButtons();
}
private void setupAddAndSubtractButtons()
{
Insets insets = new Insets(0, 0, 0, 0);
addButton = new JButton("+");
subtractButton = new JButton("-");
addButton.setBounds(305, 30, 24, 18);
subtractButton.setBounds(305, 50, 24, 18);
subtractButton.setVisible(false);
addButton.setMargin(insets);
subtractButton.setMargin(insets);
addButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
addAppliance();
}
});
subtractButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent arg0)
{
subtractAppliance();
}
});
add(addButton);
add(subtractButton);
addAppliance();
}
private void tryToAdd()
{
addAppliance();
}
private void addAppliance()
{
description.add(new JTextField(10));
volts.add(new JTextField(5));
current.add(new JTextField(5));
power.add(new JTextField(5));
Dimension size = description.get(numOfAppliances).getPreferredSize();
description.get(numOfAppliances).setBounds(10, 30 + 20 * numOfAppliances, size.width, size.height);
size = volts.get(numOfAppliances).getPreferredSize();
volts.get(numOfAppliances).setBounds(125, 30 + 20 * numOfAppliances, size.width, size.height);
size = current.get(numOfAppliances).getPreferredSize();
current.get(numOfAppliances).setBounds(185, 30 + 20 * numOfAppliances, size.width, size.height);
size = power.get(numOfAppliances).getPreferredSize();
power.get(numOfAppliances).setBounds(245, 30 + 20 * numOfAppliances, size.width, size.height);
add(description.get(numOfAppliances));
add(volts.get(numOfAppliances));
add(current.get(numOfAppliances));
add(power.get(numOfAppliances));
numOfAppliances++;
}
private void subtractAppliance()
{
int index = numOfAppliances - 1;
description.remove(index);
volts.remove(index);
current.remove(index);
power.remove(index);
}
}
Any and all help and suggestions are appreciated!
If you resize the window (if it is resizable of course) after you click the buttons, you will notice that the button clicks are taking effect. What is happening is that you are adding components, but not repainting the panel. Just add a repaint() call inside the actionPerformed() methods and your changes will show up.
The reason the methods were working outside the action listeners is that the method calls were taking place prior to the panel being first painted. Whereas, when the method calls were inside the action listeners, they were being executed once you click on the button, after the panel was painted.
Aside from that though, you definitely shouldn't be using a null layout, that will make your GUI limited to the computer you are working on. Also, read up on Java Swing.

JLabel.setText() relocates all labels in mousewheelevent

EDIT: leaving short code which shows issue:
Then label.setText("") is called and it has to change the text, i.e. scaler overcame 50 barrier the label dissapears for the mousewheelevent -> re-emerges on next event. Also a problem of the label not being present on the Load-up.
In actual program labels stack together in 1 spot if any of them has .setText() changing value or .setVisible changing (These two methods I need to be able to use without affecting labels positioning)
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
public class brokenLabel extends JFrame
{
private JLayeredPane layerPane;
private JPanel breakingLabelPanel = new JPanel();
private JLabel label;
private int scaler = 50;
private int xstart = 200;
private int ystart = 200;
public static void main(String[] args)
{
#SuppressWarnings("unused")
brokenLabel program = new brokenLabel();
}
public brokenLabel()
{
// Frame setup used on the program
final JFrame frame = new JFrame("This is a slideshow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
layerPane = new JLayeredPane();
frame.setContentPane(layerPane);
// Panel used (it's similar to what's added on the actual program)
breakingLabelPanel.setOpaque(true);
breakingLabelPanel.setLayout(new OverlayLayout(breakingLabelPanel));
breakingLabelPanel.setVisible(true);
breakingLabelPanel.setSize(getMaximumSize());
breakingLabelPanel.addMouseWheelListener(new MouseWheelListener(){
#Override
public void mouseWheelMoved(MouseWheelEvent arg0)
{
if (scaler > 5 || arg0.getWheelRotation() < 0)
scaler = scaler - 5*arg0.getWheelRotation();
setTexts();
}
});
//The testing label
label = new JLabel(new String("1"));
label.setLocation(xstart , ystart);
breakingLabelPanel.add(label);
frame.add(breakingLabelPanel,layerPane);
frame.setVisible(true);
//revalidate and repaint don't help make label visible on load-up
frame.revalidate();
frame.repaint();
}
public void setTexts(){
label.setLocation(xstart + scaler,ystart + 25);
if(scaler < 50)
{
label.setText("1");
}
else
{
label.setText("2");
}
}
}

Creating a button in the maximum size when scrollbars are not visible yet

I am trying to create very simple class. The goal is to create a button in the maximum size when scrollbars are not visible yet. My code doesn`t work. The condition pane.getVerticalScrollBar().isVisible() returns true even if the scrollbar is not visible. I wonder why and how can I fix it?
import java.awt.BorderLayout;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
JFrame frame = new JFrame();
final JButton button = new JButton("Hello");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
final JPanel buttons = new JPanel();
buttons.add(button);
buttons.revalidate();
final JScrollPane pane = new JScrollPane(buttons);
pane.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
System.out.println("*******************************************************************");
System.out.println("Vertical visible? " + pane.getVerticalScrollBar().isVisible());
System.out.println("Horizontal visible? " + pane.getHorizontalScrollBar().isVisible());
if (!pane.getVerticalScrollBar().isVisible() && !pane.getHorizontalScrollBar().isVisible()) {
button.setSize(button.getWidth() + 1, button.getHeight() + 1);
buttons.revalidate();
pane.revalidate();
} else {
button.setSize(button.getWidth() - 1, button.getHeight() - 1);
buttons.revalidate();
pane.revalidate();
pane.getViewport().removeChangeListener(this);
}
}
});
panel.add(pane);
frame.setContentPane(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Don't play with the set size method.
If you want the button to occupy all the space available then use either:
frame.add(button); // height/width will be max
frame.add(button, BorderLayout.SOUTH); // only width will be max
Let the layout managers do the job for you.
Edit:
I have another class which scale img in the label and I want to find the best value for scale to make it maximalized as possible
Create a custom panel and dynamically draw the image at the size of the panel. Then it will automatically scale based on the space available.
See Background Panel as an example of this approach.

change jlabel with method

I am writing a small program that converts files, and I wanted to have a box pop up that asks the user to please wait while the program loops through and converts all the relevant files, but I am running into a small problem. The box that pops up should have a JLabel and a JButton, while the user is "waiting" I wanted to display a message that says please wait, and a disabled "OK" JButton, and then when its finished I wanted to set the text of the JLabel to let them know that It successfully converted their files, and give them a count of how many files were converted. (I wrote a method called alert that sets the text of the label and enables the button.) The problem is That while the program is running, the box is empty, the Label and the Button are not visible, when it finishes, label appears with the final text that I want and the button appears enabled. I am not sure exactly what is going on, I tried changing the modifiers of the JLabel and JButton several times but I cant seem to get it to work correctly. Here is the code for the box that pops up, any help is greatly appricated.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PleaseWait extends javax.swing.JFrame{
private static final int height = 125;
private static final int width = 350;
final static JLabel converting = new JLabel("Please Wait while I convert your files");
private static JButton OK = new JButton("OK");
public PleaseWait(){
// creates the main window //
JFrame mainWindow = new JFrame();
mainWindow.setTitle("Chill For A Sec");
mainWindow.setSize(width, height);
mainWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// creates the layouts//
JPanel mainLayout = new JPanel(new BorderLayout());
JPanel textLayout = new JPanel(new FlowLayout());
JPanel buttonLayout = new JPanel(new FlowLayout());
// Sets Text //
converting.setText("Please wait while I convert your files");
// disables button //
OK.setEnabled(false);
// adds to the layouts //
textLayout.add(converting);
buttonLayout.add(OK);
mainLayout.add(textLayout, BorderLayout.CENTER);
mainLayout.add(buttonLayout, BorderLayout.SOUTH);
// adds to the frame //
mainWindow.add(mainLayout);
// sets everything visible //
mainWindow.setVisible(true);
}
public static void alert(){
OK.setEnabled(true);
String total = String.valueOf(Convert.result());
converting.setText("Sucsess! " + total + " files Converted");
}
}
Okay here's the issue. You are extending the JFrame . That means your class IS a JFrame.
When you create the PleaseWait frame you don't do anything to it. This is the empty box you are seeing. You are instead creating a different JFrame in your constructor. Remove your mainWindow and instead just use this. Now all of your components will be added to your PleaseWait object. That should fix your blank box issue.
You need an application to create your frame first. This is a simple example of such application.
import javax.swing.UIManager;
import java.awt.*;
public class Application {
boolean packFrame = false;
//Construct the application
public Application() {
PleaseWait frame = new PleaseWait();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.convert();
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application();
}
}
You have to slightly modify your frame to add controls to the content pane. You can do some work after frame is created, then call alert.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PleaseWait extends JFrame {
private static final int height = 125;
private static final int width = 350;
final static JLabel converting = new JLabel();
private static JButton OK = new JButton("OK");
BorderLayout borderLayout1 = new BorderLayout();
JPanel contentPane;
int count;
public PleaseWait(){
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(width, height));
this.setTitle("Chill For A Sec");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// creates the layouts//
JPanel mainLayout = new JPanel(new BorderLayout());
JPanel textLayout = new JPanel(new FlowLayout());
JPanel buttonLayout = new JPanel(new FlowLayout());
// Sets Text //
converting.setText("Please wait while I convert your files");
// disables button //
OK.setEnabled(false);
OK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// adds to the layouts //
textLayout.add(converting);
buttonLayout.add(OK);
mainLayout.add(textLayout, BorderLayout.CENTER);
mainLayout.add(buttonLayout, BorderLayout.SOUTH);
// adds to the frame //
contentPane.add(mainLayout);
}
public void convert(){
count = 0;
for (int i = 0; i <10; i++){
System.out.println("Copy "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
count++;
}
alert();
}
public void alert(){
OK.setEnabled(true);
// String total = String.valueOf(Convert.result());
converting.setText("Sucsess! " + count + " files Converted");
}
}

Categories

Resources