Getting stared with GUI programming in JAVA - java

When I compile the code below in Eclipse, I expect a window to appear with a button on the upper left corner of the window. Instead, the button has the same size as the window and fills the window completely, although I suppose to have restricted the size of the button in line 11 with "button.setBounds(20, 20, 200, 50);". Can anybody tell me what I did wrong or what I forgot?
import javax.swing.*;
import java.awt.*;
public class BeobachterGUI {
public BeobachterGUI() {
JFrame frame = new JFrame("A frame");
frame.setSize(new Dimension(600, 600));
JButton button = new JButton("Click me");
button.setBounds(20, 20, 200, 50);
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BeobachterGUI();
}
});
}
}

JFrames automatically give themselves a default BorderLayout, which is what is making your button fill the entire screen. If you want to have direct control over the size and position of your button, you have to get rid of that layout manager. Use this line of code:
frame.getContentPane().setLayout(null);
A word of advice, though: working without a layout manager is OK for fun and experimentation, but you shouldn't make a habit of it. You'll get a better GUI (and the users of your GUI get a better experience) if you learn to design with layout managers.
To see an example of what I mean, just watch what happens when you resize your window: if you make the window too small, the button can get cut off or hidden completely. A layout manager would try to reposition and/or resize the button so it stays visible.

Related

How can I add ScrollBar to JTextArea in Java Swing?

Anyone help me how to add a scroll bar to a JTextArea with Swing in Java?
The JTextArea just disappear when I add the scroll bar on it.
Hope somebody get me add a vertical scrollbar on it.
Additional explanation will be very thankful
public class Practice extends JFrame {
JFrame frame = new JFrame("AAA");
JTextArea textarea = new JTextArea();
JScrollPane scroll = new JScrollPane(textarea);
JPanel panelForScroll = new JPanel(null);
public Practice(){
frame.setLayout(null);
frame.setBounds(100,100,400,710);
frame.setResizable(false);
frame.setVisible(true);
textarea.setEditable(false);
textarea.setFont(new Font("arian", Font.BOLD, 16));
textarea.setBounds(20, 280, 340, 70);
panelForScroll.add(scroll);
frame.add(panelForScroll); //can't find text area....
}
public static void main(String[] args) {
new Practice();
}
}
There are several errors in your code:
You're using a null layout, this is discouraged as it produces more problems than solutions, specially when you try to use JScrollPanes, since they take the preferredSize of the Component to decide whether to add the scroll bars or not. See Why is it frowned upon to use a null layout in Swing? for more information about this. To fix this, remove this line:
frame.setLayout(null);
And instead use a layout manager or combinations of them along with borders for extra spacing between components.
While null layouts might seem like the best, easiest and faster way to design complex GUIs for Swing newbies, the more you progress in it, the more problems related to the use of them you'll find (as it's the case)
You're extending your class from JFrame and you're creating an instance of JFrame in it too, please use one or the other. When you extend JFrame you're saying your class is a JFrame and thus it cannot be placed inside another Container because JFrame is a rigid container. I recommend to forget the extends JFrame part, since anyway you're not using the JFrame that is generated by this action and stay with the object you created. See https://stackoverflow.com/questions/41252329/java-swing-using-extends-jframe-vs-calling-it-inside-of-class for a more detailed answer about this problem.
You're making your GUI visible before you have added all the elements, this could cause your GUI to not display all the elements until you hover over them, this line:
frame.setVisible(true);
Should be one of the last lines in your program
You're not placing your program on the Event Dispatch Thread (EDT) which makes your application to not be thread safe, you can fix it by writing this on your main method.
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Place your constructor here
}
});
You're setting bounds for the textArea but not for the scrollPane, but you should really not be setting the bounds manually (see point #1 again).
Now, you can make a simple GUI with a JTextArea with a JScrollPane as follows:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ScrollPaneToTextArea {
private JTextArea textArea;
private JFrame frame;
private JScrollPane scroll;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ScrollPaneToTextArea().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame("ScrollPane to TextArea");
textArea = new JTextArea(10, 20); //Rows and cols to be displayed
scroll = new JScrollPane(textArea);
// scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll); //We add the scroll, since the scroll already contains the textArea
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Which produces this output and the scroll bars are added when needed (i.e. when text goes further than the rows it can handle in the view)
If you want the vertical scroll bars to appear always you can uncomment the line:
scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Which will produce the following outputs:
You can read more about JScrollPane in the docs and JTextArea also in their own docs.
JPanel panelForScroll = new JPanel(null);
This sets the NULL Layout to this JPanel. This would require more configuration (just as you did for the frame object).
Just remove the null (also from frame.setLayout(null)!)
You have to use Jtextpane to get the scroll bar on textarea.
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
JFrame f = new JFrame();
f.getContentPane().add(sp);
you are setting the panel's layout to null,then you didn't specify the scroll bar bounds. Since you only have one component in your panel which is the scroll bar I recommend using FlowLayout

Open second JFrame and components don't show

Before you say something, i know the implications of having more than one JFrame. I'm kinda delayed and i need to add the components manually.
So, i open a JFrame that i have designed with a button click:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame DataCalc = new JFrame();
DataCalc.setVisible(true);
DataCalc.setSize(500, 500);
DataCalc.setLocationRelativeTo(null);
}
Then the JFrame shows up but doesn't show my components. I read that if i setVisible before adding components they won't show, but they're already there cause i designed them.
If i change my code and add the setSize and setLocation like the following code, nothing happens besides the JFrame opening.
public DataCalc() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
initComponents();
}
Sry for the post, i'll edit my post if you need more info.
JFrame DataCalc = new JFrame();
Should be:
JFrame dataCalc = new DataCalc(); // use the CUSTOM frame!

JFrame position three buttons one below another in the center

So I would like to have three JButtons all on top of each other, but not to large in width or height either. I am not too familiar with Java's layouts, and to be honest I am not too keen on them. Please view the image a code below to explain to me how, thanks.
package com.aqagame.harrykitchener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main
{
private JButton playGame, playerNames, exitGame;
public Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
playGame = new JButton("Play Game");
playerNames = new JButton("Player Names");
exitGame = new JButton("Exit Game");
mainCard.add(playGame, BorderLayout.NORTH);
mainCard.add(playerNames, BorderLayout.CENTER);
mainCard.add(exitGame, BorderLayout.SOUTH);
JFrame window = new JFrame("Harry's AQA game");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(mainCard);
window.setSize(900, 800);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
Check out the Java Documentation for the different layout managers. I know you're not familiar with them, which is why you should probably start. Once you get used to them there is no end to their benefits. There is a lot of excellent information in the documentation and I am sure you will learn a lot. Personally, I recommend looking at the Box Layout:
Create JPanel that uses a GridLayout and add all the buttons to the panel. The GridLayout will automactially size the buttons to be the same size.
Set the layout manager of your main window to use a GridBagLayout.
add the panel to the main window using the default GridBagConststraints. Then the panel will automatically be centered both horizontally and vertically.
To not use Box or GridBag, I think a combination such as this may work out:
Have main panel (let's call it A) have a BorderLayout
Create another panel (let's call it B), with a FlowLayout, with constructor aligning components to the center
Create another panel (let's call it C), with a GridLayout, 1 column 3 rows
Add each button to a new JPanel with a FlowLayout (1 JPanel per button, so buttons are wrapped by a FlowLayout), and then add each of those JPanels to C
Add C to B
Add B to A (center position)
I think this should cause buttons to be on top of each other with small amount of padding while not being stretched widthwise and while appearing in the center of the screen.

In JAVA, JButton, Button displayed only when cursor is on the button and works even on clicking anywhere in contentPane

I am a beginner into Java and OOPS in general. Am studyin Head First Java to start, and studying GUI and Swing concepts in it.
The below code is just for understanding purposes.
On running the code, The frame window is displayed with Button, and when I expand it I can see Radio Button too.
Issues-
Button works till the window size is not more than the button size . As soon as I increase the window size even slightly more than button's dimensions, then the button is displayed only when cursor is on it.
I am changing window size using mouse.
Also even if I set Frame size to be more than button. say frame.setSize(800,800); then the button covers whole contentPane. and still behaves same way on resizing.
And the button responds to clicking on mouse, irrespective of where I click in the contentPane. It should respond only when i click directly on the button.
Please inform me why it is behaving this way.
And if possible,corrections in code or additions to correct this.
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
public class Test1 implements ActionListener {
JFrame frame = new JFrame("Frame");
JButton button = new JButton("Button!");
JRadioButton radio = new JRadioButton("VideoKilledTheRadioStar!",true);
int j=0;
public static void main(String[] args) {
Test1 t = new Test1();
t.method1();
}
public void method1()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setSize(100,100);
button.setBackground(Color.ORANGE);
frame.add(button);
frame.setSize(100,100);
frame.setVisible(true);
button.addActionListener(this);
frame.getContentPane().add(radio);
radio.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{j++;
button.setText("clicked .. " + j);
if(button.getBackground()==Color.ORANGE)
button.setBackground(Color.BLUE);
else
button.setBackground(Color.ORANGE);
}
}
P.S I did not know which segment of code is important or more relevant to this question, so I have put complete code.
You are trying to add the JButton button and JRadioButton objects in the default layout(BorderLayout) of the JFrame.
Whenevery you add a component to JFrame having BorderLayout the components goes in the Middle Section and BorderLayout center section has tendency to occupy the complete space, so to position elements properly you will need to specify the location as well as set the PreferredSize of the component.
frame.add(radio, BorderLayout.SOUTH);
component.setPreferredSize(Dimension);
You are adding the JButton button and the JRadioButton both in the BorderLayout.CENTER location so only one is being displayed. Components at this location will be sized in the X and Y axis.
The JButton only displays when the cursor is over it due to the fact that it has its own MouseListener used for painting.
Also, the statements
frame.add(myComponent);
and
frame.getContentPane().add(myComponent);
both add the component to the frame's ContentPane & are equivalent but the first is chosen for convenience.
Note that components cannot co-exist in the same position in a BorderLayout. You could place the button at the BorderLayout.SOUTH position (& add directly to the frame):
frame.add(radio, BorderLayout.SOUTH);
BorderLayout disregards any preferred sizes for components so you would have to use a different layout manager such as BoxLayout to maintain a fixed size JButton.
See more about Layout Managers

JFrame is very tiny when restore down is pressed

My question is why when I press to the restore down (on a Windows platform) the JFrame is very tiny (see the bellow screenshot).
I use this code regarding State of the JFrame:
this.setExtendedState(View.MAXIMIZED_BOTH);
I need to use setMinimumSize()?
Be sure that this.setExtendedState(JFrame.MAXIMIZED_BOTH) is executed AFTER setVisible(true). Also if you have a setResizable(false) call make sure it is executed AFTER the setExtendedState() one.
I think no one understood his question. He doesn't want to maximize it, he wants when he press "Restore Down", the window won't become so small. So what you need to do, "Restore Down" restores the window to its previous size before "Maximize" has been pressed.
So what I did was this
int Width = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int Height = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setSize(Width-100,Height-100);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
Therefore, every time you press Restore Down, the first time, the size will be the screen size - 100 pixels for width and height.
What class is View?
If you are trying to set your JFrame size to maximum do this (notice I used the static variable available in JFrame class and not View):
JFrame frame=...;
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
As this was the first and most accurate question to my issue, I will add what I found here. I know it is 3 years old but my answer might help someone in the future. My issue was the same as above. When I built the Jframe I used:
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
When I clicked the "restore down" button the window was really small and in the top left corner of the screen. I fixed this by calling:
frame.setBounds(100, 100, 450, 300);
After setVisible(true). Here is the code after my editing, the original code was created with Eclipse WindowBuilder. I am just editing it after. Here is the code complete:
public class MainWindow extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
frame.setBounds(100, 100, 450, 300);
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
//setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
Basically what is happening (as far as I can determine) after the Jframe becomes visible it is first set to a size of 450x300, then immediately changes the size to maximized. Now when you hit the restore down button it will set it to the 450x300 size. I am currently searching for a way to override the method used to set the window size to the previous size (before hitting max), but I'm not at a point that I need to ask yet.
appFrame = new JFrame("BufferedImage layers");
Dimension asRestoredDown = new Dimension(400,400);//first time restored
appFrame.setMinimumSize(asRestoredDown);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
appFrame.setExtendedState(appFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);

Categories