How add a JButton in a JPanel? - java

I want to add a JButton (panButton) in the lower area of this GUI, but I don’t know to which element I have to add it. I tried to do panButton.add(element) with several elements but nothing works.
I can't understand what is the main element of the panel.
This is how the GUI appears:
While this is the code:
private void buildGUI() {
setTitle("Avanzamento upload");
setIconImage(Toolkit.getDefaultToolkit().getImage(ClientUpload.class.getResource("/clientupload/resources/logoRFI.gif")));
addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
JLabel lblProgressAll = new JLabel("Totale: ");
JLabel lblProgressCurrent = new JLabel("File attuale: ");
JLabel lblTotalBytes = new JLabel("MB totali: ");
JLabel lblCopiedBytes = new JLabel("MB copiati: ");
lblTotalBytesValue = new JLabel("0 MB");
lblCopiedBytesValue = new JLabel("0 MB");
progressButton = new JButton("Interrompi"); //fc
progressAll = new JProgressBar(0, 100);
progressAll.setStringPainted(true);
progressCurrent = new JProgressBar(0, 100);
progressCurrent.setStringPainted(true);
txtDetails = new JTextArea(5, 50);
txtDetails.setEditable(false);
DefaultCaret caret = (DefaultCaret) txtDetails.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane scrollPane = new JScrollPane(txtDetails, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel contentPane = (JPanel) getContentPane();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panProgressLabels = new JPanel(new BorderLayout(0, 5));
JPanel panProgressBars = new JPanel(new BorderLayout(0, 5));
JPanel panCopyLabels = new JPanel(new BorderLayout(0, 5));
JPanel panCopyFields = new JPanel(new BorderLayout(0, 5));
panProgressLabels.add(lblProgressAll, BorderLayout.NORTH);
panProgressLabels.add(lblProgressCurrent, BorderLayout.CENTER);
panCopyLabels.add(lblTotalBytes, BorderLayout.NORTH);
panCopyLabels.add(lblCopiedBytes, BorderLayout.CENTER);
panCopyFields.add(lblTotalBytesValue, BorderLayout.NORTH);
panCopyFields.add(lblCopiedBytesValue, BorderLayout.CENTER);
panProgressBars.add(progressAll, BorderLayout.NORTH);
panProgressBars.add(progressCurrent, BorderLayout.CENTER);
JPanel panProgress = new JPanel(new BorderLayout(0, 5));
panProgress.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Avanzamento"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panCopy = new JPanel(new BorderLayout(0, 5));
panCopy.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Stato"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panDetails = new JPanel(new BorderLayout());
panDetails.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dettagli"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
//fc
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panProgress.add(panProgressLabels, BorderLayout.LINE_START);
panProgress.add(panProgressBars, BorderLayout.CENTER);
panCopy.add(panCopyLabels, BorderLayout.LINE_START);
panCopy.add(panCopyFields, BorderLayout.CENTER);
panDetails.add(scrollPane, BorderLayout.CENTER);
JPanel panUpper = new JPanel(new BorderLayout());
panUpper.add(panProgress, BorderLayout.NORTH);
panUpper.add(panCopy, BorderLayout.SOUTH);
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
I’d like it appears in the Stato area on the right, or under the Dettagli area.
Can anyone help me?

panButton is a JPanel which is never added to the GUI so if you add something to it, it -also- wont be added to the visible GUI.
You need to add the button to the button panel and add the button panel to the contentPane
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panButton.add(element); //Add button to panel
...
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
contentPane.add(panButton, BorderLayout.CENTER); //Add panel to contentPane (which I assume is added to the frame at some point as I can see it's elements on the UI you posted)
or just add it the 'Stato' area with panCopy.add(element);

Finally I created a new JPanel and I added the button there.

Related

How to manage components in Swing Layout? [duplicate]

I have some questions on positioning components and some questions on text fields and text areas (Java Swing). Any help is greatly appreciated.
Right now I am trying to have two text fields beside each other with a different label above each describing what that text field does. To achieve this I have placed them in a GridLayout(2, 2).
Is this the best way? It is the only way I know to have a label directly over another component. Is there a better way? What about if there is just one label above one button. Is it sensible to position this through a GridLayout(2, 1)? I am visually impaired so I do not think positioning buttons just by their pixel position is an option unless there is a simple way to place components at a relative number of pixels to another component.
That leads me to my next question.
What is the best way to have the same UI as above but with another component (button) centered under it. Essentially the UI should compose of two Named text fields with a calculate button under. The way I did this is by putting the above components in a panel, and adding that plus the calculate button to a surrounding panel with a GridLayout(2, 1). The problem is that the button becomes as big as the panel above it (I'm assuming). How can I adjust this and still have the button perfectly aligned under the panel of text fields/labels? Similarly with labels above text areas. The label should be small but have a larger space for the text area under.
(text field):
Again referring to the UI above, if the user types many characters into the first text field, will the letters go over the text field on the right? If so how can I prevent this?
If I append text to a text area and it is already full, will it automatically allow the user to scroll? If not what is a simple way to make the text area scrollable?
Right now I am not setting a size of the text area. Does it just grow as I add text? Does it have a default size as in number of characters?
There are a number of layout managers that might be capable of providing you with what you need.
MigLayout
JGoodies FormLayout
GridBagLayout
For, GridBagLayout would be my choice (I'm biased, as I've been using this layout manager for the past 12 years ;))
public class TestLayout17 {
public static void main(String[] args) {
new TestLayout17();
}
public TestLayout17() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("Label 1"), gbc);
gbc.gridx++;
add(new JLabel("Label 2"), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JTextField(10), gbc);
gbc.gridx++;
add(new JTextField(10), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 2;
add(new JButton("Click"), gbc);
}
}
}
I also agree with Eng.Fouad's suggestion of using compound containers to make your life easier in the long run
You might find Laying Out Components Within a Container a worth while read.
Right now I am trying to have two text fields beside each other with a
different label above each describing what that textfield does. To
achieve this I have placed them in a GridLayout(2, 2). Is this the
best way? It is the only way I know to have a label directly over
another component. Is there a better way? What about if there is just
one label above one button. Is it sensible to position this through a
GridLayout(2, 1)?
Myself, I always do it via nested panels with BorderLayout. For example:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
Note that, you can manipulate the gaps between the components with setting empty borders. Also, you may use BorderLayout.LINE_START and BorderLayout.LINE_END instead of using BorderLayout.WEST and BorderLayout.EAST, and this will add support for RTL languages (e.g Arabic).
That leads me to my next question. What is the best way to have the
same UI as above but with another component (button) centred under it.
Essentially the UI should compose of two Named text fields with a
calculate button under. The way I did this is by putting the above
components in a panel, and adding that plus the calculate button to a
surrounding panel with a GridLayout(2, 1). The problem is that the
button becomes as big as the panel above it (I'm assuming). How can I
adjust this and still have the button perfectly aligned under the
panel of textfields/labels?
I would do it via nested panels as I did earlier, but now the bottom panel has a FlowLayout layout manager to get a good size for the button:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
panOuter.add(panBottom, BorderLayout.SOUTH);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
Similarly with labels above text areas. The label should be small but
have a larger space for the text area under.
I would suggest you to use TitledBorder:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(txtConsole, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
third (text field): Again referring to the UI above, if the user types
many characters into the first text field, will the letters go over
the text field on the right? If so how can I prevent this?
Try the above code, and see how it acts :)
Fourth: If I append text to a text area and it is already full, will
it automatically allow the user to scroll? If not what is a simple way
to make the text area scrollable?
You need to use something called JScrollPane:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
JScrollPane srcPane = new JScrollPane(txtConsole,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(srcPane, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
I hope I answered all of your questions :)

How do I stack labels in Swing?

I am writing my first Swing app and I'm having some trouble stacking labels in code.
I have the following right now
I would like "Enter the name of the repo and the name of the" to be above "owner of that repo to search for open issues." so the window isn't so wide.
Here's my code:
public class MainFrame extends JFrame {
private Boolean submitted = false;
public MainFrame(String title) {
super(title);
// Set layout manager
setLayout(new BorderLayout());
// Create components
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel();
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panTop = new JPanel();
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panTopTop = new JPanel();
panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panTopBottom = new JPanel();
panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add components to content panel
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
panOuter.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panTop, BorderLayout.NORTH);
JLabel lblTop1 = new JLabel("Enter the name of the repo and the name of the\n", JLabel.CENTER);
JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER);
JLabel lblLeft = new JLabel("Repo", JLabel.CENTER);
JLabel lblRight = new JLabel("Owner", JLabel.CENTER);
JTextField txtLeft = new JTextField("Hello", 10);
JTextField txtRight = new JTextField("World", 10);
JButton btnBottom = new JButton("Submit!");
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtRight, BorderLayout.CENTER);
panBottom.add(btnBottom);
panTopTop.add(lblTop1);
panTopBottom.add(lblTop2);
panTop.add(panTopTop, BorderLayout.NORTH);
panTop.add(panTopBottom, BorderLayout.SOUTH);
this.setContentPane(panOuter);
this.pack();
btnBottom.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(!submitted)
btnBottom.setText(txtLeft.getText());
else
btnBottom.setText(txtRight.getText());
submitted = !submitted;
}
});
}
}
I tried to make a panel that has a NORTH and SOUTH component of labels, but it didn't work.
Does anyone have suggestions?
Thanks,
erip
You could try using a GridBagLayout...
JPanel panTop = new JPanel(new GridBagLayout());
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//JPanel panTopTop = new JPanel();
//panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//JPanel panTopBottom = new JPanel();
//panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//...
//panTopTop.add(lblTop1);
//panTopBottom.add(lblTop2);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTop.add(lblTop1, gbc);
panTop.add(lblTop2, gbc);
//panTop.add(panTopBottom, BorderLayout.SOUTH);
See How to Use GridBagLayout for more details
Now, you could get really sneaky and use a JTextArea...
JTextArea ta = new JTextArea(1, 20);
ta.setText("Enter the name of the repo and the name of the owner of that repo to search for open issues.");
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setBorder(null);
ta.setFont(UIManager.getFont("Label.font"));
ta.setOpaque(false);
ta.setFocusable(false);
ta.setEditable(false);
//JLabel lblTop1 = new JLabel("<html>Enter the name of the repo and the name of the owner of that repo to search for open issues", JLabel.CENTER);
//JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER);
//...
//panTopTop.add(lblTop1);
//panTopBottom.add(lblTop2);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTop.add(ta, gbc);
Or even just use Swing's HTML support...
JLabel lblTop1 = new JLabel("<html><p align='center'>Enter the name of the repo and the name of the owner of that repo to search for open issues</p>", JLabel.CENTER);
panOuter.add(lblTop1, BorderLayout.NORTH);

New JFrame doesn´t load gif when launched from another JFrame

I am trying to open up an JFrame with an gif-loading animation from another JFrame. The problem is, that the new JFrame opens but the gif animations just shows up if the loading time is over, so there is a empty JFrame during the whole loading sequence.
I am trying to open up my new JFrame in my current JFrame like this:
Ladescreen loading = new Ladescreen();
loading.setVisible(true);
loading.pack();
And this is how my Loadingscreen is built:
public Ladescreen() {
setTitle("Crawling through files...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 157, 207);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel picture = new JLabel();
picture.setIcon(new ImageIcon("football.gif"));
panel.add(picture);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("Loading...");
panel_1.add(lblNewLabel);
}
Can you tell me how to fix this up ?

Unable to make the JTextArea scrollable..!

Here is my code:
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
//contentPane.add(textArea); (edited...still the same problem persists..)
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(sbrText);
When ever I try with this,the text area is not visible..(I am using Eclipse's Window Builder plugin and layout as "Absolute Layout")..
Here I have done, the same thingy using Nested Layout, have a look at the code example :
import java.awt.*;
import javax.swing.*;
public class WelcomeExample
{
private JPanel headerPanel;
private JButton logoutButton;
private JPanel leavePanel;
private JRadioButton casualRButton;
private JRadioButton specialRButton;
private JRadioButton sickRButton;
private JRadioButton privilegeRButton;
private ButtonGroup radioButtonGroup;
private JTextField leaveDaysField;
private JButton checkLeaveButton;
private JTextArea notesArea;
private JScrollPane notesScroller;
private JButton applyLeaveButton;
private String headerText = "<html><body><h1><font " +
"color=\"red\">Welcome : </font><font color" +
"=\"blue\">Code Zero</font></h1></body></html>";
private void displayGUI()
{
JFrame frame = new JFrame("Welcome");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
headerPanel = getHeaderPanel();
leavePanel = getLeavePanel();
contentPane.add(headerPanel, BorderLayout.PAGE_START);
contentPane.add(leavePanel, BorderLayout.CENTER);
contentPane.add(getApplyPanel(), BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getHeaderPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel headerLabel = new JLabel(headerText, JLabel.CENTER);
JPanel buttonPanel = new JPanel();
logoutButton = new JButton("Logout");
buttonPanel.add(logoutButton);
panel.add(headerLabel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.LINE_END);
panel.add(new JSeparator(
SwingConstants.HORIZONTAL), BorderLayout.PAGE_END);
return panel;
}
private JPanel getLeavePanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel leaveHeaderPanel = new JPanel();
leaveHeaderPanel.setLayout(new GridLayout(0, 1, 5, 5));
leaveHeaderPanel.setBorder(
BorderFactory.createTitledBorder("Choose a leave type : "));
JPanel leaveTypePanel = new JPanel();
leaveTypePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
casualRButton = new JRadioButton("Casual Leave");
specialRButton = new JRadioButton("Special Leave");
sickRButton = new JRadioButton("Sick Leave");
privilegeRButton = new JRadioButton("Privilege Leave");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(casualRButton);
radioButtonGroup.add(specialRButton);
radioButtonGroup.add(sickRButton);
radioButtonGroup.add(privilegeRButton);
leaveTypePanel.add(casualRButton);
leaveTypePanel.add(specialRButton);
leaveTypePanel.add(sickRButton);
leaveTypePanel.add(privilegeRButton);
JPanel applyLeavePanel = new JPanel();
applyLeavePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
JLabel applyLeaveLabel = new JLabel(
"Apply for (No. of days) : ", JLabel.CENTER);
leaveDaysField = new JTextField(5);
checkLeaveButton = new JButton("Check Leave Availability");
applyLeavePanel.add(applyLeaveLabel);
applyLeavePanel.add(leaveDaysField);
applyLeavePanel.add(checkLeaveButton);
leaveHeaderPanel.add(leaveTypePanel);
leaveHeaderPanel.add(applyLeavePanel);
notesArea = new JTextArea(10, 10);
notesScroller = new JScrollPane();
notesScroller.setBorder(
BorderFactory.createTitledBorder(
"Leave Note (Max. 200 Characters) : "));
notesScroller.setViewportView(notesArea);
panel.add(leaveHeaderPanel, BorderLayout.PAGE_START);
panel.add(notesScroller, BorderLayout.CENTER);
return panel;
}
private JPanel getApplyPanel()
{
JPanel panel = new JPanel();
applyLeaveButton = new JButton("Apply");
panel.add(applyLeaveButton);
return panel;
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new WelcomeExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :
I don't think you need to do contentPane.add(textArea);. It is this line that is causing the problem. Comment out this and your code should work fine.
See this answer, it might help you.
The following code runs fine at my place :
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(sbrText);//contentPane.add(sbrText);
frame.setVisible(true);
If your code is not running fine then you must have some other error probably related to your contentpane.
Ok...I finally got it to work...I specified the same set bounds in the scroll pane instead of the text area.. Here is the code.!!! ^_^
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
//textArea.setBounds(77, 310, 474, 136);
JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBounds(77, 310, 474, 136);
contentPane.add(scroll);
Here are the screenshots:
Previous: http://oi40.tinypic.com/11jyum0.jpg
Now: http://oi44.tinypic.com/2s9vdvt.jpg

too much empty space inside jpanel with gridlayout

I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png
Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Categories