Best way to add a String in a JFrame - java

which is the best way to add formated string in jframe.
Previously I was trying to add jlabel

If you want to display some text in a window, yes, adding a JLabel to your JFrame is fine.
Just create an instance of the font you want and assign it to the JLabel using setFont.
Here is a code samle (taken from here):
Font font = new Font("Jokerman", Font.PLAIN, 35);
JLabel textLabel = new JLabel(textMessage);
textLabel.setFont(font);

Related

only one JLabel displaying at a time (Java Swing) [duplicate]

This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 6 years ago.
I created one JLabel as a heading and customised it, which displayed with no issue. The problem came when I attempted to create a second JLabel, as only the second JLabel would display. When I created a third one, only that would display but not the first and second.
Basically, only the latest JLabel is being displayed. How can I have multiple JLabel's displayed simultaneously.
Here is my code.
public class MainForm extends JFrame {
EmployeeDAO dao = new EmployeeDAO();
private static JLabel label, name;
MainForm()
{
super("Employee Database");
GUI();
}
private void GUI()
{
JLabel label = new JLabel("Enter Employee Information");
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(new Font("Serif", Font.PLAIN, 20));
label.setForeground(Color.red);
add(label);
JLabel name = new JLabel("Name: ");
add(name);
}
}
Usually, when you want to add more than one component to a container (as you're doing with the JLabels in the JFrame) you must decide which layout manager do you want to use so that components are displayed in the positions you want.
You should probably take a look at the tutorials related to laying out components in containers.
You will need to select a Layout Manager, and call setLayout accordingly.
Use .setBounds for each element you are adding to your scene, or use another layout such as grid layout to position your UI elements. The problem stems from when you add a new element, it goes on top of previous one.

Is it possible to implement scrollbar in Jframe without Jpanel?

I've written a long code involving several frames using only JFrame, and now I need a scrollbar for one of the frames. It seems like the only way to use JScrollPane is with JPanel, but I dont want to rewrite everything, is there a way?
frames[2] = new JFrame("Title");
frames[2].setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//EXIT_ON_CLOSE
frames[2].setContentPane(new JLabel(new ImageIcon("/Users/mac/Downloads/bg.000.jpg")));
frames[2].setLayout(new BorderLayout());
frames[2].setLayout(new FlowLayout());
frames[2].setResizable(true);
frames[2].setSize(750,433);
label[4] = new JLabel (" Search : ", JLabel.CENTER);
label[4].setFont(new Font("Serif", Font.ITALIC, 16));
label[4].setForeground(Color.BLACK);// font color
frames[2].add(label[4]);
TextField[2]= new JTextField ("Enter name", 15);
TextField[2].setFont(font);
frames[2].add(TextField[2]);
This is the frame (frame[2]) that I would like the scrollbar to be used in.

how to Put JLabel text over a JTextField

how to add a JLabel simple text over/above a JTextField.
I tried many commands as you can see in my code but nothings works.
this is a snapshot of my code
private JPanel createTextPanel() {
int panelWidth = PANEL_SIZE.width;
int panelHeight = PANEL_SIZE.height/3;
Dimension panelSize = new Dimension(panelWidth,panelHeight);
JPanel textPanel = new JPanel();
textPanel.setPreferredSize(panelSize);
//textPanel.setLayout(null);
/* Add text */
JLabel Text_RED = new JLabel();
Text_RED.setText("Red");
//Text_RED = new JLabel("\nRED\n");
//Text_RED.setHorizontalTextPosition(SwingConstants.TOP);
//Text_RED.setVerticalAlignment(SwingConstants.TOP);
Red = new JTextField(3);
//Red.setVerticalAlignment(JTextField.TRAILING );
Red.setLocation(100,100);
//Red.setLocation(50, 50);
JLabel Text_Green = new JLabel("Green");
Green = new JTextField(3);
JLabel Text_Blue = new JLabel("Blue");
Blue = new JTextField(3);
//setLayout(new GridLayout(2,2,10,10));
textPanel.add(Text_RED);
textPanel.add(Red);
textPanel.add(Text_Green);
textPanel.add(Green);
textPanel.add(Text_Blue);
textPanel.add(Blue);
return textPanel;
}
Suggestions:
Don't use null layouts and absolute positioning. While it seems initially that using these tools is the easiest way to create complex GUI's, it's really a newbie fallacy, as the more you understand and use the layout managers, the more you'll find that they make the job of creating GUI's much easier, and the results much more attractive.
Learn about and use the layout managers. Tutorial link.
Consider using a BorderLayout and adding your JLabel BorderLayout.CENTER and the JTextField at BorderLayout.PAGE_END. As a side note, I generally avoid placing JTextFields in a BorderLayout.CENTER position since this will cause horizontal stretching of the field if the GUI changes size, which I don't think is aesthetically pleasing.

Overriding the paint method in JTextField to draw text

I am wishing to draw a number onto a JTextField by overwriting the paint method. So that when the user edits the text field the number doesn't disappear. However, at the moment, the number isn't appearing at all, I have tried:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(number != 0){
g.setColor(Color.RED);
g.drawString(String.valueOf(number),0,0);
}
}
Any ideas, is this even possible?
Try to play with Y position in the g.drawString(String.valueOf(number),0,0); call. E.g. use getHeight()/2
..when the user edits the text field the number doesn't disappear.
As pointed out by #mKorbel, there is no need to override a JTextField in order to get red numbers, simply configure it using the public methods. OTOH..
g.drawString(String.valueOf(number),0,0);
If this is really all about numbers, perhaps the best approach is to use a JSpinner with a SpinnerNumberModel, and set a custom SpinnerUI.
Why don't you just add a small JLabel to the front of the JTextField? The JLabel could contain the number, and because it isn't editable it will always be there no matter what the user changes in the JTextField. You could also format the JLabel to make it red by calling setForeground(Color.RED);. This might be a much simpler solution?
For example, instead of doing this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
You might do something like this...
JPanel panel = new JPanel(new BorderLayout());
JTextField textfield = new JTextField("Hello");
panel.add(textfield,BorderLayout.CENTER);
JLabel label = new JLabel("1.");
label.setForeground(Color.RED);
panel.add(label,BorderLayout.WEST);
Which adds a red JLabel to the left of the JTextField, and because you're using BorderLayout for the JPanel then it automatically makes the JLabel the smallest it can possibly be.
maybe there no reason override paintComponent() for JTextField, instead of use
JTextField.setBackground()
JTextField.setForeground()
JTextField.setFont()
JTextField.setHorizontalAlignment(javax.swing.SwingConstants.LEFT)
some hacks are possible by put there Html colored or special formatted text
EDIT
maybe this question is about
filtering KeyEvents in the Document / DocumentListener
or
JFormattedTextField with Number Formatter

Button inside text box

My code shows a button inside a textbox, but when the input value changes, the size of the text box also changes. That I don't like. Is there any solution such that the textbox size remains fixed? Or any other idea on how to create a button inside textbox?
The following is my code:
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
Try a BorderLayout and add the textfield at BorderLayout.CENTER and the button at BorderLayout.EAST.
I tried your code, and it seemed to work fine for me, with a slight modification. It may be that panel is resizing itself, not textField. Try wrapping panel in another JPanel to force its shape.
JPanel panel = new JPanel();
JPanel outerPanel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
outerPanel.add(panel);
Your example appears to be the second alternative discussed in the article Component Border. Instead, you may want to install() a ComponentBorder, a third alterative described later in the same article.
Is there any solution such that the textbox size remains fixed?
I'm guessing you are creating the JTextField like:
JTextField textField = new JTextField();
In this case the text field size changes as you add text to it.
Instead you should use:
JTextField textField = new JTextField(10);
and the text field will remain a constant size an hold a minimum of 10 characters before scrolling.
Use a different Layout, or try absolute positioning

Categories

Resources