I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.
My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.
Here's my code, the second image is created by not adding headerPanel:
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
this.add(headerPanel);
this.add(descLabel);
This class extends JPanel, and is added to a JFrame, which is simply pack()'d
Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
descPanel.add(descLabel);// added
this.add(headerPanel);
this.add(descPanel);// modified
My problem is the margin to the left of the large lipsum box, how can I get rid of this?
You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.
I'm guessing the JLabel is center aligned so you need to use:
descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.
Related
Following Java's official tutorial:(https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)
However, there is no source code for this, picture:
So I did it myself. My stupid idea is that there are 4 areas in here. So BorderLayout does not work well for me. Because I already tried BorderLayout.PAGE_END. It's not working. So I make one panel consolidate all 3 panels except the first scroll pane. It's inefficient, but works.
But now the issue is when I drag(stretch) the frame, the last text-area not stretched, but the second last selection option is stretched.
How to make the last text area stretch when I resize the frame?
Below is my code:
JRadioButton mulintselRadioButton = new JRadioButton("Multiple Interval Selection");
JRadioButton singleselRadioButton = new JRadioButton("Single Selection");
JRadioButton singleIIntSelRadioButton = new JRadioButton("Single Interval Selection");
JCheckBox rowSelection = new JCheckBox("Row Selection");
JCheckBox columnSelection = new JCheckBox("Column Selection");
JCheckBox cellSelection = new JCheckBox("cell Selection");
ButtonGroup G = new ButtonGroup();
ButtonGroup GButton = new ButtonGroup();
GButton.add(rowSelection);
GButton.add(columnSelection);
GButton.add(cellSelection);
G.add(mulintselRadioButton);
G.add(singleIIntSelRadioButton);
G.add(singleselRadioButton);
JTable table = new JTable(data, columnNames);
TableColumn column = null;
table.setPreferredScrollableViewportSize(new Dimension(400,70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JPanel selectionMode = new JPanel(new GridLayout(4,1));
JPanel selectionOption = new JPanel(new GridLayout(4,1));
JPanel textAreaPanel = new JPanel(new GridLayout(1,1));
JPanel consolidatedPanel = new JPanel(new BorderLayout());
JLabel selcttionModeTitle = new JLabel("Selection Mode");
JLabel selcttionOptionTitle = new JLabel("Selection Options");
JTextArea textArea = new JTextArea("thsisaskjhkjk shial");
textAreaPanel.add(textArea);
selectionMode.add(selcttionModeTitle);
selectionMode.add(singleIIntSelRadioButton);
selectionMode.add(singleselRadioButton);
selectionMode.add(mulintselRadioButton);
selectionOption.add(selcttionOptionTitle);
selectionOption.add(rowSelection);
selectionOption.add(columnSelection);
selectionOption.add(cellSelection);
textArea.setText("hsknd hkcjshksdjl sldh RadioButton mulintselRadioButton = new JRadioButton(\"Multiple Interval Selection\");\n" +
" JRadioButton singleselRadioButton = new JRadioButton(\"Single Selection\");\n" +
" JRadioButton singleIIntSelRadioButton = new JRadioButton(\"Single Interval Selection\");\n" +
" JCheckBox rowSelection = new JCheckBox ");
add(scrollPane,BorderLayout.NORTH);
consolidatedPanel.add(selectionMode,BorderLayout.NORTH);
consolidatedPanel.add(selectionOption,BorderLayout.CENTER);
consolidatedPanel.add(textAreaPanel,BorderLayout.SOUTH);
add(consolidatedPanel);
Following the Java's official tutorial (the link of which you have posted in your question), one can read that it says that you can consult the example index... If you follow this link you can find a table which gives you the link to the file you are searching for. Specifically the code you are looking for is in the following link:
https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java
As for your question, you can use a BoxLayout to achieve what you are asking, exactly as the TableSelectionDemo does. The result can strech the table and the text area, but not the middle section.
The following program segment generates the output within the red box as shown in the figure:
st = c.con.createStatement();
rs = st.executeQuery("SELECT itemName, itemMaxQty, itemImage FROM item");
while(rs.next()){
JPanel itemPanel = new JPanel();
itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.PAGE_AXIS));
JPanel componentPanel = new JPanel();
componentPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel itemName = new JLabel(rs.getString(1));
ImageIcon itemImage = new ImageIcon(rs.getString(3));
Image scaleImage = itemImage.getImage().getScaledInstance(120, 120, Image.SCALE_SMOOTH);
JLabel imageHolder = new JLabel(new ImageIcon(scaleImage));
JSpinner quantityField = new JSpinner();
Dimension d = quantityField.getPreferredSize();
d.width = 60;
d.height = 30;
quantityField.setPreferredSize(d);
componentPanel.add(imageHolder);
componentPanel.add(quantityField);
itemPanel.add(componentPanel);
itemPanel.add(itemName);
add(itemPanel);
}
I know that probably some parts of the code are not the best way to do it, but that is what I can do for now in line with the knowledge that I have.
I actually have two problems here.
First, I don’t understand why the itemName's value (Cupcake Stand, chafers, etc) are centred.
1. How do I make it left centred such that it is aligned with its corresponding picture on top?
Second, as you can see, there is a large space in between the componentPanel (the picture and the JSpinner) and the itemName. I think it’s because the height allotted for itemName is the same as that for the componentPanel. So
2. How do I make itemName immediately below the componentPanel?
You can do that by changing the LayoutManager of itemPanel to BorderLayout (with that line itemPanel.setLayout(new BorderLayout());), and add the component to itemPanel in that way:
itemPanel.add(componentPanel, BorderLayout.CENTER);
itemPanel.add(itemName, BorderLayout.SOUTH);
I wrote a little code to see how the scroll Pane functions but my code never worked.
here's the code,
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work??
Any help explaining how scrollpane works?
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.
content.add(textArea);
content.add(imad);
A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.
Try something like:
JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.
I created an OptionDialog without any buttons and put a JPanel in it that uses MigLayout for its layout. That JPanel has another JPanel inside of it.
Both of these panels seem to have a margin on the outside of it. Maybe it is padding on the container. Either way I would like a way to get rid of them.
How can I get rid of these margins? In the picture they are the grey and the dark orange borders around the JPanels.
Here is the panel code:
setBackground(new Color(239,209,59));
setLayout(new MigLayout("wrap 1"));
JLabel title = new JLabel("Enroll Today!", JLabel.CENTER);
Font f = title.getFont().deriveFont((float)36);
title.setFont(f);
add(title);
JPanel docsPanel = new JPanel();
docsPanel.setBorder(BorderFactory.createEmptyBorder());
docsPanel.setLayout(new MigLayout("wrap 1", "", "[grow,fill]"));
docsPanel.setBackground(new Color(255,235,115));
for (final Document d : docs){
JButton doc = new JButton("* "+d.getName());
doc.setFont(f.deriveFont((float)24));
doc.setBorder(null);
doc.setContentAreaFilled(false);
docsPanel.add(doc);
}
add(docsPanel);
Here is the OptionDialog code:
DocumentPanel panel = new DocumentPanel(controller.getDocuments());
JOptionPane.showOptionDialog(null, panel, "Enroll now!", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, new Object[] {}, null);
Blindly, try "ins 0, wrap 1" in the MigLayout constructor.
I am using a JPanel as a Main panel to display information to my users.
I have created 3 other Jpanels using methods. titlePanel, verbiagePanel, closeButtonPanel. Each of these methods are assigned as a component and added to the main panel. I am using BoxLayout in the main panel as well as the other panels.
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
this.setTitle("EDI - Help Page");
//this.setResizable( false );
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(700, 300));
centerPanel.add(titlePanel, BorderLayout.NORTH);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel, BorderLayout.CENTER);
centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
centerPanel.add(closeButtonPanel, BorderLayout.SOUTH);
getContentPane().add(centerPanel);
this.pack();
Above it the main panel Method. When I compile and run the dialog, everything looks right except the verbiagePanel. It is only half the size of the parent panel and the other 2 panels.
Here is the code for my verbiagePanel
private JPanel verbiagePanel() {
String titleText = "<html><font size=4><b><center>How To Use This Application</b></center></font></html>";
String text = "<html>" +
" <P align=left><font size=3>" +
" 1. Add a data to the list of Selected Datasets.<br/>" +
" (see below for detailed instructions on adding datasets)<br/>" +
" 2. Select the project to send data to.<br/>" +
" 3. Adjust the list of selected 3D or 2D translators if desired.<br/>" +
" 4. Use the Send button to continue the EDI transaction." +
" </font></P>" +
" </html>";
JLabel titleLabel = new JLabel(titleText, JLabel.CENTER);
titleLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel bodyLabel = new JLabel(text, JLabel.LEFT);
bodyLabel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(titleLabel);
p.add(bodyLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
The funny thing is, if I remove the BoxLayout from the Panel. The Panel will expand to match the other 2 panels. But the spacing of the labels are crazy. I will end up having at least 5 labels in the panel. Right now I am only showing 2 to make it more simple.
I ended up using GrigBagLayout model. It worked as needed.