Custom JPanel objects overlap when added to JFrame - java

I'm trying to create a custom timer that will record cumulative time passage separated by days. I have a custom JPanel that does all of the timer work for me. I would like to have a GUI interace with this JPanel represented 7 times. However, when I add more than one custom JPanel to either a JPanel or a JFrame, they don't show up. I've tried setting the layout and setting them to everything I can think of, but nothing works.
This is the basic setup of the Panel:
public class TimerPane extends JPanel{
private static JButton button = new JButton("Start");
private static JLabel label = new JLabel("Time elapsed:");
private static JLabel tLabel = new JLabel("0:0:0");
private static JLabel title = new JLabel("Timer");
public TimerPane(){
button.addActionListener(new ButtonListener());
this.add(title);
this.add(label);
this.add(tLabel);
this.add(button);
this.setOpaque(false);
this.setPreferredSize(new Dimension(100,100));
this.setMaximumSize(new Dimension(100,100));
}
}
This is my latest attempt at getting the JPanel to display multiple times (just twice here):
public static void main(String[] args){
JFrame frame = new JFrame("Timer");
JPanel panel = new JPanel();
frame.setPreferredSize(new Dimension(700,110));
panel.setLayout(new BorderLayout());
panel.add(new TimerPane(), BorderLayout.EAST);
panel.add(new TimerPane(), BorderLayout.WEST);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
The GUI that executes after this is 700x110 of which only 100x100 on the far left is used for exactly one of my TimerPane panels. I have also tried GridLayout on the same code, but then only the TimerPane in the second "spot" shows up. Any suggestions?

First of all, please remove the static from the member variables: button, label, tLabel and title. Otherwise, having them static, it means they are shared by all the TimerPane instances.
You will see 2 timer panels now.
Next, you can change the BorderLayout to a FlowLayout for instance and add several instances of TimerPane.
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(new TimerPane());
panel.add(new TimerPane());

Related

FlowLayout takes up too much vertical space, change height

I have JFrame that uses FlowLayout for buttons and BoxLayout for the JFrame and looks like this:
I need it to look like this:
For some reason the JPanel of the buttons (green) takes up too much space, while the labels on the red panel are all on the same row, instead of each on a different row.
My code is as follows:
import javax.swing.*;
import java.awt.*;
public class ButtonsTest extends JFrame {
private JButton button1 = new JButton("Button1");
private JButton button2 = new JButton("Button2");
private JButton button3 = new JButton("Button3");
private JButton button4 = new JButton("Button4");
private JPanel panel = new JPanel(new FlowLayout());
private JPanel otherPanel = new JPanel();
public ButtonsTest() {
setPreferredSize(new Dimension(200, 200));
setMinimumSize(new Dimension(200, 200));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.setBackground(Color.GREEN);
add(panel);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
otherPanel.add(new Label("1"));
otherPanel.add(new Label("2"));
otherPanel.add(new Label("3"));
otherPanel.setBackground(Color.RED);
add(otherPanel);
pack();
}
public static void main(String[] args) {
ButtonsTest test = new ButtonsTest();
}
}
What is my mistake?
For some reason the JPanel of the buttons (green) takes up too much space
When using a BoxLayout, the components will grow up to the maximum size when extra space is available. So extra space is allocated to both the red and green panels.
Don't set the layout of the content pane to use a BoxLayout.
while the labels on the red panel are all on the same row, instead of each on a different row.
By default a JPanel uses a Flow layout.
The solution is to use the default BorderLayout of the JFrame.
Then you add the green panel to the frame using:
add(panel, BorderLayout.PAGE_START);
Then for the "otherPanel" you can use the BoxLayout:
otherPanel.setLayout( new BoxLayout(otherPanel, BoxLayout.Y_AXIS) );
Then you add the "otherPanel" to the frame using:
add(otherPanel, BorderLayout.CENTER);
Also, components should be added to the frame BEFORE the frame is visible. So the setVisible(...) statement should be the last statement in the constructor.

Why does Java pack does not size frame properly when textarea is used with other components?

I am creating a GUI using GridLayout to arrange multiple panels that are using FlowLayout. I am using pack to determine the size of the frame. If I have only a TextArea then it sizes correctly. If I have only other components it sizes correctly. But if I use both p1 and p2 in the code below the panel with the buttons does not size properly. The test code I am using is below. If I use only p1 or p2 it sizes properly, but not with both: Here is the image I am getting. I don't want all that extra space between the buttons and the textarea.
If I do textarea alone it sizes properly.:
public class GUIPractice extends JFrame
{
//declare GUI objects
JPanel panel;
JPanel p1, p2;
JButton btnAdd, btnShow;
JScrollPane js;
JTextArea taOutput;
//constructor
public GUIPractice()
{
//give frame a title
setTitle("GUI Practice");
panel =new JPanel(new GridLayout(0, 1));
btnAdd = new JButton("Add");
btnShow = new JButton("Show");
taOutput = new JTextArea(10, 20);
js = new JScrollPane(taOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
p1 = new JPanel();
p1.add(btnAdd);
p1.add(btnShow);
p2 = new JPanel();
p2.add(js);
panel.add(p1);
panel.add(p2);
//add the panel to the frame
add(panel);
}
public static void main(String[] args)
{
GUIPractice frame = new GUIPractice();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
It sizes correctly. The GridLayout means that all cells will be the same size. Since the text area is bigger than the buttons, you see extra space in the panel containing the buttons.
I would suggest you just use the default BorderLayout of the frame.
There is no need for your "panel" using the GridLayout.
You just use:
add(p1, BorderLayout.PAGE_START);
add(p2, BorderLayout.CENTER);
Now the p1 and p2 will be the proper height.
Also, there really is no need for "p2". You can just add the scroll pane directly to the frame.

Move a JLabel inside of JPanel without losing hierarchy

So I'm moving a JLabel inside of JPanel and I want the JPanel to automatically show scrolls bars that lead you to the JLabel wich I moved out of it.
The problem is that when I set the new JLabel location and this location is out of the JPanel, it loses its hierarchy so the JLabel stops being part of the JPanel and as a result the panel doesn't scrolls and you can't see the JLabel. So this is my code if you can tell how can I do this. Thanks.
//Panel creation
JPanel panel = new JPanel();
panel.setSize(500, 500);
panel.setAutoscrolls(true);
//Label creation
JLabel label = new JLabel();
label.setText("Hi");
label.setSize(40, 40);
//Adding Jlabel to Panel
panel.add(label);
label.setLocation(800, 200);//this invalidates inherancy
What you need to do it encapsulate the JPanel inside a JScrollPane and increase its size with setPreferredSize()
Note that using setSize() setWidth() or setHeight() will be ignored by the scroll pane
A example code of how to set it up follows:
public class TestFrame extends JFrame {
public TestFrame() {
setLayout(null);
setSize(500, 500);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(panel);
add(scroll);
scroll.setBounds(100, 100, 200, 200);
panel.setPreferredSize(new Dimension(800, 800));
}
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
In this example, the JScrollPane is fixed with 200x200 size, while the JPanel inside it has a size of 800x800. You can change this value dynamically as you move your JLabel around.
Also, I'd like to mention that null layouts is not the correct approach. In this example, I wanted to minimize the influence of LayoutManagers and underlying calls to setBounds() by them to emphasize the point I'm trying to get across, which is a pure change of size inside a JScrollPane. The whole JFrame is just a "vessel" for the example.

how can i set the size of two JPanel in one JFrame?

how can i modify the size of the panel in the JFrame
am doing a calculator, the first panel will hold the JTextField which i suppose to be small
the second panel will hold the JButtons which suppose to be bigger
JFrame frame = new JFrame(new GridLayout(2, 1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPabel();
frame.add(panel1);
frame.add(panel2);
i've been trying to make panel1 smaller than panel2 yet nothing worked!
GridLayout would not be an appropriate choice in this scenario since it ignores the preferred sizes of the components inside the container and displays them all at an equal size instead.
I'd suggest using a BorderLayout. You can find a demonstration and description of that layout manager as well as a few others in Oracle's tutorial, A Visual Guide to Layout Managers.
Here's another example using BorderLayout which might be more relevant to your problem.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String []args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
panel2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
});
}
}
Edit: The JFrame's content pane uses a BorderLayout by default, hence the absence of a call to setLayout. Source

problem in nested Jpanel over Jframe

I have a JFrame and a Jpanel over that in which various buttons are placed.so on click of a button I have called a new class which is also having containers placed in a Jpanel.so I want to show that new class panel over the main Jframe panel.How can I do that?
And if we use card layout in it then how can i use that as on click button i have called an object of a new class.
as
Card layout consider each component in a container as card and i want whole Jpanel as a card so is it possible to do that???
Can We do nesting of Jpanels in it?
Please suggest me a right way to do that?
here is SSCCE:
// this is the main class on which i want to use panel of other class
public class mymain
{
JFrame jframe = new JFrame();
JPanel panel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
public mymain()
{
jframe.setLayout(borderlayout);
JMenuBar menubar = new JMenuBar();
jframe.setJMenuBar(menubar);
JButton home_button = new JButton("HOME");
menubar.add(home_button);
jframe.getContentPane().add(panel,BorderLayout.CENTER);
panel.setLayout(new GridBagLayout());
//here used containers over that frame
and call it from main()
}
here is another class to manage category is
public class manageCategory
{
JPanel panel = new JPanel();
GridBagLayout gridbglayout = new GridBagLayout();
GridBagConstraints gridbgconstraint = new GridBagConstraints();
public manageCategory()
{
panel.setLayout(new BorderLayout());
// i have again here used containers placed with grid bag layout
}
}
So now i want that as i click on home button used in mymain class then the panel that is used in manageCategory() should be displayed on the same panel.and when i again click on home button then the mymain panel get displayed.how can i do that???
I would advise you to use a CardLayout for this task.
Updated example with JPanel and "classes":
static class MainPanel extends JPanel {
public MainPanel(final Container frame) {
add(new JButton(new AbstractAction("Click to view next") {
#Override
public void actionPerformed(ActionEvent e) {
frame.add(new NextPanel(), "NextPanel");
((CardLayout) frame.getLayout()).show(frame, "NextPanel");
}
}));
}
}
static class NextPanel extends JPanel {
public NextPanel() {
add(new JLabel("Next page in the card layout"));
}
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
frame.setLayout(new CardLayout());
frame.add(new MainPanel(frame.getContentPane()), "MainPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
CardLayout is one of possible ways, but there are another options valid or required by most completed GUI
1) BorderLayout, because there only one JComponent can occupate decision area
someContainer.add(myPanel, BorderLayout.CENTER)
revalidate();
repaint();
2) GridBagLayout
before anything you have to get declared GridBagConstraints from myOldComponent layed by GridBagLayout
myContainer.setVisible(myOldComponent);
//or
myContainer.remove(myOldComponent);
myContainer.add(myNewComponent, gbc);
revalidate();
repaint();
You can
JFrame myFrame = new JFrame();
JPanel panel1 = new JPanel();
Panel1.setVisible(true);
myFrame.add(panel1);
JPanel panel2 = new JPanel();
Panel2.setVisible(false);
myFrame.add(panel2);
//Here you setup your panels and your actionlisteners etc and when
//you wish for your second panel to show up just run the code below.
panel1.setVisible(false);
panel2.setVisible(true);
Obviously you first have to add both panels to your Jframe. Panel1 will be at first visible, as it is the one shown by default. Panel2 must be set to be invisible in the beginning.

Categories