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.
Related
I'm trying to add two tables into my frame, but I only get one. I tried to use different positions in BorderLayouts, but still don't get the final result. My code is below:
private JFrame f = new JFrame("List of cars");
// [SIZE]
f.setSize(700, 600);
// [TABLE]
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("GROUP 1");
table.setPreferredScrollableViewportSize(new Dimension(30, 20));
JScrollPane jScrollPane1 = new JScrollPane(table);
JPanel listPahel = new JPanel();
listPahel.setLayout(new BorderLayout());
listPahel.add(jScrollPane1, BorderLayout.CENTER);
listPahel.setBorder(BorderFactory.createEmptyBorder(50, 10, 400, 500));
listPahel.validate();
//-----------------
DefaultTableModel model2 = new DefaultTableModel();
JTable table2 = new JTable(model2);
model2.addColumn("GROUP 2");
table2.setPreferredScrollableViewportSize(new Dimension(30, 20));
JScrollPane jScrollPane2 = new JScrollPane(table2);
JPanel listPahel2 = new JPanel();
listPahel2.setLayout(new BorderLayout());
listPahel2.add(jScrollPane2, BorderLayout.SOUTH);
listPahel2.setBorder(BorderFactory.createEmptyBorder(200, 20, 20, 20));
listPahel2.validate();
f.add(listPahel);
f.add(listPahel2);
f.setVisible(true);
I always get the second table, but I need to get both.
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.
You didn't say how you wanted the JTables arranged on your page, so I put them side by side. Here's the example GUI I came up with.
All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
I separated the creation of the JFrame from the creation of the two JPanels that hold the JTables. The JFrame has a default BorderLayout. I defined each of the two JPanels to have a BorderLayout.
The two JPanels each have a JScrollPane placed in the CENTER of their BorderLayout.
The two JPanels are placed in the WEST and EAST of the JFrame BorderLayout.
Here's the complete runnable code.
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TwoJTablesGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TwoJTablesGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("Two JTables GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createJTable1Panel(), BorderLayout.WEST);
frame.add(createJTable2Panel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createJTable1Panel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
DefaultTableModel model = new DefaultTableModel();
model.addColumn("GROUP 1");
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createJTable2Panel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
DefaultTableModel model = new DefaultTableModel();
model.addColumn("GROUP 2");
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
}
I have problem to display two panel in Jframe. Please help me to fix the code below
public class quotingtable extends javax.swing.JFrame {
DefaultTableModel model;
JTable table;
JButton SetButton = new JButton("Set Symbol");
JButton VNStock = new JButton("VNStockChart");
JButton Global = new JButton("GlobalChart");
JPanel quotingpanel = new JPanel(new BorderLayout());
JPanel functionpanel = new JPanel(new BorderLayout());
public void run(){
model = new DefaultTableModel(col,row);
quotingpanel.add(table);
functionpanel.add(BorderLayout.CENTER,SetButton);
functionpanel.add(BorderLayout.WEST,VNStock);
functionpanel.add(BorderLayout.EAST,Global);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
quotingpanel.add(pane);
getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);
setSize(800,800);
setLayout( new FlowLayout());
setLayout ( new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Any help is appreciated.
Remove:
setLayout( new FlowLayout());
setLayout ( new BorderLayout());
Using BorderLayout this way won't pick up the pre-existing components, so will ignore them and won't lay them out
And consider replacing setSize(800,800); with pack();
You may also want to change
getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);
to
getContentPane().add(functionpanel, BorderLayout.CENTER);
getContentPane().add(quotingpanel, BorderLayout.SOUTH);
it's simply a more consistent and preferred mechanism
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
For some reason I can't get my scrollpane to be displayed within an applet.
public void init() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrPane.setLayout(new ScrollPaneLayout());
frame.getContentPane().add(scrPane);
this.setVisible(true);
}
You never display the JFrame that you create!
This:
frame.getContentPane().add(scrPane):
this.setVisible(true); // this != frame
is not working because you create a JFrame and then ignore it.
You shouldn't have an applet display a JFrame anyway. If you need to show a separate window, consider showing a JDialog. Better still, why not simply put the JScrollPane in the applet itself?
e.g.,
public void init() {
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
JScrollPane scrPane = new JScrollPane(panel);
scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// scrPane.setLayout(new ScrollPaneLayout());
// frame.getContentPane().add(scrPane);
getContentPane().add(scrPane);
// this.setVisible(true);
}
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.