Can't get JPanel to display Entire Content - java

To put Basically, In Java, I have 3 different JPanels.
A Label with a JTextField in Panel 1,
A Button Panel in Panel 2,
and a second JTextField in Panel 3.
In Panel 1, I'm trying to Place the Label over top the JTextField so both components can become visible in one Panel. When I added my buttons to the Second Panel, Panel 2 overlayed the TextField or rather it wasn't visible. I tried setting the panels to different border Layouts and looked into trying SpringLayout, though either the Label is getting overwritten, or the JTextField itself.
Is there an answer to this Dilemma or something I haven't tried?
[Code Added]
public class Finale extends JApplet implements ActionListener{
private JButton PostP1 = new JButton("Post Position 1");
private JButton PostP2 = new JButton("Post Position 2");
private JButton PostP3 = new JButton("Post Position 3");
private JButton PostP4 = new JButton("Post Position 4");
private JTextField ReadOut = new JTextField("Hello");
public Finale(){
JPanel LabelPane = new JPanel();
JPanel ButtonPane = new JPanel();
ButtonPane.add(PostP1);
ButtonPane.add(PostP2);
ButtonPane.add(PostP3);
ButtonPane.add(PostP4);
JLabel Welcome = new JLabel("example text");
LabelPane.add(Welcome);
LabelPane.add(ReadOut);
add(LabelPane, BorderLayout.NORTH);
add(ButtonPane, BorderLayout.SOUTH);
}

Related

How do I use nested panels in my GUI application?

I created a drawing application that lets a user choose pen colors but I have having trouble with the layout. I created multiple panels but when I run it, all the buttons are still in one panel. Is there a way to fix this?
public class DrawingGUI extends JPanel {
private JRadioButton penColor1, penColor2, penColor3, randomPenColor, eraser;
private JButton clearButton;
private static Color defaultColor = Color.BLACK;
private static boolean isRandomSelected = false;
private final static int DIAMETER = 12;
protected static boolean canDraw;
private ArrayList<PointTracker> points;
public DrawingGUI() {
setBackground(Color.WHITE);
points = new ArrayList<PointTracker>();
JPanel drawPanel = new JPanel();
JLabel instructions = new JLabel("Enter your information:");
JPanel instructionsPanel = new JPanel();
instructionsPanel.add(instructions);
drawPanel.add(instructionsPanel);
JPanel colorPanel1 = new JPanel();
penColor1 = new JRadioButton("Red");
drawPanel.add(penColor1);
penColor1.addActionListener(new ToolListener());
drawPanel.add(colorPanel1);
JPanel colorPanel2 = new JPanel();
penColor2 = new JRadioButton("Blue");
drawPanel.add(penColor2);
penColor2.addActionListener(new ToolListener());
drawPanel.add(colorPanel2);
JPanel colorPanel3 = new JPanel();
penColor3 = new JRadioButton("Yellow");
drawPanel.add(penColor3);
penColor3.addActionListener(new ToolListener());
drawPanel.add(colorPanel3);...(So on)
all the buttons are still in one panel
Why is that a problem. That is what I would expect to happen.
Why are you creating a separate panel for each button? The whole point of using panel is to logically group components.
So I would expect you should have something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add( button1 );
buttonsPanel.add( button2 );
buttonsPanel.add( button3 );
JPanel drawPanel = new JPanel();
drawPanel.add( component1 );
drawPanel.add( component2 );
frame.add(drawPanel, BorderLayout.PAGE_START);
frame.add(buttonsPanel, BorderLayout.PAGE_END);
Above is a simple example of "nesting" two panels on a frame. Each of the panel can use a different layout manager as required.
For a working example of this approach you can check out Custom Painting Approaches. Both code examples show how you can "nest" a drawing panel and a buttons panel in a frame.

How to exactly make JComponents background transparent?

There are two things that I am trying to figure out. First thing, I want to figure out how to make a Jcomponent background transparent. When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. I tried using the firstpanel.setOpache function and repaint but it doesn't do anything.
And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. The images below will show what I am trying to describe. I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . The code below is what I am practicing with.
import javax.swing.*;
import java.awt.*;
public class LearningFrame extends JFrame {
private JLabel userLabel;
private LearningFrame(){
JPanel backGroundPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
userLabel = new JLabel("User");
userLabel.setFont(new Font("Arial",1 ,24));
backGroundPanel.setBackground(new Color(247,211,53));
// backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));
setContentPane(backGroundPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,800);
setLocationRelativeTo(null);
//backGroundPanel.setLayout(null);
mainPanel.setLayout(new GridLayout(1,2));
firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));
// firstPanel.setBackground(new Color(211,43,185));
secondPanel.setBackground(new Color(34,233,44));
JButton button = new JButton("Click");
JButton button2 = new JButton("Click");
JButton button3 = new JButton("Click");
JButton button4 = new JButton("Click");
firstPanel.add(button);
firstPanel.add(button2);
firstPanel.add(userLabel);
secondPanel.add(button3);
secondPanel.add(button4);
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
backGroundPanel.add(mainPanel);
setVisible(true);
}
public JPanel logPanel() {
JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField userTextField = new JTextField(14);
JTextField passTextField = new JTextField(14);
userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
passLabel.setFont(new Font("Arial", Font.BOLD, 24));
userLabel.setFont(new Font("Arial", Font.BOLD, 24));
logInPanel.add(userLabel);
logInPanel.add(userTextField);
logInPanel.add(passLabel);
logInPanel.add(passTextField);
logInPanel.setOpaque(true);
logInPanel.repaint();
return logInPanel;
}
public static void main(String[] args){
LearningFrame x = new LearningFrame();
}
}
Just use
firstPanel.setOpaque(false);
This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it.

Multiple JPanel inside another

look here
I have to make a format like this, like make a 1,2 panel, then put a 3,1 panel on the left side. Then, I put 4 text fields in the middle left panel and one button in the bottom left panel.
I'm not sure if I can just use GridLayout or if I have to use BorderLayout too.
How can I get this organized because when I set it up, I can't get the layout right. The textfields are not in the right position, they end up on the right side.
public class CreatePanel extends JPanel
{
private Vector projectList;
private JButton button1;
private ProjectSpendingPanel spendingPanel;
private JFrame frame1;
JPanel panel1;
JPanel leftPanel;
JPanel subPanel;
GridLayout layout1;
BorderLayout layout2;
GridLayout layout3;
JLabel message;
JLabel labelName;
JLabel labelNumber;
JLabel labelLocation;
JLabel labelFunding;
JTextField textField1;
JTextField textField2;
JTextField textField3;
JTextField textField4;
//Constructor initializes components and organize them using certain layouts
public CreatePanel(Vector projectList, ProjectSpendingPanel spendingPanel)
{
this.projectList = projectList;
this.spendingPanel = spendingPanel;
//organize components here
layout1 = new GridLayout(1,2);
layout2 = new BorderLayout(1,3);
layout3 = new GridLayout(4,2);
this.setLayout(layout1);
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
labelName = new JLabel("Project Name");
leftPanel.add(labelName);
textField1 = new JTextField("", 15);
leftPanel.add(textField1,BorderLayout.SOUTH);
labelName = new JLabel("Project Number");
leftPanel.add(labelName);
textField2 = new JTextField("",15);
leftPanel.add(textField2);
labelLocation = new JLabel("Project Location");
leftPanel.add(labelLocation);
textField3 = new JTextField("",15);
leftPanel.add(textField3);
labelFunding = new JLabel("Initial Funding");
leftPanel.add(labelFunding);
textField4 = new JTextField("",15);
leftPanel.add(textField4);
add(leftPanel);
button1 = new JButton("Create a project");
subPanel.add(button1,BorderLayout.SOUTH);
}
To get the result you probably intended, you need to tweak your code just a little.
Your lines
panel1 = new JPanel(layout1);
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
should be altered to read
panel1 = new JPanel(new GridLayout(2, 1));
leftPanel = new JPanel(layout3);
subPanel = new JPanel(layout2);
GridLayout takes rows first, then columns. Your layout1 defines one row and two columns but you use it for the text fields and the button, so I assume the two panels need to be above each other.
Second, you need to change the order from
add(panel1);
panel1.add(subPanel);
panel1.add(leftPanel);
to
add(panel1);
panel1.add(leftPanel);
panel1.add(subPanel);
Finally, I think you need to change
add(leftPanel);
button1 = new JButton("Create a project");
to
add(spendingPanel);
button1 = new JButton("Create a project");
Adding leftPanelagain seems to destroy the layout. If you do not want to see your spendingPanel here, you could use new JPanel() instead.
Here is a screenshot of the running program

(Java) There are white borders around all my buttons

In java, My buttons are surrounded by white, thick borders that only appear when I have a image in a jlabel for the background.
Example:
The problem here is that white borders go around all of my buttons, making it look terrible. The intended use was for the buttons to just go over the image, like this image:
Here is my code:
public class Gui extends JFrame {
private JTextField TextField;
private JButton Hi, Bye, Exit;
private JPanel Panel, Panel1, Panel2;
private JLabel label;
public Gui() {
super("My Program");
TextField = new JTextField("");
TextField.setEditable(false);
Hi = new JButton("Hi");
Bye = new JButton("Bye");
Exit = new JButton("Exit");
Actions a = new Actions();
Hi.addActionListener(a);
Bye.addActionListener(a);
Exit.addActionListener(a);
Dimension tfd = new Dimension(780, 25);
Dimension bd = new Dimension (75, 25);
Dimension lpd = new Dimension (800, 600);
TextField.setPreferredSize(tfd);
Hi.setPreferredSize(bd);
Bye.setPreferredSize(bd);
Exit.setPreferredSize(tfd);
ImageIcon image = new ImageIcon("C:/Users/Dakota/Desktop/Coding/Coding/img/Background.png");
label = new JLabel(image);
Panel = new JPanel();
this.setContentPane(label);
this.add(Panel);
this.setLayout(new FlowLayout());
Panel.add(TextField);
Panel1 = new JPanel();
this.add(Panel1);
Panel1.add(Hi);
Panel1.add(Bye);
Panel2 = new JPanel();
this.add(Panel2);
Panel2.add(Exit);
}
Your problem appears that you're adding JPanels to your GUI and not changing the opaque property. This property is by default true, meaning that the JPanel will paint a background that visually covers up anything below it, but if you set it to false by calling myPanel.setOpaque(false);, then any components below the JPanel will show through. This is true for most any Swing component, although is not quite straight-forward for component components such as JScrollPanes, and JTextComponents.

My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.
{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}
Here is my code
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
setTitle("Falling Distance Calculator");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 5));
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setVisible(true);
}
public static void main(String[] args)
{
new TurnerRandyFallingGUI();
}
}
You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent)method call in your code above.
Consider adding all of your JPanels to a single JPanel
Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel).
Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.
Before setVisible (true) statement add following statements:
add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);
There is nothing in your JFrame. That is the reason
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setLayout(new GridLayout(5, 1));
this.add(introP);
this.add(timeP);
this.add(gravityP);
this.add(answerP);
this.add(buttonsP);
setTitle("Falling Distance Calculator");
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.validate();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TurnerRandyFallingGUI();
}
});
}
}
Consider the following
In GridLayout, the first parameter is Rows, Second is columns
Never set the size of JFrame manually. Use pack() method to decide
the size
Use SwingUtilities.InvokeLater() to run the GUI in another thread.

Categories