how to create 2 content panels in north widget - java

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.

Related

Positioning two panels on JFrame [duplicate]

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.

Swing GUI moving when user enters text

I am trying to do a fairly basic swing GUI where a user can enter a url, choose a local file location etc. I am using multiple layout managers including boxlayout, borderlayout and flowlayout. The code is below.
My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?
Setup menu bar
JButton menu_File = new JButton("File");
JButton menu_Edit = new JButton("Edit");
JToolBar toolBar = new JToolBar();
toolBar.add(menu_File);
toolBar.add(menu_Edit);
//Setup options area
JPanel options = new JPanel();
options.setBorder(BorderFactory.createTitledBorder("Options"));
BoxLayout layout_Options = new BoxLayout(options, BoxLayout.Y_AXIS);
options.setLayout(layout_Options);
JLabel optionsLblURL = new JLabel("Enter URL:");
optionsTxtUrl = new JTextArea(1,15);
JLabel chooseDestLbl = new JLabel("Choose save location:");
chooseDest = new JButton("Browse");
chooseDest.addActionListener(this);
options.add(optionsLblURL);
options.add(optionsTxtUrl);
options.add(chooseDestLbl);
options.add(chooseDest);
//Setup launch area
JPanel launch = new JPanel();
launch.setBorder(BorderFactory.createTitledBorder("Launch"));
launchBtnStart = new JButton("Start Download");
launchBtnStart.setVerticalAlignment(SwingConstants.CENTER);
launchBtnStart.setHorizontalAlignment(SwingConstants.CENTER);
launchBtnStart.addActionListener(this);
launch.add(launchBtnStart);
//Setup reporting area
JPanel logging = new JPanel();
logging.setBorder(BorderFactory.createTitledBorder("Log"));
BoxLayout layout_Logging = new BoxLayout(logging, BoxLayout.Y_AXIS);
logging.setLayout(layout_Logging);
JTextArea loggingTxt = new JTextArea(3,10);
loggingTxt.setEditable(false);
logging.add(pb);
logging.add(loggingTxt);
//Add components to window
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
add("North", toolBar);
add("West", options);
add("East", launch);
add("South", logging);
setVisible(true);
"My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?"
Start by putting your JTextArea in a ScrollPane and setLineWrap(true) and setWrapStyleWord(true). You may want to consider doing this with both JTextAreas you have
JTextArea optionsTxtUrl = new JTextArea(1,15);
optionsTxtUrl.setLineWrap(true);
optionsTxtUrl.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(optionsTxtUrl);
options.add(scroll); // take out options.add(optionsTxtUrl);
This will make your lines wrap when they reach the right edge of the text area
public void setWrapStyleWord(boolean word) - Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false.
public void setLineWrap(boolean wrap) - Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("lineWrap") is fired when the policy is changed. By default this property is false.
If this doesn't solve your problem, you should edit your post with a Minimal, Complete, Tested and Readable example so we can test out your problem.

Can I resize only the height of my frame using pack()?

I am writing a program that brings up a JDialog box that lists multiple options from a config file. The number of options can vary each time it is opened, so I need to be able to dynamically adjust the height of the window, but not the width. The window looks best using FlowLayout and defining the width of the window so that the JPanels that the data is in wrap propertly. But I am unable to dynamically adjust the height. I tried to use the pack method, but it lines all the panels up in one line like FlowLayout is meant to be. I tried GridLayout but it re-sizes all of the panels to the same size and is an unacceptable look. I also tried BoxLayout but was unable to get that one to work.
Is there a better layout manager to use or a way to make the width static?
Here is my code. Every panel and box is defined above the constructor, I just did not copy that code:
public ReSizeMe()
{
curConfig = new Config();
config = curConfig.getConfig();
setBox = new JDialog();
setBox.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set as 480, 600
setBox.setLayout(new FlowLayout());
this.buildSetFrame();
//setBox.pack();
setBox.setVisible(true);
}
public void buildSetFrame()
{
intPanel = new JPanel();
butPanel = new JPanel();
optPanel = new JPanel();
editPanel = new JPanel[maxOptions];
optWrapper = new JPanel[maxOptions];
intPanel.setLayout(new FlowLayout());
optPanel.setLayout(new GridLayout(10, 1)); // trying different things here too.
optText = new JTextField[maxOptions];
editButton = new JButton[maxOptions];
delButton = new JButton[maxOptions];
intPanel.setBorder(BorderFactory.createTitledBorder("Modify Interface"));
apply = new JButton("Apply");
newOpt = new JButton("New Option");
help = new JButton("Help");
close = new JButton("Close");
intPanel.add(ethIntLabel);
intPanel.add(ethIntName);
butPanel.add(apply);
butPanel.add(newOpt);
butPanel.add(close);
ethIntName.setText(config.getProperty("EthIntName"));
setBox.add(welcomeMsg);
setBox.add(intPanel);
setBox.add(optPanel);
buildOptions();
setBox.add(butPanel);
}
void buildOptions()
{
for (int i = 0; i < maxOptions; i++)
{
editable = Boolean.parseBoolean(config.getProperty("option." + i + ".edit"));
if (config.getProperty("option." + i + ".name") == null)
{
break;
}
else if (editable != false &&
config.getProperty("option." + i + ".name") != null &&
!config.getProperty("option." + i + ".name").isEmpty())
{
editPanel[i] = new JPanel();
optWrapper[i] = new JPanel();
optText[i] = new JTextField(20);
editButton[i] = new JButton("Edit");
delButton[i] = new JButton("Delete");
editButton[i].setActionCommand(Integer.toString(i));
delButton[i].setActionCommand(Integer.toString(i));
optText[i].setText(config.getProperty("option." + i + ".name"));
optText[i].setEditable(false);
editPanel[i].add(editButton[i]);
editPanel[i].add(delButton[i]);
optWrapper[i].add(optText[i]);
optWrapper[i].add(editPanel[i]);
optPanel.add(optWrapper[i]);
}
}
}
Yes, Box should work wonderful:
Box box = Box.createVerticalBox();
box.add(...)
box.add(Box.createVerticalStrut(5)); // spacing
<etc.>
add(box);

using a box layout in java

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.

Can I add more than on button to a java JFrame?

I am facing a problem, I want to add more than a button to a JFrame, but it only takes the last one and puts it into the frame, a sample of my code is below:
String isName = "";
JFrame frame = new JFrame(isName);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
String childAmb = "PDA276";
for (int j=0; j<3; j++){
if (childAmb.matches("Phone\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/phone.gif"), frame);
else if (childAmb.matches("PDA\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/pda.gif"), frame);
else if (childAmb.matches("PC\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/pc.gif"), frame);
}
frame.setVisible(true);
frame.setBounds(100, 200, 200, 200);
Thank you.
If you don't have a layout-manager, only one, the last component added will show up.
frame.setLayout (new FlowLayout ());
frame.add (new JButton ("foo"));
frame.add (new JButton ("bar"));
Read the section from the Swing tutorial on Using Layout Managers for examples.
You can start with the FlowLayout.

Categories