I know this question was asked before, but there was no answer to it.
In Java, when I am adding components to the frame, all the elements after adding a JTextField are not rendered when the application is initialized. They are rendered after you refresh the screen e.g. minimize and maximize the screen. In the following only the textfield is rendered. It looks like some Java rendering issue.
My code is as follows:
private void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setBounds(100, 100, 569, 321);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtGenerationRate = new JTextField();
txtGenerationRate.setBounds(322, 29, 86, 20);
frame.getContentPane().add(txtGenerationRate);
txtGenerationRate.setColumns(10);
lblAmountOfSolarPanelsText = new JLabel("Amount of solar panels:");
lblAmountOfSolarPanelsText.setBounds(10, 57, 159, 14);
frame.getContentPane().add(lblAmountOfSolarPanelsText);
frame.setVisible(true); // added it for the second time, just to make sure
}
Can anyone help, please?
Piotr
At first, you should definitely read about how to use layouts: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
The second thing, you should set your frame visible after all components are added, if you do not, you have to call repaint().
Try putting this at the bottom of the initialize method
frame.setVisible(true);
frame.setBounds(100, 100, 569, 321);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
you could also try frame.revalidate(); and frame.repaint();
This seems working for me, I see both label and text field when the window first appears. The label text does not fit into label so is truncated, but apart that everything looks as expected. Maybe the wrong fragment has been removed from the code while trying to simplify it?
Related
I have such piece of code which shoud add button over JTextArea placed in JScrollPane. Button isn't inside scroll pane!
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 340, 375, 242);
contentPane.add(scrollPane);
JButton btnClean = new JButton();
btnClean.setBounds(340, 341, 26, 23);
contentPane.add(btnClean);
taLog = new JTextArea();
taLog.setEditable(false);
taLog.setLineWrap(true);
taLog.setFont(new Font("Arial", Font.PLAIN, 12));
scrollPane.setViewportView(taLog);
epInfo = new JEditorPane();
epInfo.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
epInfo.setFont(new Font("Tahoma", Font.PLAIN, 12));
epInfo.setContentType("text/html");
epInfo.setEditable(false);
epInfo.setBackground(null);
epInfo.setBorder(null);
epInfo.setBounds(10, 241, 375, 37);
setInfoText();
contentPane.add(epInfo);
Problem is whenever JTextArea changes its value button is not refreshed - it just dissapears - until I will drag mouse cursor over it. I suppose then some repaint() is launched.
I figured out I can add DocumentListener to JTextArea and there manually refresh button but it works until scroll bar appears in JScrollPane.
I can also use
scrollPane.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e)
{}});
but it is invoked many times and all those refreshes are not neccesary.
Is there any reasonable (and efficient) way to check if JScrollPane changed?
Null layout is useful in this case because main window is fixed
No it is not useful.
Any time you code uses random numbers (ie to set the size/location) of components) the code is not easily maintainable.
Also I use WindowBuilder
Code generated by an IDE is not maintainable in another IDE.
Learn to use layout managers. The point of using layout managers is to avoid hard coding of numbers and to make the code maintainable in any environment.
Problem is whenever JTextArea changes its value button is not refreshed
contentPane.add(scrollPane);
...
contentPane.add(btnClean);
Swing is designed to display components in two dimension when added to the same container.
If you want components displayed in two dimension then you need to have a parent / child relationship:
- content pane
- text area
button
So the code should be something like:
textArea.setLayout( new FlowLayout(FlowLayout.RIGHT) );
//contentPane.add(btnClean);
textArea.add(btnClean);
Note: you can configure the FlowLayout to remove the spacing on the top and right.
The problem with this is that the button will cover any text that displayed in the text area.
So the better solution is to make the button external to the text area.
I need help adding a scrollbar to an empty textbox that will output the information that i input into the required textboxes by then clicking the buttons. My issue is im not sure how to implement the scrollbar correctly with the textbox that is in the code, it just goes over and it and does nothing like in the picture shown. I need it to align with the textbox and not mess with the Jtextfield so that it can properly scroll the textbox, i will handle the events after i finish the design.
Code:
// Display box for all the inputs
JTextField outputBox = new JTextField(5);
wv.add(outputBox, 39, 575, 800, 150);
JScrollBar outputBoxScrollBar = new JScrollBar(JScrollBar.VERTICAL, 30, 20, 0, 500);
wv.add(outputBoxScrollBar, 790, 300, 50 , 250);
Output:
https://i.stack.imgur.com/oHnY8.png - Link for different OS
A JTextField is single-line, I guess you are looking for JTextArea.
You need to wrap JTextArea in a JScrollPane (not a JScrollBar) like this.
JTextArea outputBox = new JTextArea(5, 20); //rows, columns
wv.add(new JScrollPane(outputBox), 790, 300, 50 , 250);
I can't add scroll bar on EditorPane.
private JEditorPane editorPane;
private JScrollPane scrollpane;
Container :
Container c = getContentPane();
c.setLayout(null);
setBounds(100, 100, 450, 300);
editorPane = new JEditorPane();
editorPane.setBounds(0, 54, 434, 208);
scrollpane = new JScrollPane(editorPane);
scrollpane.setPreferredSize(new Dimension(350, 110));
c.add(scrollpane);
..
..
Nothing added
You're shooting yourself in the foot here:
editorPane.setBounds(0, 54, 434, 208);
By setting the editorPane's absolute size, you prevent it from expanding when it needs to do so, preventing the JScrollBars from having to show.
Solution: don't do this. And yeah, avoid using null layouts. They'll bite you, as you're finding out. Set the width using CSS
getContentPane().setLayout(null);
This means "I give a damn on the help of others because I know better that anyone else how to layout a GUI!"
So this is where you are.
I'd recomment to go through the tutorials and learn how to build GUIs using LayoutManagers.
I'm trying to align the position (start + end) of text inside a JTextField, but i haven't been able to find and/or understand how to do this...
example of what is wrong and what i'm looking for:
The setHorizontalAlignment method only allows this..:
•JTextField.LEFT
•JTextField.CENTER
•JTextField.RIGHT
•JTextField.LEADING
•JTextField.TRAILING
With android projects, this is so easy...but for the sake of me, i can't figure this one out, when doing a swing project...How can this be done?
EDIT:
I've been able to sort this one, by using double (aka layered) JTextFields. emptyborder(), although easier code wise, removes all JTextField frame borders, leaving you with a big empty square, which, for an input box, it's not a good idea...here's a picture and some code sample:
JTextField Padding Working example
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class TextBoxOffset {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setSize(300, 300);
frame.setVisible(true);
JLabel label1 = new JLabel("JTextField, using CreateEmptyBorder");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("Tahoma", Font.PLAIN, 11));
label1.setBounds(0, 125, 284, 23);
frame.getContentPane().add(label1);
//Creates a new JTextField with a emptyborder
JTextField emptyborder = new JTextField();
emptyborder.setBounds(22, 164, 250, 43);
emptyborder.setBorder(new EmptyBorder(5, 17, 5, 15));
frame.getContentPane().add(emptyborder);
JLabel label2 = new JLabel("JTextField, using double (layered) JTextFields");
label2.setFont(new Font("Tahoma", Font.PLAIN, 11));
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setBounds(0, 22, 284, 23);
frame.getContentPane().add(label2);
// Creates a new input field, layered above the (disabled) JTextField background object
JTextField inputbox = new JTextField();
inputbox.setBounds(40, 61, 216, 31);
inputbox.setBorder(null);
frame.getContentPane().add(inputbox);
// Creates a border for the input text field that will serve as a background
JTextField bg = new JTextField();
bg.setBounds(22, 56, 250, 43);
frame.getContentPane().add(bg);
bg.setEnabled(false);
// Grab the overlayed inputbox focus
inputbox.requestFocusInWindow();
}
}
If anyone knows a better, easier way of doing this, meaning, control the text padding on all sides (TOP,LEFT,BOTTOM,RIGHT), inside a JTextField (or any other field for what matters (e.g.: control the position of the text inside a button) without using double JTextFields, please post a reply.
I think you want to add a Border to your text field:
textField.add( new EmptyBorder(...) );
Read the section from the Swing tutorial on How to Use Borders for more information and examples.
i can't figure this one out, when doing a swing project.
Keep the link to the Swing tutorial handy for Swing basics.
Edit:
Oops, I forgot a JTextField also has a special method to control this:
textField.setInsets(...);
So you don't need the CompoundBorder, however so you still be aware of the flexibility provided by a CompoundBorder.
I think the easiest way to achieve this is by using :
textField.setMargin(new Insets(...));
I was looking for the same kind of result and this literally made my day, I hope it'll help others too.
I would like to achieve the below layout.
There are 6 panels. The 4 buttons at the top are one panel, and the 3 buttons at the right side of the image are also in one panel. Apart from those two there are 4 other panels as indicated by the borders. I tried the below code but displays everything in a scattered way.
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
How can I achieve the design as shown in the image? I need all the panels to be arranged inside that mainPanel. I cannot use null layout though. Please advice.
After trashgod's answer :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
What I get :
Add lefsideToolBarPanel and descriptionPanel to a panel having GridLayout; add the new panel to the BorderLayout.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
There is no BorderLayout.LEFT. See also A Visual Guide to Layout Managers.
Addendum: Your updated question shows elements of topToolBarPanel, which should be added to PAGE_START, rather than LINE_START.
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the propertiesPanel and height of the tablePanel need to be increased. I used setSize()…
For the propertiesPanel, you can override getPreferredSize(), as discussed here. For the tablePanel, override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane, for example.
I suggest using a JLabel as your "layout" to use exact positioning of yout objects with setBounds(x, y, width, height). It would look similar to this :
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
I know that this is just a quick example for you, but I think it should do for explanation... so just add everything on the backgr JLabeland your good to go. Quick and dirty example but the a way to go.