I installed a plugin for eclipse that helps creating a JFrame in a UI.
The code generated form the plugin has some strange syntax.
I never ever saw something like this in java:
private JPanel b_,cb_,pb_,l_,tf_,ta_contentPane;
public Mainf() {
b_,cb_,pb_,l_,tf_,ta_contentPane = new JPanel();
b_,cb_,pb_,l_,tf_,ta_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
b_,cb_,pb_,l_,tf_,ta_contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(b_,cb_,pb_,l_,tf_,ta_contentPane);
}
how is that even possible? ^^
The standard java compiler sees that as a syntax error.
Is there a option to compile this right?
Edit:
I found it again (^^). All of these tools use this syntax.
URL for Eclipse: Help->Install new Software...
WindowBuilder Pro Eclipse Update Site - http://download.eclipse.org/windowbuilder/WB/integration/4.3/
and the Website:
http://www.eclipse.org/windowbuilder/
No, there is no option to compile that right.
private JPanel b_,cb_,pb_,l_,tf_,ta_contentPane;
Is a correct sentence.
b_,cb_,pb_,l_,tf_,ta_contentPane = new JPanel();
b_,cb_,pb_,l_,tf_,ta_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
b_,cb_,pb_,l_,tf_,ta_contentPane.setLayout(new BorderLayout(0, 0));
Are incorrect sencences.
The code is correct but the variable name generated is a not a valid identifier. The solution is to rename the variable and the code will compile fine.
private JPanel contentPane;
public Mainf() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
Related
I'm trying practice my GUI and I am having troubles putting gap between component and the frame.
The picture above is what I have so far. But I really want to put a gap between the left side of the frame and "label1".
private void initialize() {
frame = new JFrame();
frame.setTitle("WINDOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 5));
l1 = new JLabel("Label1");
l2 = new JLabel("Label2");
l3 = new JLabel("Label3");
l4 = new JLabel("Label4");
l5 = new JLabel("Label5");
bottomPanel.add(l1);
bottomPanel.add(l2);
bottomPanel.add(l3);
bottomPanel.add(l4);
bottomPanel.add(l5);
panel.add(bottomPanel, BorderLayout.SOUTH);
frame.add(panel);
}
Above is part my code. I tried doing:
bottomPanel.setLayout(new GridLayout(1, 5, -20, 0));
to put some horizontal gap but that only added gap between the components. That didn't move "label1" away from the frame at all. Is there any other way of doing this? I am very new to Java so I don't really know much of the other tricks. I would appreciate any help! Thank you!
The other answers are fudges that won't achieve the desired effect when the GUI is resized. Instead use:
JLabel.setHorizontalAlignment(SwingConstants.CENTER);
By centering the text within the JLabel, combined with GridLayout stretching the components to the full width of the cell, each label will have as much space either side as the GUI can allow. E.G. here is the effect when the GUI is at minimum size.
And when stretched wider:
(The red border is added to show the bounds of each label.)
Add a Border to the panel:
bottomPanel = new JPanel();
bottomPanel.setBorder( new EmptyBorder(0, 10, 0, 0) );
Read the section from the Swing tutorial on How to Use Borders for more information about the different borders you can create.
Try the following:
bottomPanel.add(javax.swing.Box.createHorizontalStrut(10));
Reading through SO questions. Sadly no solution worked for me.
My problem is, I have a JPanel:
contentPane = new JPanel();
contentPane.setName("contentPane");
contentPane.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100,305, 403);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
This one has fixed elements. When I enter another project from the "projekt" combobox I might have the chance (dependent on the project) that I get added elements. Input fields or comboboxes. These elements are in a different JPanel which lies ontop of the other JPanel.
this.zusatzPanel = new JPanel();
zusatzPanel.setName("zusatzPanel");
this.zusatzPanel.setLayout(null);
this.zusatzPanel.setBounds(11, 301, 270, 713);
When this is the case I need more space (in this case I have enough space but there could be more elements added), but I found no clue how to make the JPanel resize. I believe that I need to make both resizeable.
with too many added elements:
I also tried with contentPane.setLayout(new BorderLayout()); but that did not do anything.
EDIT: tried jframe.pack(); changed the Jpanel to make is smaller to test it out. But did not work. Or did i do something wrong?
this.pack();
contentPane = new JPanel();
contentPane.setName("contentPane");
contentPane.setLayout(new CardLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100,305, 203);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
Im trying to use JLabel to insert a image into my GUI. However it does not appear
Here is a partial of my code
public FirstAid() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 507);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
panel = new JPanel();
ImageIcon imageIcon = new ImageIcon("src/method_1.png");
JLabel label = new JLabel(imageIcon);
panel.add(label);
tabbedPane.addTab("name", null, panel, null);
You create a JPanel called panel, add a JLabel to it, but do nothing with the panel variable after this. You must add it to your GUI for the JLabel and the image it might hold to be seen. I'm guessing that you want to add panel to your JTabbedPane, but without more information, I can only guess.
Edit: your edited question now shows that you're adding the JPanel to the JTabbedPane. If you're still not seeing the image, then the problem is likely in your reading in of the image. Myself, I use ImageIO.read(...) and try to read the image in as an InputStream or as a URL. Key to all of these methods is to make sure that you have the image path correct, and don't make any assumptions about how you might think that it is correct. The only way to know for sure is to test it.
IntelliJ Idea GUI designer doesn't provide source code of generated JForm. I tried with File>Settings>GUI Designer> Java source Code, but there's no effect. What I get is something like this, and it looks different than on a preview.
private void $$$setupUI$$$() {
panel1 = new JPanel();
panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
tabbedPane1 = new JTabbedPane();
panel1.add(tabbedPane1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 200), null, 0, false));
final JPanel panel2 = new JPanel();
panel2.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
tabbedPane1.addTab("Log In", panel2);
final JPanel panel3 = new JPanel();
Also there is error warnings "cannot resolve symbol 'intellij'"
How to fix it?
The source generated by IDEA (as will be stated by a lot of the comments also output) isn't something that is meant to be edited.
It's unclear what was meant in your question regarding what looks different: Did you mean from how you think it should look given what the preview looks like? Or that the resultant gui that the code produces does look different ... if the later - please provide all source code to enable us to take a deeper look.
The warnings are because the classes (such GridLayoutManager) are not visible to the IDE/compiler until runtime. However, if the program is run from IDEA - it should execute just fine since the forms_rt.jar (that contains this and other useful classes) will be present in the classpath.
Please see http://www.jetbrains.com/idea/webhelp/gui-designer.html for more details.
I am making KenKen as my term project using java swing library. For alignment I have used gridbag and gridlayout, But now i want to add one more component of JPanel to the UI. These screenshots will make the problem more clear:
Now I select the grid cell to which i want to add respective candidates of in the left most panel.
It disturbs the adjacent alignments of the grid and panels.
Here are the panels with their respective layouts:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanel.setPreferredSize(new Dimension(20,40));
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
buttonPanel.add(eraseButton);
buttonPanel.add(hintButton);
JPanel cellPanel = new JPanel();
cellPanel.setName("cellPanel");
cellPanel.setLayout(new GridLayout(pSize, pSize, 0, 0));
JPanel numPanel = new JPanel();
numPanel.setName("numPanel");
numPanel.setLayout(new GridLayout(1,1,5,5));
numPanel.setPreferredSize((new Dimension(50,60)));
JPanel candPanel = new JPanel();
candPanel.setName("candidatesPanel");
JLabel candidates = new JLabel("Candidates");
candidates.setFont(new Font("Courier New", Font.ITALIC, 14));
candidates.setForeground(Color.GRAY);
candPanel.setLayout(new GridLayout(0,1));
candPanel.add(candidates);
Then it all goes into the content panel:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(candPanel, pos.nextRow());
content.add(new Gap(GAP) , pos.nextRow()); // Add a gap below
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
The buttons are all generated on runtime, and they are added to the candPanel in an action listener.
You appear to be using a GridBagConstraints subclass of which I am unaware (variable pos), though I can guess its function from context.
Assuming your problem is that you want the candidates panel to be to the left of the cellPanel, and not above it, you need to swap the lines which add the candPanel and the new Gap(GAP) as follows:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(new Gap(GAP), pos.nextRow()); // These two lines
content.add(candPanel, pos.nextRow()); // swapped over
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());