I'm a beginner in Java, especially in the GUI Design area. I created a simple Java Swing interface to accept a string from the user and display it using a JOptionPane (ActionListener not yet implemented.)
The main problem I'm facing is with the alignment of the objects in the output. No matter what bounds I give to the objects, they always appear in one line. Also, sometimes, the output Frame will show absolutely nothing. After multiple runs, it will finally show me the objects, but not in the layout I expected them to be.
This is my code:
package guiapp;
import javax.swing.*;
import java.awt.*;
public class GUIApp {
private static JFrame frame;
private static JPanel panel;
private static JLabel label;
private static JTextField text;
private static JButton click;
public static void CreateGUI(){
frame = new JFrame("Hello to NetBeans!");
frame.setSize(750, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.BLUE);
label = new JLabel("Enter a string: ");
label.setBounds(50,35,150,40);
label.setVisible(true);
text = new JTextField();
text.setBounds(250,35,150,40);
text.setVisible(true);
click = new JButton("Click here!");
click.setBounds(150,80,150,40);
click.setVisible(true);
panel.add(text);
panel.add(label);
panel.add(click);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
CreateGUI();
}
}
Can someone please tell me where I'm going wrong? I seem to have got the layout syntax wrong.
No matter what bounds I give to the objects, they always appear in one
line.
This is probably because of the default layout manager of JPanel: FlowLayout. On the other hand Swing is designed to be used with layout managers and the use of methods such as setBounds(...), setLocation(...) and setSize(...) is discouraged. See Laying out Components within a Container lesson.
Related
This might be a very basic question. But I am stuck at this. The error that I get for the String variable display states:
Cannot refer to the non-final local variable display defined in an enclosing scope.
If I use a final keyword, I get the message:
The final local variable display cannot be assigned, since it is defined in an enclosing slope.*
The code is:
public class Frame {
public static void main(String[] args) {
String display=" ";
Frame ob=new Frame();
JFrame frame=new JFrame("Test");
frame.setBounds(300,100,800,500);
//Container c=frame.getContentPane();
frame.setLayout(null);
final JTextField name=new JTextField();
name.setBounds(500,212,150,20);
JLabel nameLabel=new JLabel("Name: ");
nameLabel.setForeground(Color.WHITE);
nameLabel.setBounds(450,171,100,100);
JTextField ohr=new JTextField();
ohr.setBounds(500,282,150,20);
JLabel ohrID=new JLabel("OHR ID: ");
ohrID.setForeground(Color.WHITE);
ohrID.setBounds(450,241,100,100);
final JButton button=new JButton("Submit");
button.setBounds(530,350,90,20);
frame.add(name);
frame.add(ohr);
frame.add(ohrID);
frame.add(nameLabel);
frame.add(button);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setVisible(true);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
display=name.getText();
JOptionPane.showMessageDialog(null, "Hi "+ display);
System.exit(0);
}
}
});
}
Thanks in advance!
There are multiple issues with your code, and we'll address them right here, right now and solve your problem at the same time.
public class Frame { this particular line has an error, Frame is the name of an AWT class, so it might confuse you or anyone who reads this code later on, give it a more meaningful name and avoid those names that could be confused with other Java packages.
Frame ob=new Frame(); you create an instance of your class and never use it again, why?
frame.setLayout(null); NEVER, please don't use null-layout, Swing has to deal with multiple PLAFs, screen sizes and resolutions, different OS, pixel perfect apps might seem like the easiest way to create complex UIs but later on you'll find that errors like this happen very often.
.setBounds(...) on every component, again, this is due to null-layout but it's better to use Layout managers
final JTextField name=new JTextField(); There's no need to declare any of your components as final, this is due to a poor design of your class, your components should be declared as class members (outside any method including main).
Speaking about main, separate your program into smaller pieces, don't throw everything at main or at the very least create a method that is not static so you can call it after creating an instance of your class (or else later on you'll end up with tons of static variables and that's a poor design of your class once again).
System.exit(0); it will stop the JVM, it's never a good idea to do that, it's better to .dispose() the JFrame and have your JFrame's defaultCloseOperation set to EXIT_ON_CLOSE which will safely dispose your app and then stop the JVM.
display=name.getText();, for this particular case, display could be an inner variable rather than a class member. This will solve your particular question
JOptionPane.showMessageDialog(null, "Hi "+ display); that null should be a reference to your JFrame, this will place your dialog in the middle of that JFrame rather than in the middle of the screen.
You never place your program inside the EDT, see point #2 in this answer.
So, having all the above points in mind, here's an improved version of your code.
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UsingVariablesInsideActionListenerExample {
//We declare our components here
private JFrame frame;
private JButton button;
private JTextField name;
private JTextField ohr;
private JLabel nameLabel;
private JLabel ohrID;
private JPanel pane;
private JPanel namePane;
private JPanel ohrPane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new UsingVariablesInsideActionListenerExample()::createAndShowGUI); //This is using Java 8 lambdas to place your program in the EDT
}
private void createAndShowGUI() {
frame = new JFrame("Test"); //Create your JFrame
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); //This will make this JPanel to arrange components vertically
namePane = new JPanel(); //By default, JPanels have FlowLayout which will arrange components horizontally
ohrPane = new JPanel();
name = new JTextField(10); //We create a JTextField with 10 columns
nameLabel = new JLabel("Name: ");
nameLabel.setForeground(Color.WHITE);
ohr = new JTextField(10);
ohrID = new JLabel("OHR ID: ");
ohrID.setForeground(Color.WHITE);
button = new JButton("Submit");
//Add the action listener
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String display = name.getText(); //The display variable is now an inner variable rather than a class member
JOptionPane.showMessageDialog(frame, "Hi " + display);
frame.dispose(); //We dispose the JFrame and it will be closed after due to EXIT_ON_CLOSE below.
}
}
});
//We add the components to the namePane (horizontally), the order matters
namePane.add(nameLabel);
namePane.add(name);
//Now we add these components to the ohrPane (horizontally again)
ohrPane.add(ohrID);
ohrPane.add(ohr);
//We then add the name and ohr panes to a bigger JPanel (pane, which if you remember will add them vertically) and we add the button at the end
pane.add(namePane);
pane.add(ohrPane);
pane.add(button);
//We make them non opaque (transparent) so that we can see the background color of the JFrame
namePane.setOpaque(false);
ohrPane.setOpaque(false);
pane.setOpaque(false);
frame.add(pane);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.pack(); //This will get every component's preferred size and make the JFrame as small as possible where it looks good on every OS, PLAF, screen size and resolution.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //We make the frame visible (always at the very end, when we've added everything to it).
}
}
And this is how it looks like now.
The UI may not be perfectly equal to the one you have, but I'm sure you can play with the different layout managers, and nest various JPanels to get a much better looking UI than mine, or at least a more similar one to the one you had.
Variable used in side an inner class should be effectively final . You can use a string[] of length 1 instead of string to resolve this . Please read bellow post for more details
Difference between final and effectively final
Also check this post for more details
Variable used in lambda expression should be final or effectively final
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
My JFrame is not displaying the button or background color that is set in the constructor. I am only getting a blank box when I start the program. Not sure what is wrong with the code.
//imports
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;;
public class StartingTheCode{
JButton CalculateButton;
JTextField Ans;
JPanel p;
JFrame f;
public static void main (String[] args){
new StartingTheCode();
}
//constructor
StartingTheCode(){
f = new JFrame("test");
f.setVisible(true);
f.setSize(600,600);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
p.setBackground(Color.BLUE); // not displaying blue background
CalculateButton = new JButton("+"); // should display button
CalculateButton.setSize(30,30);
CalculateButton.setLocation(5,5);
}
}
You're not adding your button or your JPanel to anything, and so no JFrame is magically going to display them.
You should add your JButton to your JPanel via its add(...) method, and then add the JPanel to the JFrame via its add(...) method, and do so before setting the JFrame visible.
Most importantly, you should read the Swing tutorials, since I speak from experience when saying you'll get no-where just guessing at this stuff. This is all well explained there.
As an aside, avoid setting the sizes of any components and instead read the tutorial section on use of the layout managers as it will allow you to simplify and empower your code greatly.
You need to add your calculateButton to the JPanel with p.add(calculateButton) and add the panel to the frame with f.add(p)
I think im heading in the wrong direction. Im creating a notepad app. I have every method running perfectly except one - WordWrap
Its just a JTextarea inside a panel inside a frame.
I think i should be using a JScrollPane instead of a Textarea? Or aswell as it even?
How would i go about resizing the width of a textarea or am i correct in saying i need to insert a JScrollPane.
Edit
Ok so my attempt is gone wrong somehow. Text area doesnt work. Something possibly needs resizing.
public class TextEditor extends JFrame implements ActionListener{
JFrame textFrame = new JFrame();
JPanel textPanel = new JPanel();
JTextField textArea = new JTextField();
JScrollPane scroll = new JScrollPane(textArea);
JTextArea text = new JTextArea(24,33);
public TextEditor(String str){
super(str);
textFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
textFrame.add(textPanel);
textPanel=(JPanel)getContentPane();
textPanel.setLayout(new FlowLayout());
textPanel.setBackground(Color.WHITE);
// Create text Area
textPanel.add(scroll);
scroll.add(text);
textPanel.setFont(textAreaFont);
textArea.setFont(textAreaFont);
text.setFont(textAreaFont);
}
public static void main(String args[])
{
TextEditor notePad = new TextEditor("Notepad");
notePad.setSize(500,500);
notePad.setVisible(true);
notePad.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Have a look at what I have tried to put together:
public class SO{
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel();
JTextArea outputArea = new JTextArea();
outputArea.setColumns(20);
outputArea.setRows(20);
outputArea.setLineWrap(true); //Set line wrap
outputArea.setWrapStyleWord(true); //set word wrap
JScrollPane sp = new JScrollPane(outputArea); //Create new scroll pane with textarea inside
p.add(sp); //add scrollPane to panel
f.add(p); //Add panel to frame
f.pack()
f.setLocationRelativeTo(null); //frame location
f.setVisible(true);
}
}
The scroll pane is created using the textarea in the constructor, this seems to allow the scroll pane to 'contain' the JTextArea, adding scroll bars when the text the area contains exceeds the limits. Earlier when creating the JTextArea I set two lines of code to set a word wrap on it, this stops words seeping off the sides by pushing them onto the next line. Have a look and see if it can help with your project.
Good Luck!
import java.awt.*;
import javax.swing.*;
public class TextEditor extends JFrame {
JFrame textFrame = new JFrame();
JPanel textPanel = new JPanel();
JTextArea textArea = new JTextArea(10,25);
public TextEditor(String str){
super(str);
textFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // nicer
add(textPanel);
textPanel.setLayout(new GridLayout());
textPanel.setBackground(Color.WHITE);
// Create text Area
JScrollPane scroll = new JScrollPane(textArea);
textPanel.add(scroll);
}
public static void main(String args[])
{
TextEditor notePad = new TextEditor("Notepad");
notePad.setVisible(true);
notePad.setDefaultCloseOperation(EXIT_ON_CLOSE);
notePad.pack();
}
}
There were so many things wrong in that short code that I lost track of the changes. Two things I can recall are:
The code was quite confused about what was a JTextField and what was a JTextArea.
It added strange things to other strange things for no apparent reason.
I am really new to GUI programming in Java, I did a lot of research and I couldn't find an answer to this problem.
I have a simple JFrame with a menu, and inside this JFrame I have a JPanel with a log in form (were users input their username and password), and then I want to change that JPanel to another JPanel depending on what users want to do.
What would be the best way of doing this? I think that stacking JPanels is OK. But after I add new JLayeredPanels in Netbeans they don't stack. I read somewhere that I should use Z ordering or something like that, but I can't find it on the designer view.
Well, thank you very much for your patience!
CardLayout class has a useful API that can serve your requirements. Using methods like next(), first(), last() can be helpful.
I've prepared a simple demonstration of changing panels within a parent panel and/or frame.
Take a look at it:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelChanger implements ActionListener
{
JPanel panels;
public void init(Container pane)
{
JButton switcher = new JButton("Switch Active Panel!");
switcher.addActionListener(this);
JPanel login = new JPanel();
login.setBackground(Color.CYAN);
login.add(new JLabel("Welcome to login panel."));
JPanel another = new JPanel();
another.setBackground(Color.GREEN);
another.add(new JLabel("Yeah, this is another panel."));
panels = new JPanel(new CardLayout());
panels.add(login);
panels.add(another);
pane.add(switcher, BorderLayout.PAGE_START);
pane.add(panels, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
CardLayout layout = (CardLayout)(panels.getLayout());
layout.next(panels);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("CardLayoutDemo");
PanelChanger changer = new PanelChanger();
changer.init(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}