I am trying to add combo box to the JPanel dynamically but the combo box occupying entire panel.According to the combo box count the size of the combo boxes are changing but I want fixed size of the combo box and I need to create the combo boxes one by one means below of another combo /in a new line.
How to set the location of the components in a panel.
JComboBox startDate = new JComboBox();
startDate.setPreferredSize(new Dimension(80,25));
jPanelStartDate.add(startDate);
jPanelStartDate.setLayout(new GridLayout(0, 3, 10, 10));
jPanelStartDate.revalidate();
Ok, you have more than one option. You can use a BoxLayout with a Y_Axis and a rigid area or you can use the more advanced, complex and dynamic GridBagLayout. The following is an example with a BoxLayout and rigid areas.
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
for(int index = 0; index < 5; ++index){
JComboBox<String> box = new JComboBox<>(new String[]{"a", "b", "c"});
box.setMaximumSize(new Dimension(50, 50));
box.setMinimumSize(new Dimension(50, 50));
panel.add(box);
panel.add(Box.createRigidArea(new Dimension(10, 10)));
}
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setVisible(true);
You can change the dimensions, specially of the rigid area to adjust the spaces and the sizes of the components according to your needs.
Related
This is my plugin to the application with Miglayout:
This is MigLayout initialistion code, layout occupies 100% space of main panel:
resPanel.setLayout(new BoxLayout(resPanel, BoxLayout.Y_AXIS));
mainPanel.setLayout(new MigLayout("", "[][grow][grow][]", "[][][][grow]"));
mainPanel.add(new JLabel("Class expression"), "wrap");
mainPanel.add(owlDescriptionEditor, "growx,span 3");
mainPanel.add(calcButton, "wrap");
mainPanel.add(new JLabel("Definitions found"), "span 2");
mainPanel.add(new JLabel("Target signature"), "span 2,wrap");
JScrollPane jsp = new JScrollPane(resPanel);
mainPanel.add(jsp, "growy, growx, span 2");
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
mainPanel.add(new JScrollPane(deltaList), "growx,growy,span 2");
The problem is not working horizontal scrollbar of the left list, which is JPanel with BoxLayout, even though I call setVerticalScrollBarPolicy for it. JPanel is populated dynamically with this loop:
static void updateList(JPanel panel, Collection<OWLClassExpression> list) {
panel.removeAll();
for (int i = 0; i < 3; i++) {
for (OWLClassExpression p : list) {
String name = ((OWLNamedObject) p).getIRI().getShortForm();
JEditorPane jep = new JEditorPane("text/html", name+"QWQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
jep.setPreferredSize(new Dimension(200,20));
jep.setEditable(false);
jep.setOpaque(false);
panel.add(jep);
}
}
panel.add( Box.createVerticalStrut(400) );
}
I had to add last line to get vertical scrollbar working. If I don't add it, then its also missing, even though it has side effect of extra empty space in the bottom. How can I make scrollbars work properly?
jep.setPreferredSize(new Dimension(200,20));
Don't hardcode preferred sizes.
Each component will dynamcially determine its own preferred size based on the properties of the component (text, font etc.).
The layout manager can then dynamically determine the size of the panel based on the preferred size of each component and scrollbars will appear when required.
Also after you are finished adding all the components to the panel you need to use:
panel.revalidate();
To invoke the layout manager so the new size of the panel can be calculated.
So I have a problem that I have to make a questionnaire about something and I have to use multiple Layouts.
My problem is that when I add 2 JPanels to a Grid layout (only to 1 side) my first Panel takes up most of the space.
Code:
public class MainFrame extends JFrame implements ItemListener{
JPanel mainPanel,rightSideAge,rightSideGender,leftSide,rightSideBox,leftSideBox;
JTextArea nameArea;
JSpinner ageSpinner;
JRadioButton genMale,genFema;
ButtonGroup genderGroup;
MainFrame(){
this.setSize(1000, 800);
this.setLocationRelativeTo(null);
this.setTitle("Közvélemény kutatás a zenei ízlésekről");
mainPanel = new JPanel(new GridLayout(0, 2));
this.setContentPane(mainPanel);
/* --- RIGHT PANEL --- */
rightSideBox = new JPanel();
rightSideBox.setLayout(new BoxLayout(rightSideBox, BoxLayout.Y_AXIS));
rightSideAge = new JPanel(new FlowLayout(FlowLayout.LEFT));
rightSideAge.setBorder(BorderFactory.createLineBorder(Color.BLUE));
//rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
mainPanel.add(rightSideBox);
//Age label
//JLabel labelAge = new JLabel("Kor: ");
//labelAge.setSize(100, 30);
//Age Spinner
ageSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 120, 1));
ageSpinner.setPreferredSize(new Dimension(40, 20));
Component mySpinnerEditor = ageSpinner.getEditor();
JFormattedTextField jftf = ((JSpinner.DefaultEditor) mySpinnerEditor).getTextField();
jftf.setColumns(5);
//New box for zenei ízlés
rightSideGender = new JPanel();
rightSideGender.setBorder(BorderFactory.createLineBorder(Color.GREEN));
rightSideGender.setLayout(new BoxLayout(rightSideGender,BoxLayout.Y_AXIS));
//Gender ComboBox
genderGroup = new ButtonGroup();
genMale = new JRadioButton("Férfi");
genderGroup.add(genMale);
genFema = new JRadioButton("Nő");
genderGroup.add(genFema);
/* --- LEFT SIDE --- */
rightSideBox.setBorder(BorderFactory.createLineBorder(Color.RED));
/* ADD STUFF TO PANELS */
/* RightSideBox */
rightSideBox.add(rightSideAge);
rightSideBox.add(rightSideGender);
/*RIGHT SIDE PANELS*/
rightSideGender.add(new JLabel("Nem:"));
rightSideGender.add(genMale);
rightSideGender.add(genFema);
rightSideAge.add(new JLabel("Kor"));
//rightSide.add(labelAge);
rightSideAge.add(ageSpinner);
/*LEFT SIDE PANEL*/
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
The Blue lineout sould be only under the JSpinner:
ageSpinner.setPreferredSize(new Dimension(40, 20));
First of all, you should not be manually setting the size of a component. Each Swing component is responsible for determining its own size.
The Blue lineout should be only under the JSpinner:
The box layout will resize a component to its maximum size if there is space available. For some reason a JSpinner doesn't appear to have a maximum height so it expands to fill all the available space.
To fix this you can do something like:
//ageSpinner.setPreferredSize(new Dimension(40, 20));
ageSpinner.setMaximumSize( ageSpinner.getPreferredSize() );
mainPanel layout is set to have two columns:
mainPanel = new JPanel(new GridLayout(0, 2));
You only add one panel to mainPanel which uses the GridLayout:
mainPanel.add(rightSideBox);
Note: the first component you add, in this case rightSideBox will occupy the first column, meaning it will be the LEFT one.
To add rightSideGender to mainPanel you need to :
mainPanel.add(rightSideGender);
The second component you add, in this case rightSideGender will occupy the second column, in this case the RIGHT one.
In a part of my software I have a layout on bottom that holds couple of JButtons and a JLabel. I want to keep buttons one the right side of the panel, and label on the left side. I could manage to put buttons on the right, but do not know how to keep the JLabel on the left side.
Here is the code:
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
ftpBack = new JButton("Back");
ftpNext = new JButton("Next");
label = new JLabel("Text);
bottomPanel.add(label);
bottomPanel.add(ftpBack);
bottomPanel.add(ftpNext);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
This is what I am trying to achieve:
Any idea how to make it ?
You can't do this with a FlowLayout.
You can use a horizontal BoxLayout:
Box box = Box.createHorizontalBox();
box.add(label);
box.add(Box.createHorizontalGlue());
box.add(backButton);
box.add(Box.createHorizontalStrut(5));
box.add(nextButton);
Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples.
Or another approach is to nest layout managers:
JPanel main = new JPanel( new BorderLayout() );
main.add(label, BorderLayout.WEST);
JPanel buttonPanel= new JPanel();
buttonPanel.add(back);
buttonPanel.add(next);
main.add(buttonPanel, BorderLayout.EAST);
I have a panel which is divided by two parts with BoxLayout.X_AXIS:
public TabsPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLeftPanel());
add(createRightPanel());
}
Each left and right panels have the following structure: an outer panel with BorderLayout, and an inner panel in BorderLayout.CENTER of the outer panel, which in its turn has BoxLayout.Y_AXIS and several components from top to bottom. The right panel has JTextArea with JScrollPane as one of its components:
protected JPanel createRightPanel() {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextArea label = createLabel();
JScrollPane scroll = new JScrollPane(label);
scroll.setMaximumSize(new Dimension(500, 200));
panel.add(Box.createRigidArea(new Dimension(0,106)));
panel.add(scroll);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
Font font_text = new Font("Georgia", Font.PLAIN, 20);
JButton[] buttons = new JButton[2];
buttons[0] = new JButton("Clear");
buttons[1] = new JButton("Exit");
for (int i = 0; i < buttons.length; i++) {
buttons[i].setMaximumSize(new Dimension(120, 40));
buttons[i].setFont(font_text);
panel_buttons.add(buttons[i]);
if (i == 0)
panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
buttons[i].addActionListener(new TextActionListener(label));
}
panel.add(Box.createRigidArea(new Dimension(0,20)));
panel.add(panel_buttons);
pane.add(panel, BorderLayout.CENTER);
return pane;
}
When text goes beyond the borders, scroll bars appear and I can move them and read the text. Looks like everything is ok, but when I either click any place outside the scroll pane or even just move the pointer, the scroll pane moves to the left and grows down. It doesn't change its width, but it shifts to the left because the area between it and the right panel's borders increases. Accordingly, size of the left panel shrinks. When I clear the text area and again either click or move the pointer, it is back to its normal size.
What is the reason its height grows and its left and right margins increase? What am I doing wrong?
UPDATE. I've found the problem. The thing is that I didn't create JTextArea correctly. I initialized it without parameters:
JTextArea text = new JTextArea("Some initial text");
Now I have rewritten:
JTextArea text = new JTextArea(5,10);
It is now shifted to the left by about 5 mm and do not changes its height. Still not perfect, but looks like I am on the right track.
Thank you everybody for your help!
BoxLayout accepting Min, Max and PreferredSize override those methods for JPanel
use JSPlitPane, there you can to hide Divider
2 steps to correct:
Set the size of the JTextArea: JTextArea text = new JTextArea(row, col);
Still shifts to the left by the size of the vertical bar:
either add ChangeListener to adjust the size of the JScrollPane
scroll.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (scroll.getVerticalScrollBar().isVisible())
scroll.setPreferredSize(480, 200);
}
}
});
or add scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
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());