I get a compiler error while trying to access addMenu.setLayout().
Syntax error on token "setLayout", = expected after this token
Here is my code:
JPanel pnl1 = new JPanel(new GridLayout(8,0));
JPanel homeMenu = new JPanel(new GridLayout(3,0));
JPanel addMenu = new JPanel();
addMenu.setLayout(new BoxLayout(addMenu, BoxLayout.Y_AXIS)); // Here is the red underline
Are you using Eclipse? Sometimes it is buggy.
Just do File >> Save All.
Related
I'm trying to develop a little application for my Java class. I'm using jsoup to get information from an URL.
I finally got everything, but I don't know how to remove this huge blank between the images and the text. Any advice?
JFrame jf4 = new JFrame("¡¡NEWS WITH PICTURE!!");
JPanel p3 = new JPanel(new BorderLayout());
p3.setBorder(new EmptyBorder(5, 5, 0, 0));
p3.setLayout(new GridLayout(90, 2, 5, 5));
for (Element link: pictures) {
Element picture = link.select("source[media=(max-width: 48em)]").first();
Element text = link.select("img").first();
//System.out.println(picture);
//System.out.println(picture.attr("data-original-set"));
try {
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);
JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);
} catch (Exception exp) {
exp.printStackTrace();
System.out.println(exp);
}
} // IN CASE OF ERROR OF THE URL IT PRINTS java.net.MalformedURLException: no protocol: LINK TRIED
JScrollPane panelPane2 = new JScrollPane(p3);
jf4.getContentPane().add(panelPane2);
jf4.pack();
jf4.setVisible(true);
jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Finally thanks to #prasad_ I get the solution.
I follow his advice. Instead of creating a new JLabel I use the propiertie setText on the same label.
Instead of this:
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
p3.add(label3);
JLabel label4 = new JLabel(text.attr("alt"));
p3.add(label4);
I do this:
JLabel label3 = new JLabel();
label3.setIcon(new ImageIcon(new ImageIcon(new URL(picture.attr("data-original-set"))).getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT)));
label3.setText(text.attr("alt"));
p3.add(label3);
So finally, the blank disappear.
This question already has an answer here:
JAVA positioning labels on JFRAME
(1 answer)
Closed 5 years ago.
I'm trying to get an output like this (designed with Netbeans designer), where I need to actually design it by code:
Where the layout of the JFrame should be like this:
JFrame frame = new JFrame("Horizontal Histogram");
frame.setVisible(true);
frame.setSize(400, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 1));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(panel);
JPanel panel_2 = new JPanel();
panel_2.setLayout(new GridLayout(5, 1));
panel_2.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.add(panel_2);
JLabel label_1 = new JLabel("0-29");
JLabel label_2 = new JLabel("30-39");
JLabel label_3 = new JLabel("40-69");
JLabel label_4 = new JLabel("70-100");
JLabel stats_1 = new JLabel(); //number of stars
JLabel stats_2 = new JLabel();
JLabel stats_3 = new JLabel();
JLabel stats_4 = new JLabel();
stats_1.setText(stars); //starts is a string like ("***")
stats_2.setText(stars);
stats_3.setText(stars);
stats_4.setText(stars);
panel.add(label_1);
panel.add(label_2);
panel.add(label_3);
panel.add(label_4);
My code below only shows the stars, in one entire column. If I remove the second panel and add the 'stats labels' to the first panel it shows a 2 x 4 grid layout like this:
Any ideas on how to get an output like the first image I've posted?
JFrame uses by default BorderLayout.
This: frame.add(panel); adds panel to BorderLayout.CENTER
This: frame.add(panel_2); adds panel_2 to BorderLayout.CENTER
The problem is that BorderLayout.CENTER can hold one component only.
Use:
frame.add(panel, BorderLayout.WEST); and frame.add(panel_2, BorderLayout.EAST);
To get better insight of layouts read A Visual Guide to Layout Managers.
I am using the above layout. All i want is split the second row into two equal and half parts, where I can have a jlabel "enter:" on the left and a jtextfield on the right. How can I accomplish this? I use:
GridLayout gl = new GridLayout(2,1);
setLayout(gl);
JButton jb = new JButton("Click Me!");
jb.setFocusPainted(false);
add(jb);
JLabel jl = new JLabel("Enter:");
JTextField jt = new JTextField();
add(jl);
The simplest solution is to put another JPanel in the bottom row, which is configured to use GridLayout.
Code (tested):
this.setLayout(new GridLayout(2,1));
JButton button = new JButton("Click Me!");
JPanel bottomPanel = new JPanel(new GridLayout(1,2));
JLabel label = new JLabel("Enter:");
JTextField textField = new JTextField();
bottomPanel.add(label);
bottomPanel.add(textField);
add(button);
add(bottomPanel);
can we create 2 content panels in north widget.
BorderLayoutContainer con = new BorderLayoutContainer();
ContentPanel cp = new ContentPanel();
VerticalLayoutContainer logoLayout = new VerticalLayoutContainer();
BorderLayoutData d = new BorderLayoutData(.20);
d.setMargins(new Margins());
Image logo = new Image("/IMAGES/Logo.png");
logoLayout.add(logo);
cp.add(logoLayout);
cp.setHeaderVisible(false);
con.setNorthWidget(cp, d);
please suggest me how to create two content panels.
Basically what I need to do is - please look into the image and let me know what I can do for that
You create your two panels within a single panel, and then assign that single panel to NORTH. Remember that your overall layout can be created from nested layouts.
ContentPanel cp = new ContentPanel();
JPanel panelA = new JPanel();
JPanel panelB = new JPanel();
JPanel panelBig = new JPanel();
panelBig.add(panelA);
panelBig.add(panelB);
cp.add(panelBig, BorderLayout.NORTH);
I think you can probably work out the rest of the details on your own.
the following codes created a box layout conviniently but the problem i have is the textfields occupy the entire rows. which is supposed to asume the parameter length in which it was specified.
public void makeControlpanel(){
JPanel controlpanel = new JPanel();
//SET PANEL LAYOUT MANAGERS
controlpanel.setLayout(new BoxLayout(controlpanel,BoxLayout.PAGE_AXIS));
controlpanel.setBorder(BorderFactory.createTitledBorder("Create Control file"));
filenameC = new JLabel("Filename");
filenameBad = new JLabel("Bad Filename");
filenameDis = new JLabel("Discard Filename");
// fields
fileField = new JTextField(1);
badfileField = new JTextField(7);
discardfileField = new JTextField(7);
The layout manager decides the size of the components. You have options to define the bounds of a component to the layout manager using
comp.setMinimumSize(new Dimension(w, h));
comp.setPreferredSize(new Dimension(w, h));
comp.setMaximumSize(new Dimension(w, h));
When you give setPreferredSize layout manager will try to give that size. GridBagLayout is the msot flexible layout and you can prettymuch achieve any layout you need.
The parameter length by defenition only defines the character you can put in the textfield.