How do I remove the empty space in a JFrame? - java

I'm relatively new to Java GUI programming, and although the concepts are coming along well in my head, there's this small issue I'm having with removing the empty space in a JFrame.
If you run the code I have below, you'll notice that there's a considerable gap between the JTextFields and the JButton. I'd like the remove this gap so that the button is touching the very bottom JTextField.
Also, another very minor question. How do I increase the height of the JTextFields so that they have three rows instead of just one?
Anyway here's the code.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HW4GUI extends JFrame
{
private JButton jbtAction;
private JTextField jtfFName;
private JTextField jtfLName;
private JTextField jtfLibNo;
private static int nextLibNo;
private JPanel textPanel;
public HW4GUI()
{
super("HW4GUI");
makeFrame();
showFrame();
}
public void makeFrame()
{
setLayout(new BorderLayout());
setResizable(false);
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3,2));
jbtAction = new JButton("Add Borrower");
JLabel FirstNameLabel = new JLabel("FirstName:");
jtfFName = new JTextField(3);
JLabel LastNameLabel = new JLabel("LastName:");
jtfLName = new JTextField(3);
JLabel LibNoLabel = new JLabel("Library Number:");
jtfLibNo = new JTextField(3);
FirstNameLabel.setHorizontalAlignment(JTextField.RIGHT);
LastNameLabel.setHorizontalAlignment(JTextField.RIGHT);
LibNoLabel.setHorizontalAlignment(JTextField.RIGHT);
jtfLibNo.setEditable(false);
textPanel.add(FirstNameLabel);
textPanel.add(jtfFName);
textPanel.add(LastNameLabel);
textPanel.add(jtfLName);
textPanel.add(LibNoLabel);
textPanel.add(jtfLibNo);
add(textPanel, BorderLayout.NORTH);
add(jbtAction, BorderLayout.SOUTH);
}
public void showFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}

Both of these are very easy fixes. For the first question, just set the second to "CENTER" instead of "SOUTH", and for the second question you can make it a JTextarea, which is multiple lines, or a JScrollPane which can scroll so it has a very large size.
P.S. you should always include a main method to run from like this one below.
public static void main(String[] args){
new HW4GUI();
}

Related

How to add to a JPanel from another class

I've been spending the past few hours trying to figure out how to use a panel in my GUI class from a class named algorithms. I know this question has been asked before, But I cant seem to figure out how to incorporate it into my code, so I would appreciate any form of guidance.
Specifically, in my algorithms class I was testing trying to change the JLabel named title by creating a instance of my GUI class and then trying to use title.setText(), however it returns a Null Pointer Exception.
The title JLabel is located on my JPanel named topPanel
I know I'm not doing something right, I've struggled with this for the past few hours and cant seem to make any progress. Like I said, any guidance would be appreciated.
GUI.java
import javax.swing.*;
import java.awt.*;
import java.util.Date;
public class GUI extends JFrame {
public JFrame myFrame;
public JPanel firstFitDisplay,topPanel;
public JLabel title;
public Timer timer;
public int count=0;
public GUI(){
}
public void initGUI(){
JFrame myFrame = new JFrame();
myFrame.setTitle("CIS 452 Dynamic Memory Allocation Project");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setResizable(true);
myFrame.setLayout(new BorderLayout(6,6));
myFrame.setSize(1000,700);
myFrame.setVisible(true);
//myFrame.getContentPane().setBackground(Color.white);
//setup panels
JPanel infoPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel bottom = new JPanel();
infoPanel.setBackground(Color.cyan);
centerPanel.setBackground(Color.red);
topPanel.setBackground(Color.yellow);
bottom.setBackground(Color.pink);
infoPanel.setPreferredSize(new Dimension(200,1000));
centerPanel.setPreferredSize(new Dimension(500,500));
topPanel.setPreferredSize(new Dimension(750,20));
bottom.setPreferredSize(new Dimension(750,25));
//setup layout for panels, so that we can add subpanels
infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.PAGE_AXIS));
//add panels to main frame
myFrame.add(infoPanel,BorderLayout.WEST);
myFrame.add(centerPanel,BorderLayout.CENTER);
myFrame.add(topPanel,BorderLayout.NORTH);
myFrame.add(bottom,BorderLayout.SOUTH);
// setup sub panels for infoPanel
JPanel infoSubPanel = new JPanel();
infoSubPanel.setBackground(Color.pink);
infoSubPanel.setPreferredSize(new Dimension(50,90));
infoPanel.add(infoSubPanel);
//setting up text for infoSubPanel
JLabel subPanelTitle = new JLabel("Next Process Size to be allocated");
JLabel firstFitNextUpLabel = new JLabel("First:0");
JLabel bestFitNextUpLabel = new JLabel("Best:0");
JLabel worstFitNextUpLabel = new JLabel("Worst:0");
infoSubPanel.add(subPanelTitle);
infoSubPanel.add(firstFitNextUpLabel);
infoSubPanel.add(bestFitNextUpLabel);
infoSubPanel.add(worstFitNextUpLabel);
//subClockPanel
JPanel infoSubClockPanel = new JPanel();
infoSubClockPanel.setBackground(Color.GRAY);
infoSubClockPanel.setPreferredSize(new Dimension(50,90));
infoPanel.add(infoSubClockPanel);
//setting up text for sub clock panel
JLabel clockText = new JLabel("Seconds passed: ");
int ten = 10;
JLabel clockCounter = new JLabel("0");
infoSubClockPanel.add(clockText);
infoSubClockPanel.add(clockCounter);
//logic for running timer;
timer = new Timer(1000, e -> {
clockCounter.setText(String.valueOf(count++));
});
timer.start();
//setting up sub panels for the main center panel
centerPanel.setLayout(new FlowLayout());
JPanel firstFitDisplay = new JPanel();
JPanel bestFitDisplay = new JPanel();
JPanel worstFitDisplay = new JPanel();
firstFitDisplay.setPreferredSize(new Dimension(200,500));
bestFitDisplay.setPreferredSize(new Dimension(200,500));
worstFitDisplay.setPreferredSize(new Dimension(200,500));
centerPanel.add(firstFitDisplay);
centerPanel.add(bestFitDisplay);
centerPanel.add(worstFitDisplay);
//center components
centerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
firstFitDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
//setup title
JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
topPanel.add(title);
}
}
and here is algorithms.java
import javax.swing.*;
import java.awt.*;
public class algorithms {
public static void main(String[] args) {
GUI f = new GUI();
f.initGUI();
f.title.setText("HHHHHHHHHHH");
}
}
so your mistake is that you never define title in your GUI class. I think you intended to with this line:
JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
This actually creates a new local variable called title instead of defining the global one. So the global variable is still null. To fix this simply change that to like to this:
this.title = new JLabel("Dynamic Memory Allocation Simulator");
Now it will define the global variable! This shouldn't give a null pointer exception now.

Java Button Placement using BorderLayout

This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.
package test;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("");
textfield = new JTextField("enter text here");
JPanel bottom = new JPanel(new BorderLayout());
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel (new BorderLayout());
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom, BorderLayout.PAGE_END);
centre.add(subCentre, BorderLayout.CENTER);
}
}
Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:
You never added the panels to the frame
You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.
One simple way to achieve your desired UI is like so(I added a main method to show the window):
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public Test() {
super("test");
JPanel buttons = new JPanel();
JPanel centre = new JPanel();
add(buttons, BorderLayout.SOUTH); //these lines add the
add(centre, BorderLayout.CENTER); //panels to the frame
JButton clear = new JButton("Clear"); // No need
JButton copy = new JButton("Copy"); // to declare
JLabel label = new JLabel("Label"); // these
JTextField textfield = new JTextField("enter text here"); // privately
buttons.add(copy);
buttons.add(clear);
centre.add(label);
centre.add(textfield);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//added main method to run the UI
public static void main(String[] args) {
new Experiments();
} //end main
} //end class
And it shows the window:
I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?
package lab6;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("label");
textfield = new JTextField("enter text here");
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(100, 200));
JPanel bottom = new JPanel();
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel ();
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom);
centre.add(subCentre);
masterPanel.add(bottom, BorderLayout.PAGE_END);
masterPanel.add(top, BorderLayout.PAGE_START);
masterPanel.add(centre, BorderLayout.CENTER);
add(masterPanel);
}
}

GUI not showing as intended

I'm trying to draw a gui like shown in the figure, but somehow I'm not able to place the objects in right place (I guess that the problem is with the layout) the textArea is suppose to go in the middle... but is not showing at all
package Chapter22Collections;
import javax.swing.*;
import java.awt.*;
public class Exercise226 extends JFrame {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
public Exercise226() {
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
jTextDisplay = new JTextArea();
jTextAdd = new JTextField(8);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
add(p1, BorderLayout.NORTH);
add(jTextDisplay, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
}
public static void main(String... args) {
Exercise226 gui = new Exercise226();
gui.setTitle("Numbers");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 200);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
}
The JTextArea is actually where you expect it to be but has no outline border. It is usual to place the component in a JScrollPane which will give this effect:
add(new JScrollPane(jTextDisplay), BorderLayout.CENTER);
or simply
add(new JScrollPane(jTextDisplay));
To make the textArea re-size with the window, try BoxLayout. Box is "A lightweight container that uses a BoxLayout object as its layout manager."
Box p1 = new Box(BoxLayout.X_AXIS);
How could I add spacing/padding between the elements in the frame? So the text area is more visible and centered.
Borders and padding. E.G.
Compared with:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class Exercise226 {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
private JPanel gui;
public Exercise226() {
gui = new JPanel(new BorderLayout(5,5));
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
// set the size constraints using columns/rows
jTextDisplay = new JTextArea("Here I am!", 6,20);
jTextAdd = new JTextField(8);
JPanel p1 = new JPanel(new GridLayout(1,3,3,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3,3,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
JPanel textAreaContainer = new JPanel(new GridLayout());
textAreaContainer.add(new JScrollPane(jTextDisplay));
textAreaContainer.setBorder(new TitledBorder("Text Area Here"));
gui.add(p1, BorderLayout.PAGE_START);
gui.add(textAreaContainer, BorderLayout.CENTER);
gui.add(p2, BorderLayout.PAGE_END);
gui.setBorder(new EmptyBorder(4,4,4,4));
}
public Container getGui() {
return gui;
}
public static void main(String... args) {
JFrame f = new JFrame();
Exercise226 gui = new Exercise226();
f.setContentPane(gui.getGui());
f.setTitle("Numbers");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
This code:
Primarily provides 'white space' in the GUI using different constructors for the layouts that accept 2 int arguments for horizontal & vertical spacing.
Also adds 2 borders:
An empty border around the entire GUI to provide some spacing between it and the frame decorations.
A titled border around the text area, to make it very obvious.
Does implement a change for one unnecessary part of the original code. Instead of extending frame, it simply retains an instance of one.
Uses the JScrollPane container for the text area, as suggested by #Reimeus. It adds a nice beveled border of its own to an element that needs no scroll bars.
Creates a textAreaContainer specifically so that we can set a titled border to surround the scroll pane - without interfering with its existing border. It is possible to use a CompoundBorder for the scroll pane that consists of the existing border (scroll.getBorder()) & the titled border. However that gets complicated with buttons & other elements that might change borders on selection or action. So to set an 'outermost border' for a screen element (like the text area here) - I generally prefer to wrap the entire component in another container first.
Does not create and show the GUI on the EDT. Swing GUIs should be created and modified on the EDT. Left as an exercise for the user. See Concurrency in Swing for more details.
Old Code
The original code on this answer that provides the 'comparison GUI image' seen above. IT is closely based on the original code but with the text area wrapped in a scroll pane (and gaining a beveled border because of that) & given some text to display.
import javax.swing.*;
import java.awt.*;
public class Exercise226 extends JFrame {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
public Exercise226() {
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
// set the size constraints using columns/rows
jTextDisplay = new JTextArea("Here I am!", 6,20);
jTextAdd = new JTextField(8);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
add(p1, BorderLayout.NORTH);
add(new JScrollPane(jTextDisplay), BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
}
public static void main(String... args) {
Exercise226 gui = new Exercise226();
gui.setTitle("Numbers");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//gui.setSize(300, 200);
gui.pack();
//gui.setLocationRelativeTo(null);
gui.setLocationByPlatform(true);
gui.setVisible(true);
}
}

JScrollPane component change

i have a JScrollPane.
But default it displays JTextArea.
JTextArea jTextArea = new JTextArea();
JScrollPane pane = new JScrollPane(jTextArea);
so here everything is fine. But now i would like to change JScrollPane component by user action:
pane.remove(jTextArea);
pane.add(new JTable(data[][], columns[]));
pane.revalidate();
frame.repaint();
frame in my main Window. JScrollPane is added to main window with GridBagLayout.
But this doesn't work. After running action JScrollPane becomes grey.
jScrollPane.getViewport().remove/add
One alternative is to put a JPanel with a CardLayout1 into the JScrollPane, add both the components to the panel, then simply flip between the text area and table as needed.
How to Use CardLayout
Given the components might be of vastly different size, it might be better to do it this way:
JPanel with CardLayout contains many JScrollPane instances, each of which contains a single component. That will also work inherently better for a JTable.
Edited my answer after receiving one valuable suggestion by His Majesty #camickr, setViewportView(componentObject); is used to do such things.
A sample code to help your cause :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollPaneExample extends JFrame
{
private JPanel panel;
private JScrollPane scrollPane;
private JTextArea tarea;
private JTextPane tpane;
private JButton button;
private int count;
public ScrollPaneExample()
{
count = 0;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
panel = new JPanel();
panel.setLayout(new BorderLayout());
tarea = new JTextArea();
tarea.setBackground(Color.BLUE);
tarea.setForeground(Color.WHITE);
tarea.setText("TextArea is working");
scrollPane = new JScrollPane(tarea);
tpane = new JTextPane();
tpane.setText("TextPane is working.");
button = new JButton("Click me to CHANGE COMPONENTS");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (count == 0)
{
scrollPane.setViewportView(tpane);
count++;
}
else if (count == 1)
{
scrollPane.setViewportView(tarea);
count--;
}
}
});
setContentPane(panel);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ScrollPaneExample();
}
});
}
}
Hope this might help you in some way.
Regards

JTabbedPane JLabel, JTextField

Right, I have a JTabbedPane that has a JPanel that contains a JLabel and a JTextField.
my code
JTabbed Pane declaration :
this.tabPane = new JTabbedPane();
this.tabPane.setSize(750, 50);
this.tabPane.setLocation(10, 10);
tabPane.setSize(750,450);
tabPane.add("ControlPanel",controlPanel);
textfield declaration :
this.channelTxtFld = new JTextField("");
this.channelTxtFld.setFont(this.indentedFont);
this.channelTxtFld.setSize(200, 30);
this.channelTxtFld.setLocation(200, 10);
JLabel :
this.channelLabel = new JLabel("Channel name : ");
this.channelLabel.setSize(150, 30);
this.channelLabel.setLocation(10,10);
private void createPanels() {
controlPanel = new JPanel();
controlPanel.setSize(650,500);
}
private void fillPanels() {
controlPanel.add(channelLabel);
controlPanel.add(channelTxtFld);
}
So what my plan is, was to have a tabbed pane that has a JPanel where I have some Labels, textfields and buttons on fixed positions, but after doing this this is my result:
http://i.stack.imgur.com/vXa68.png
What I wanted was that I had the JLabel and next to it a full grown JTextfield on the left side not in the middle.
Anyone any idea what my mistake is ?
thank you :)
What kind of Layout Manager are you using for your controlPanel, you probably want BorderLayout, putting the Label in the West, and the TextField in the center.
BTW, setting the size and position of various components doesn't make sense unless you are using a Null Layout, which isn't a good idea. So i'd remove all that stuff and let the Layout Manager do it for you.
Use a LayoutManager and consider also the methods setPreferredSize, setMinimumSize, setMaximumSize to adjust components bounds according on which is your desired effect.
Assuming the default JPanel layout, FlowLayout, give the JTextField a non-zero number of columns, and give the JLabel a JLabel.LEFT constraint.
Addendum:
a full grown JTextField
Something like this?
import java.awt.*;
import javax.swing.*;
/**
* #see http://stackoverflow.com/questions/5773874
*/
public class JTabbedText {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
private final JTabbedPane jtp = new JTabbedPane();
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtp.setPreferredSize(new Dimension(400, 200));
jtp.addTab("Control", new MyPanel("Channel"));
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class MyPanel extends JPanel {
private final JLabel label = new JLabel("", JLabel.LEFT);
private final JTextField text = new JTextField();
public MyPanel(String name) {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
label.setText(name);
label.setAlignmentY(JLabel.TOP_ALIGNMENT);
text.setAlignmentY(JTextField.TOP_ALIGNMENT);
this.add(label);
this.add(text);
}
}
}

Categories