Tried to put on frame some swing components.
This code worked to days ago. Now it's not work, didn't nothing.
Maybe somebody can tell me what it's wrong?
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setSize(500, 400); //Size of frame
mainFrame.setTitle("Cinema City"); //Set title
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel mainLabel = new JLabel("Welcome to Cinema City catalog!");
JLabel actorLabel = new JLabel("Actors: ");
JLabel laLabel = new JLabel("Last added: ");
JLabel searchLabel = new JLabel("How to search ?");
GridBagConstraints gbc = new GridBagConstraints();
mainFrame.add(mainLabel, new GridBagConstraints(4, 0, 1, 3, 1, 1,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(20, 160, 0, 0), 0, 0));
mainFrame.add(actorLabel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(100, 0, 0, 0), 0, 0));
mainFrame.setVisible(true);
This is the error:
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at javax.swing.JRootPane$1.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at GUI.main(GUI.java:40)
You're not actually setting your layout to GridBagLayout, so it is still the default (which would be a FlowLayout).
Of course, only a GridBagLayout can actually handle GridBagConstraints.
This can be fixed by changing your declaration to JFrame mainFrame = new JFrame(new GridBagLayout());
Layout is not mentioned for the particular JFrame - mainframe
Add this line after the JFrame declaration
mainFrame.setLayout(new GridBagLayout());
Should work fine.
you are not set frame layout .
after creation object of frame write this code.
new GridBagLayout();
mainFrame.setLayout(gbl);
its work
Related
I have a JTree inside a JScrollPane which is inside a JPanel.
The problem I got is the width which is not fixed when I fill the JTree with nodes, or with a node with a long name.
Here an example:
As you can see, the left one is longer then the right one.
My goal is to keep them exactly equal in size, splitting the main window at 50% each.
Here the code used to generate the window.
Is there a way to keep the width size of the JScrollPane fixed?
Thanks.
public void initialize() {
this.frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Services");
mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
menuBar.add(mnNewMenu);
frame.getContentPane().setLayout(new MigLayout("", "[grow][grow]", "[grow][grow]"));
JPanel left_JPanel = new JPanel();
frame.getContentPane().add(left_JPanel, "cell 0 0,grow");
left_JPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));
left_ScrollPane = new JScrollPane();
left_JPanel.add(left_ScrollPane, "cell 0 0,grow");
JLabel left_Label = new JLabel("Left Scroll Pane");
left_Label.setFont(new Font("Tahoma", Font.BOLD, 12));
left_Label.setForeground(Color.BLUE);
left_Label.setHorizontalAlignment(SwingConstants.CENTER);
left_ScrollPane.setColumnHeaderView(left_Label);
JTree left_tree = new JTree();
left_ScrollPane.setViewportView(left_tree);
JPanel right_JPanel = new JPanel();
frame.getContentPane().add(right_JPanel, "cell 1 0,grow");
right_JPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));
JScrollPane right_ScrollPane = new JScrollPane();
right_JPanel.add(right_ScrollPane, "cell 0 0,grow");
right_JTree = new JTree(phModel);
right_JTree.setVisibleRowCount(8);
right_ScrollPane.setViewportView(right_JTree);
JLabel right_Label = new JLabel("Right Scroll Pane");
right_Label.setFont(new Font("Tahoma", Font.BOLD, 12));
right_Label.setForeground(Color.BLUE);
right_Label.setHorizontalAlignment(SwingConstants.CENTER);
right_ScrollPane.setColumnHeaderView(right_Label);
}
My goal is to keep them exactly equal in size, splitting the main window at 50% each.
Use nested panels with standard layout manager from the JDK.
The GridLayout makes components the same size.
Something like:
JPanel left = new JPanel( new BorderLayout() );
left.add(leftLabel, BorderLayout.PAGE_START);
left.add(listScrollPane, BorderLayout.CENTER);
JPanel right = ...
JPanel main = new JPanel( new GridLayout(0, 2) );
main.add( left );
main.add( right );
frame.add( main );
I want to make a Java-Code, where I can insert as many Panels as I want. So that I can scroll down to see the Panels. I'm so far right now:
But my problem is, I can't scroll down. I tested the JScrollPane with JTextAreas which worked just fine.
Picture of my Program
package test;
import java.awt.*;
import javax.swing.*;
public class Scrollbar {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JTextField tFId = new JTextField("ID: ", 5);
JTextField tFName = new JTextField("Name: ", 5);
JTextField tFHersteller = new JTextField("Hersteller: ", 5);
JTextField tFId2 = new JTextField("ID: ", 5);
JTextField tFName2 = new JTextField("Name: ", 5);
JTextField tFHersteller2 = new JTextField("Hersteller: ", 5);
JTextField tFId3 = new JTextField("ID: ", 5);
JTextField tFName3 = new JTextField("Name: ", 5);
JTextField tFHersteller3 = new JTextField("Hersteller: ", 5);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(200, 85));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
JScrollPane scrollPanel = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel1.setLayout(new FlowLayout());
panel1.add(tFId);
panel1.add(tFName);
panel1.add(tFHersteller);
panel2.setLayout(new FlowLayout());
panel2.add(tFId2);
panel2.add(tFName2);
panel2.add(tFHersteller2);
panel3.setLayout(new FlowLayout());
panel3.add(tFId3);
panel3.add(tFName3);
panel3.add(tFHersteller3);
frame.add(scrollPanel);
frame.setVisible(true);
}
}
You are over-using FlowLayout.
Different layouts have diferent behaviors. First, you need to remove this line:
frame.setLayout(new FlowLayout());
The default layout for a frame’s content pane is a BorderLayout. You want to leave it that way, because a component added to a BorderLayout with no constraints will be placed in the center, where it will stretch to fill the entire space.
Second, you want to remove these:
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(200, 85));
Setting the preferred size interferes with the JScrollPane’s ability to manage its view (that is, panel). When you want to have your components appear on multiple rows, you should try to force FlowLayout to do it by constraining its width; rather, use a layout that is designed to place components on different rows. The best choice is GridBagLayout:
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(panel1, gbc);
panel.add(panel2, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
panel.add(panel3, gbc);
The use of GridBagConstraints.REMAINDER in a constraint will tell the GridBagLayout to make a child component take up an entire row.
The use of weighty = 1 tells the GridBagLayout that the grid cell of the child about to be added should take up all extra vertical space, when the panel is larger than its children. Finally, GridBagConstraints.NORTH keeps that child placed at the top of that stretched grid cell, no matter how high the grid cell is.
I use a Gridlayout to place 4 elements in one Line. First I had a JPanel and everything worked fine. For the case that the number of lines get to big and I have to be able to scroll down, I changed it a bit. Now I have my JPanel with one JScrollPane added on it. I used the same code, now I just add the elements to the viewport of the Jscrollpane, but now I get this exception Get java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout: at javax.swing.JScrollPane.setLayout(Unknown Source) and I dont know exactly why. Why shouldnt be Gridlayout's be unknown for Jscrollpane?
Here is the code:
public objectDetails() {
setTitle("LLI_MC_Solver");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setLayout(new GridLayout());
setBounds(100, 100, 510, 401);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setVisible(true);
contentPane.setPreferredSize(new Dimension(500, 390));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0), 2));
scrollPane.setBounds(10, 10, 474, 342);
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
scrollPane.setPreferredSize(new Dimension(465, 330));
contentPane.add(scrollPane);
JPanel view = (JPanel)scrollPane.getViewport().getView();
for(Values v : colDetails)
{
JLabel lblC = new JLabel();
lblC.setText(k);
view.add(lblC);
view.validate();
JLabel lblN = new JLabel();
lblN.setText(v.getName());
view.add(lblN);
view.validate();
JLabel lblT = new JLabel();
lblT.setText(v.getType());
view.add(lblT);
view.validate();
JTextField valueBox = new JTextField();
valueBox.setText(v.getValue());
view.add(valueBox);
view.validate();
}
}
I marked the line which causes the Problem according to the compiler. I dont understand why, with the JPanel the same code worked fine. The for-loop where the elements are added I posted for completion purposes, the issue must be somewhere in the setLayout()-Method.
Thanks in advance, appreciate every help.
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
You can't change the layout manager of a scrollpane.
A JScrollPane has its own custom layout manager because it needs to manage the horizontal/vertical scrollbars as well as the row/column headers etc..
Instead you add a panel that uses a GridLayout:
JPanel panel = new JPanel( new GridLayout(0, 4) );
panel.add( component1 );
panel.add( component2 );
panel.add( component3 );
panel.add( component4 );
JScrollPane = new JScrollPane( panel );
With this code I will have the following window. I created 2 panels and added the mainp one to the frame and the panel to the mainp I did this in order to make window resizing dynamic (so the panel wont resize to the frame) I tried making my default panel size wider so that the text fields and label become wider but panel.setsize doesn't seem to do anything.
// creates the labels
studId = new JLabel("Student ID");
studAvg = new JLabel("Student Average");
studName = new JLabel("Student Name");
// creates the text fields
JTextField studIdText = new JTextField();
JTextField studAvgText = new JTextField();
JTextField studNameText = new JTextField();
JPanel mainp = new JPanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 2, 2));
panel.setSize(300, 100);
// adds to the GridLayout
panel.add(studId);
panel.add(studIdText);
panel.add(studName);
panel.add(studNameText);
panel.add(studAvg);
panel.add(studAvgText);
mainp.add(panel);
add(BorderLayout.CENTER,mainp);
// verifies the textfields
studIdText.setInputVerifier(new IntVerifier());
studAvgText.setInputVerifier(new DoubleVerifier());
setTitle("Student Form");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
The method you are looking for is setPreferredSize. Use it instead of panel.setSize(300, 100).
panel.setPreferredSize(new Dimension(300, 100));
I would also recommend to not setting the size of your JFrame to the fixed value (300,200) but do pack() instead. This will set the size of your JFrame to fit the panels inside.
using Advise from #Dan and #MADprogrammer and #trashgod i came up with the following
JTextField studIdText = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints r1 = new GridBagConstraints();
r1.fill = GridBagConstraints.HORIZONTAL;
r1.weightx = 0.0;
r1.gridx = 0;
r1.gridy = 0;
panel.add(studId,r1);
r1.weightx = 0.5;
r1.gridx = 1;
r1.gridwidth = GridBagConstraints.REMAINDER;
panel.add(studIdText,r1);
of course you can make GridBagConstraints for every row and just change the gridy
Set the layout for mainp as BorderLayout.
mainp.setLayout(new BorderLayout());
Then, in order to avoid having the textfields resize vertically and look strange, you can add panel to BorderLayout.NORTH, for instance.
mainp.add(panel, BorderLayout.NORTH);
I have this code in which I am trying to fit a scrollable Panel (JPanel) but I don't get it. Here is my code:
public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
init();
getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
getContentPane().add(initConnectors(),
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
getContentPane().add(props,
new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
setSize(800, 600);
setLocationRelativeTo(null);
}
Thanks in advance.
I edit to add a code that partially seems to work...
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane);
scrPane.setLayout(new ScrollPaneLayout());
init();
add(initConnectors());
setSize(800, 600);
setLocationRelativeTo(null);
}
But it isn't still scrollable, at least it makes its function inside a JScrollPane, is a good step.
Make a JPanel scrollable and use it as a container, something like this:
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane); // similar to getContentPane().add(scrPane);
// Now, you can add whatever you want to the container
To extend #Eng.Fouad answer:
public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
container.setLayout(new GridBagLayout());
init();
container.add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
container.add(initConnectors(),
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
container .add(props,
new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
setSize(800, 600);
setLocationRelativeTo(null);
}
}
Maybe this will help...
JFrame frame = new JFrame();
JPanel panel = new JPanel();
// add something to you panel...
// panel.add(...);
// add the panel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane(panel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Then, add the jScrollPane to your frame
frame.getContentPane().add(jScrollPane);
To make a component of a JFrame scrollable, wrap the component in a JScrollPane:
JScrollPane myJScrollPane = new JScrollPane(myJLabel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
and replace mentions of myJLabel with myJScrollPane. Worked for me.
Just chiming in, and feel free to correct me if I'm off base, but using a JScrollPane like that has an unintended consequence of requiring more frequent window resizing.
For example, I had a program where I set up a JFrame-wide scrollpane like that. I also had a JTextArea in a tab in the frame that was the same size as the content pane. This textArea was also in its own scrollpane (this was more of a screwing around project than anything else). When I loaded content from a file to deposit in the textArea, it triggered the scrollbars around the text area.
The result was that my, let's call it the innerScrollPane, was now larger than the JFrame because of the scrollbars, which hadn't been visible before. This then triggered what I'm now going to call the outerScrollPane, to display its scrollbars, which then covered up the inner scrollbars.
This was easily resolved by adding an extra window.pack() argument to the end of my file opening method, but I just wanted to throw this out there. The scrollbars can potentially cover up content in the window if you're not careful. But... well there are a million ways to prevent this problem, so it's not a huge deal. Just something to be aware of.
Try this:
JScrollPane sp = new JScrollPane();
this.add(sp).
sp.add( *GUI elements for your applications.*)
Something like that should work for you. Take a look at this as well: