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.
Related
I'm trying to build a GUI in Java Swing. I have alot of trouble sizing components. I managed to size a JPanel by setting the dimensions with setPreferredSize(), but I can't get the components within that JPanel to size properly.
This is my screen with only the JPanel visible.
When I add the button to my JPanel the following happens:
As you can see the button is taking up the whole lenght and width of my JPanel/JFrame. Why is this happening? How can I fix it?
Here is my code:
Application.java
public void start() {
ControllerObserveer observeer = new ControllerObserveer();
frame = new JFrame("-");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.add(observeer.getView(), BorderLayout.CENTER);
frame.setResizable(false);
}
ControllerObserveer.java
public class ControllerObserveer {
private ModelObserveer model;
private ViewObserveer view;
public JPanel getView(){
return this.view.p;
}
}
ViewObserveer.java
public class ViewObserveer {
public JPanel p;
public ViewObserveer(){
this.p = new JPanel(new BorderLayout(), false);
p.setPreferredSize(new Dimension(500, 500));
p.setBackground(Color.red);
JButton b = new JButton("Hello World!");
b.setPreferredSize(new Dimension(40, 40));
p.add(b);
}
}
At last I would like to ask what the diffrences are between the diffrent layouts, like BorderLayout() or BoxLayout() for example.
Thank you for your time!
Install Java WindowBuilder on Eclipse. So, you can get things done.. "but I can't get the components within that JPanel to size properly."
I'm trying to get a JTextArea with a "save" JButton centered underneath it, maybe with a small bit of padding between the components as well as the components to the frame if possible. I've tried messing around with layout managers, panels, etc. and can't seem to get the result i want. Just looking for the simplest way to do this. Thanks.
Suggestions:
The overall layout of the GUI container could be BorderLayout.
Add the JScrollPane that holds your JTextArea BorderLayout.CENTER.
Create a JPanel just to hold the JButton and don't give it a specific layout manager. It will now use JPanel's default FlowLayout and will center components in the horizontal direction.
Add your JButton to this last JPanel.
Add that same JPanel to the GUI in the BorderLayout.PAGE_END (bottom) position.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class SimpleLayout extends JPanel {
private static final int ROWS = 20;
private static final int COLS = 60;
private JTextArea textArea = new JTextArea(ROWS, COLS);
private JButton button = new JButton("Button");
public SimpleLayout() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
setLayout(new BorderLayout());
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
SimpleLayout mainPanel = new SimpleLayout();
JFrame frame = new JFrame("SimpleLayout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
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
How can I switch Panels with ScrollPanes in a Frame? I've tried many possible ways but cannot come up with a solution to.
Actually this is one of the Java Problems my professor gave me and I needed to accomplish this by not using other layouts (such as CardLayout) and I should use the null layout only. Additional classes are allowed as long as I maintain these three classes and the scroll pane.
public class MainDriver {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
panel1 p1 = new panel1();
panel2 p2 = new panel2();
JScrollPane jsp = new JScrollPane(panel1.panel);
Container c = frame.getContentPane();
jsp.getVerticalScrollBar().setUnitIncrement(10);
c.add(jsp);
//codes for panel switching from panel1 to panel2 vice versa
frame.setDefaultCloseOperation(JFrame.exit_ON_CLOSE);
frame.setSize(1058, 600);
frame.setLocation(100, 50);
frame.setVisible(true);
}
}
---------------------------------------------
public class panel1{
public JPanel panel(){
JPanel fore = new JPanel();
fore.setLayout(null);
fore.setPreferredSize(new Dimension (1024, 600));
fore.setBackground(Color.decode("#004050"));
fore.setVisible(true);
JButton but = new JButton();
but.setLocation(425, 300);
but.setSize(100, 35);
//button action/mouse listener
fore.add(but);
return fore;
}
}
---------------------------------------------
public class panel2{
public JPanel panel(){
JPanel fore = new JPanel();
fore.setLayout(null);
fore.setPreferredSize(new Dimension (1024, 600));
fore.setBackground(Color.decode("#004050"));
fore.setVisible(true);
JButton but = new JButton();
but.setLocation(425, 300);
but.setSize(100, 35);
//button action/mouse listener
fore.add(but);
return fore;
}
}
How can I switch Panels with ScrollPanes in a Frame?
scrollPane.setViewportView( anotherPanel );
I have this code to create a simple gui (by hand) and I am trying to display gui components on the frame. However, when I run the program, only the frame shows without showing the components, such as the JTable.
Any idea why ?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
frame.setVisible(true);
Container contentPane = frame.getContentPane();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new FlowLayout());
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
contentPane.add(listPanel);
}
}
You should set a preferredSize() on the JTables and do a pack() afterwards.
Edit:
Moved setVisible(true) after pack(). This is the order which is used by Sun/Oracle.
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
Container contentPane = frame.getContentPane();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new FlowLayout());
Dimension d = new Dimension(100, 100);
JTable chOneTable = new JTable();
chOneTable.setPreferredSize(d);
JTable chTwoTable = new JTable();
chTwoTable.setPreferredSize(d);
JTable listTable = new JTable();
listTable.setPreferredSize(d);
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
contentPane.add(listPanel);
frame.pack();
frame.setVisible(true);
}
}
Construct the JFrame instance
Add the components to the JFrame instance
Realize the JFrame instance (i.e. setVisible(true))
The reason none of the components show up when the JFrame instance is shown is because you add components to it after it has been realized. If you want to components to show up, either follow the steps above, or at the end of the buildGui method, revalidate/repaint the container.