Im creating a JDialog and adding components to it as such:
Window thisWin = SwingUtilities.getWindowAncestor(ancestorPanel);
final JDialog progressDialog = new JDialog(ancestorPanel, "There was an error");
progressDialog.setUndecorated(true);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(600, 600));
Next I add one JLabel, one JTextArea, one JScrollPane that contains a table, and finally a button as such:
label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
label.setHorizontalAlignment(JLabel.LEFT);
area.setAlignmentX(JLabel.LEFT_ALIGNMENT);
Box vBox1 = Box.createVerticalBox();
vBox1.add(label);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(area);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(scroll);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(button);
contentPane.add(vBox1);
progressDialog.setContentPane(contentPane);
progressDialog.pack();
progressDialog.setLocationRelativeTo(ancestorPanel);
progressDialog.setVisible(true);
The results is exactly as I want except for the button. The label is on top (aligned to the left), then the text area comes below it (also aligned to the left), then below that comes the table, and finally the button, but I can't seem to make the button appear in the middle of the row. Its appearing on the left. I tried using the following line but it didn't work:
button(JButton.CENTER_ALIGNMENT);
How can I get the button to appear in the center of the last row?
I managed to solve the above by adding the button to a separate Box and then using Boxlayout to add both boxes to the panel as such:
Box vBox1 = Box.createVerticalBox();
vBox1.add(label);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(area1);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(scroll);
vBox1.add(Box.createVerticalStrut(7));
Box vBox2 = Box.createVerticalBox();
vBox2.add(button);
contentPane.add(vBox1, BorderLayout.CENTER);
contentPane.add(vBox2, BorderLayout.PAGE_END);
Related
I am attempting to design a panel with MiGFormat that has a label at the top, and two buttons at the bottom - a yes/no prompt.
I achieve this closely, but the label yesOrNoText (text is "TEST") is not fully centered:
I initialize the panel containing this prompt like so:
private JPanel createYesNoPrompt() {
JPanel panel = new JPanel(new MigLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.red));
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
yesOrNoText = new JLabel();
yesOrNoText.setText("TEST");
yesOrNoText.setFont(panel.getFont().deriveFont(Font.BOLD, 30f));
yesOrNoText.setHorizontalAlignment(SwingConstants.CENTER);
Dimension dimension = new Dimension(500, 125);
Font font = panel.getFont().deriveFont(Font.BOLD, 20f);
yesButton.setFont(font);
yesButton.setBackground(new Color(35, 138, 35));
yesButton.setPreferredSize(dimension);
noButton.setFont(font);
noButton.setBackground(new Color(183, 19, 19));
noButton.setPreferredSize(dimension);
yesButton.addActionListener(e -> isYes = true);
noButton.addActionListener(e -> isYes = false);
panel.add(yesOrNoText, "wrap, dock center");
panel.add(yesButton);
panel.add(noButton);
return panel;
}
Then, I add it to gamePanel, then gamePanel to mainPanel, then mainPanel to the frame.
gamePanel.add(YesOrNoPanel, "align center");
mainPanel.add(gamePanel);
add(mainPanel);
I'm unsure of what would be causing yesOrNoText to not become fully centered within the YesNoPanel. Please let me know if I need to clarify anything!
Thank you.
I needed to make the add call for the yesNo label span 2 cells. By adding one component in the first row, then adding two in the next, I essentially created a 2x2 grid.
panel.add(yesOrNoText, "wrap, align center, span 2 1");
panel.add(yesButton);
panel.add(noButton);
Notice that on the first component I add yesOrNoText I use span to tell MiGFormat to take up two cells for this component. I can then center that with the remaining two components because it becomes the only component in the row.
I want to have a textArea to display results that can be scrolled. The scrollbar doesn't appear even though I set it to VERTICAL_SCROLLBAR_ALWAYS
What Am I doing wrong??
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
// automatically added to contentPane with gameFrame.add()
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// text are to show registered players
JTextArea display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
display.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// Add Text area
playerPanel.add(scroll);
playerPanel.add(display);
}
You're adding both the JScrollPane AND it's display to the GUI -- DON'T do that. Add just the JScrollPane. It holds the display and that is what you need.
So change:
playerPanel.add(scroll);
playerPanel.add(display);
to
playerPanel.add(scroll);
// playerPanel.add(display);
Question: why are you setting the layout manager of your JTextArea? That really makes little sense.
I'm busy writing a button menu for a Java Swing application and I am wondering if it is possible to remove the padding between JButtons that are added to a JPanel.
The JPanel uses a FlowLayout that is aligned left.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT));
The buttons are standard JButtons
JButton buttOne = new JButton("One");
JButton buttTwo = new JButton("Two");
I added the JButtons to the panel as normal
add(panelMenu, BorderLayout.NORTH);
panelMenu.add(buttOne);
panelMenu.add(buttTwo);
Everything works as expected but what do I need to do to remove the default spacing between the buttons?
I found a suggested solution online which is the following
buttOne.setBorder(null);
buttOne.setBorderPainted(false);
buttOne.setMargin(new Insets(0,0,0,0));
buttTwo.setBorder(null);
buttTwo.setBorderPainted(false);
buttTwo.setMargin(new Insets(0,0,0,0));
However this seems to remove the spacing inside of the button and not the spacing between each button.
Is this spacing produced by the FlowLayout? If so, how can I remove it?
// 0, 0 equates to horizontal and vertical offsets, the default is 5.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
Should sort it!
The FlowLayout controls the spacing, the default is 5.
Use new FlowLayout(FlowLayout.LEFT, 0) to remove the spacing.
I have a JScrollPane on a panel (which has many panels inside of it) and when I open the frame with the scroll pane on it, the fame is scrolled to the bottom. Is there anyway I can avoid this?
Here are some facts:
The panel the scroller is on contains multiple panels. Some of these panels have text fields. I have tried to set the carat of the text fields to 0 and this did not work.
I know there should be actual code, but when I tried to make a mock pane (as the one I am using is intertwined with a lot of code) it is not replicating the issue.
The panel that is being scrolled is being generated using a loop that generates a series of questions... so a text box, a said amount of buttons/answers, and a text box and label that shows the amount of points for each question.
The last elements of my panel that is being scrolled are a JTextArea and a JLabel.
Below is the code to declare those.
Is there anyone out there that could at least throw out an idea of what would be making the scroll pane automatically go to the bottom?
Here is the declaration of the pane and the panels inside/outside of it
JPanel newPanel = new JPanel(new BorderLayout(0, 0));
JPanel showPanel = new JPanel();
BoxLayout layout = new BoxLayout(showQuizPanel, BoxLayout.Y_AXIS);
showQuizPanel.setLayout(layout);
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.white);
newPanel.setBackground(Color.white);
showPanel.setBackground(Color.white);
populateButtonPanel();
populateShowPanel(showPanel, buttonPanel);
populateQuestions(showPanel);
JScrollPane scrollPane = new JScrollPane(showPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getViewport().setBackground(Color.WHITE);
scrollPane.setAlignmentX(JScrollPane.CENTER_ALIGNMENT);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.getViewport().setViewPosition(new Point(0,0));
newPanel.add(scrollPane, BorderLayout.CENTER);
return newPanel;
code to declare last elements on page
JPanel pPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
pPanel.setBackground(Color.WHITE);
pPanel.add(qValue);
pPanel.add(new JLabel("Points"));
qArea.add(pPanel);
qArea.add(Box.createVerticalStrut(50));
qValue.setCaretPosition(0);
Thanks!
I believe that when you add text to a text area when building the GUI, the scroll pane will scroll to make the text area visible.
So basically you need to reset the scroll pane to the top.
You can use code like the following after adding all components to the scroll pane:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
scrollPane.getViewport().setViewPosition( new Point(0, 0) );
}
});
The invokeLater() adds the code to the end of the Event Dispatch Thread (EDT) so that it gets executed after the GUI is visible.
Of course this code assumes that you are creating the rest of the GUI properly on the EDT.
I just started messing around with the BoxLayout manager.
I've made two buttons next to each other, and the third one should go to the next line (underneath the first two), and the two first buttons should be at the top of the frame.
How can I accomplish this?
This is my current code
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(new JButton("Button"));
box.add(new JButton("Hello"));
box.add(Box.createVerticalBox());
box.add(Box.createVerticalStrut(100));
box.add(new JButton("Button2"));
add(box);
Your current code looks nothing like the description you give of what you want. It sounds like you need
a top-level vertical box
a horizontal box
button
gap
button
gap
button
So something like
Box vbox = Box.createVerticalBox();
Box hbox = Box.createHorizontalBox();
hbox.add(new JButton("Button"));
hbox.add(Box.createHorizontalStrut(10));
hbox.add(new JButton("Hello"));
vbox.add(hbox);
vbox.add(Box.createVerticalStrut(100));
vbox.add(new JButton("Button2"));