swing - JFrame not displaying the real size - java

Im very new to java. I don't know what's wrong with my frame. I set the size to 300 and 200.
What Im seeing is a short and fat stick like thing.
Below is my code:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class BicycleDemo extends JFrame {
/**
* The serialVersionUID.
*/
private static final long serialVersionUID = -4541236176053545919L;
public static void createGUI () {
JFrame jFrame = new JFrame("JFrame Demo");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = jFrame.getContentPane();
container.setLayout(new FlowLayout());
container.setBackground(Color.BLACK);
jFrame.setSize(300, 200);
jFrame.setResizable(false);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI ();
}
});
}
}
Please help.

You are calling pack(). pack() method resizes the frame to the smallest possible size to hold all the elements. So in fact you set the size to 200 x 300 and then resize the frame once more with pack().
Be aware however, that "hold all elements" is calculated by their preferred size, which can be just 0x0 pixels in a lot of cases.

Related

How to update Swing Components in Real Time

I am programming a multiplication app for very large integers, I need to update every sigle step of the multiplication in a Swing component ( I created a JPane extended class with a JTextArea in it, then add it to the JFrame inside a ScrollPane). The issue is that this Swing component only updates once the multiplication algorithm is done. I tried using a Thread that would call repaint method of the Pane every 10 ms, but it did not work. The next is a sample of the problem.
This is the main Frame class:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame extends JFrame implements ActionListener{
private Console console;
private JButton calculate;
private Calculator calculator;
public Frame(){
console=new Console();
calculate=new JButton("Calculate");
calculate.addActionListener(this);
calculate.setActionCommand("");
calculator=new Calculator(this);
this.setLayout(new BorderLayout());
this.add(console,BorderLayout.CENTER);
this.add(calculate, BorderLayout.NORTH);
this.setTitle("Frame");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(new Dimension(500,500));
this.setLocationRelativeTo(null);
}
public void writeOnConsole(String txt){
console.write(txt);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("")){
console.clear();
calculator.calculate();
}
}
public static void main(String[] args) {
new Frame();
}
}
This is the Console Class
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Console extends JPanel{
private JTextArea area;
public Console(){
this.setBorder(new TitledBorder("Console:"));
area=new JTextArea();
this.setLayout(new BorderLayout());
JScrollPane scroll=new JScrollPane(area);
this.add(scroll,BorderLayout.CENTER);
}
public void clear(){
area.setText("");
}
public void write(String txt){
area.append(txt+"\n");
}
}
Finally, this is the Calculator class (the one responsible for calling the writing)
public class Calculator {
private Frame parent;
public Calculator(Frame f){
parent=f;
}
public void calculate(){
for (int i = 0; i <= 1000000; i++) {
parent.writeOnConsole("Iteration "+i);
}
}
}
Note that if you run the program, the GUI will freeze until the Calculator class is done with the loop.
if you have a layout like a BorderLayout and you want to update it inside the JFrame do as bellow
JFrame frame = new JFrame();
BorderLayout layout = new BorderLayout();
layout.layoutContainer(frame.getContentPane());// use the frame as the border layout container
else you can use JFrame pack() method. The pack method packs the components within the window based on the component’s preferred sizes. it's not for updating but it updates the JFrame which is kind of a trick
JFrame frame = new JFrame();
//change the components dynamically
frame.pack();
or use Container methdod validate(). Validating a container means laying out its subcomponents. Layout-related changes, such as setting the bounds of a component, or adding a component to the container.
JFrame frame = new JFrame();
Container container = frame.getContentPane();
container.validate();
or if you want to update an specific component use
Component component = new JPanel();
component.repaint();
If this component is a lightweight component, repaint() method causes a call to this component's paint method as soon as possible .
or if you want for example numerous changes happen one by one dynamically then you could use the code below which is completely different from the things i said above. for that you could use platform.runlater() inside another thread which deals with everything that is about to change in realtime
new Thread(new Runnable()
{
#Override
public void run()
{
Platform.runLater(new Runnable()//use platform.runlater if you are using javafx
{
#Override
public void run()
{
try
{Thread.sleep(50);}catch(Exception e){}//use it in for loop where changes happen
//do some realtime change of components
}
});
}).start();
your Console class would be
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Console extends JPanel{
private JTextArea area;
public Console(){
this.setBorder(new TitledBorder("Console:"));
area=new JTextArea();
this.setLayout(new BorderLayout());
JScrollPane scroll=new JScrollPane(area);
this.add(scroll,BorderLayout.CENTER);
}
public void clear(){
area.setText("");
}
public void write(String txt){
area.append(txt+" "+"\n");
}
}
and the Calculator class is
public class Calculator {
private Frame parent;
public Calculator(Frame f){
parent=f;
}
public void calculate(){
new Thread(new Runnable() {
#Override
public void run()
{
for (int i = 0; i <= 100; i++) {
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
parent.writeOnConsole("Iteration "+i);
}
}
}).start();
}
}
as you can see i used another thread to do the changes
try the update method to call paint method for maintain every change

JFrame setSize as screen size but minus titlebar

I want to have JFrame with size that fits my screen perfectly. I used getScreenSize(); to get resolution of my computer and set it to JFrame's size. However i found that Jframe's size is actually bigger than my computer's resolution because of the titlebar. (which mean u will find bottom of the jframe is behind window taskbar)
The following code demostrate my problem :
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
is that possible to set the jframe size minus titlebar's size ?
Two ways are there:
1. Just "Maximize" the window using the code i.e. add the following:
this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );
Your code will look like this:
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
2. This option is to workaround i.e. by keeping always at the top then you will see this over the task bar i.e. adding the following:
this.setAlwaysOnTop(true);
Your code will be like this:
public class Titlebar extends JFrame {
private final Dimension _screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
this.setTitle("TitleBar");
this.setSize(_screenSize);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
Titlebar test = new Titlebar();
test.run();
}
}
Depending upon your need you can choose the option. But I believe option 1 will be good for you.

Java Swing: problems with width

I have problems with understanding the behavior of my application. I want to create a simple window (1000x700px), divided into two parts (250px and 750px width respectively). I tried the following code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JFrame
{
private static final long serialVersionUID = 1L;
public Example()
{
this.setSize(1000, 700);
this.setTitle("Example");
this.setResizable(false);
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel();
JPanel content_panel_wrap = new JPanel();
navigation_panel_wrap.setPreferredSize(new Dimension(250, 700));
content_panel_wrap.setPreferredSize(new Dimension(750, 700));
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
this.getContentPane().add(navigation_panel_wrap);
this.getContentPane().add(content_panel_wrap);
}
public static void main(String[] args)
{
Example example = new Example();
example.setVisible(true);
}
}
As you can see I manually set layout manager for JFrame (FlowLayout instead of BorderLayout with zero horizontal and vertical gaps). Of course, I can just use BorderLayout and than use add() method with BorderLayout.EAST and BorderLayout.WEST parameters, but I want to understand what's wrong with FlowLayout.
When I run my application, I get the following (no green JPanel):
If I decrease width of, for example, content_panel_wrap and make it 744px instead of 750px, everything works correctly.
So the question is - what are these strange 6 pixels? I'm not sure this value is constant for all operating systems, so I want to understand its origin.
There's nothing wrong with FlowLayout but you will need to call pack() for all components to be sized.
As for your codes problem (+1 to #Reimeus) calling pack() is the solution.
as per docs:
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. If the window and/or its owner are not yet
displayable, both are made displayable before calculating the
preferred size. The Window will be validated after the preferredSize
is calculated.
Tips:
Dont extend JFrame unnecessarily.
Use Event Dispatch Thread when creating and changing UI components:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// create UI components etc here
}
});
Dont call setPreferredSize() rather override getPrefferedSize() of component.
Dont call setSize(...) on JFrame rather call JFrame#pack() before setting it visible.
Dont forget to call JFrame#defaultCloseOperation(..) or your initial/EDT thread will not be terminated when JFrame is closed.
Here is an example combining my advice and your code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example {
private final JFrame frame;
public Example() {
frame = new JFrame();
frame.setTitle("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//app exited when frame closes
frame.setResizable(false);
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
JPanel navigation_panel_wrap = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 700);
}
};
JPanel content_panel_wrap = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(750, 700);
}
};
content_panel_wrap.setBackground(Color.green);
navigation_panel_wrap.setBackground(Color.red);
frame.add(navigation_panel_wrap);
frame.add(content_panel_wrap);
//pack frame (size components to preferred size)
frame.pack();
frame.setVisible(true);//make frame visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}

repaint() in Java [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Java GUI repaint() problem?
I write a Java code, but I have a trouble with GUI problem. When I add a component into JFrame object, then I call repaint() method in order to update the GUI but it doesn't work. But when I minimize or resize this frame, the GUI is updated.
Here is my code:
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(460, 500);
frame.setTitle("Circles generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
String input = JOptionPane.showInputDialog("Enter n:");
int n = Integer.parseInt(input);
CircleComponent component = new CircleComponent(n);
frame.add(component);
component.repaint();
}
If you added JComponent to already visible Container, then you have call
frame.getContentPane().validate();
frame.getContentPane().repaint();
for example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(460, 500);
frame.setTitle("Circles generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
String input = JOptionPane.showInputDialog("Enter n:");
CustomComponents0 component = new CustomComponents0();
frame.add(component);
frame.getContentPane().validate();
frame.getContentPane().repaint();
}
static class CustomComponents0 extends JLabel {
private static final long serialVersionUID = 1L;
#Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
#Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
}
Simply write :
frame.validate();
frame.repaint();
That will do .
Regards
You're doing things in the wrong order.
You need to first add all JComponents to the JFrame, and only then call pack() and then setVisible(true) on the JFrame
If you later added JComponents that could change the GUI's size you will need to call pack() again, and then repaint() on the JFrame after doing so.
You may need to call frame.repaint() as well to force the frame to actually redraw itself. I've had issues before where I tried to repaint a component and it wasn't updating what was displayed until the parent's repaint() method was called.

how can i program this ui box

in what way can this be programmed.
a UI box that displays random number between min and max value for 2 seconds then shows blank for 2 seconds then shows another random numer for 2 seconds then shows blank for 10 seonds and then repeats the cycle infitely until form closed. Font of the text to be configurable.
any help at all will be appreciated.
UPDATE after feedback
this is my progress so far. simple jpanel. now how do i add random number and timer
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RandomSlide {
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();
//Create a JFrame that we will use to add the JPanel
JFrame frame=new JFrame("Create a JPanel");
//ADD JPanel INTO JFrame
frame.add(panel);
//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 400 pixels
frame.setSize(400,400);
//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}
You have multiple languages in your tags, so I'm unsure what to repond with.
For java you'd make a JPanel with the appropriate UI elements and build a Timer instance to shoot off scheduled events every 2 seconds to update the value.
In VBA for Excel you'd have to do the same with a Form and Timer Control, however you may encounter more issues in Excel/VBA than in java.
Update 16/04/2010
You can actually sub-class the JPanel to clean up the code.
class RandomDialog extends JFrame
{
private Timer _timer;
private JPanel _container;
public RandomDialog()
{
_timer = new Timer();
_container = new JPanel();
// Etc...
}
}
From here you can instantiate your children and register an event on the timer to call a function on your class which generates the random number and displays it to a JLabel.
Then you can just call your dialog in your driver like so:
public static void main(string [] args)
{
RandomDialog rand = new RandomDialog();
rand.show();
}
Here is some code that will get you rolling.
Note that you could just extend a JLabel instead of JPanel. I used JPanel based on your question. Also, note that you may use timer.setDelay() API to change the timing. Also, you could stop the timer by a stop() call.
package answer;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class RandomPanel extends JPanel implements ActionListener {
private static final int TWO_SECONDS=2000;
private static final int MAX=99999;
private static final int MIN=0;
private Timer timer = new javax.swing.Timer(TWO_SECONDS, this);
private JLabel msgLabel;
Random generator = new Random();
public RandomPanel(Font f){
msgLabel = new JLabel();
msgLabel.setFont(f);
msgLabel.setText(rand());
add(msgLabel);
}
#Override
public void actionPerformed(ActionEvent e) {
msgLabel.setText(msgLabel.getText().equals("")?rand():"");
}
private String rand() {
//generate random beteween MIN & MAX
return Integer.toString((int) (MIN + Math.random() * ( MAX - MIN) + 0.5));
}
public void start(){
timer.start();
}
/**
* Rudimentary test
* #param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
RandomPanel randomPanel = new RandomPanel(new Font("Serif", Font.BOLD, 50));
frame.getContentPane().add(randomPanel);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
randomPanel.start();
}
}

Categories