The code is like this:
JTextField txt = new JTextField();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
However the text field is ignoring my call to setBorder.
No changes whatsoever.
I were to replace it with a JLabel (for instance)
JLabel txt = new JLabel();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
I would see the red border.
Can anybody tell me why? Or even better explain to me how to add a border in the JTextField?
Check out this explanation/recommendation from the Java API
In general, when you want to set a
border on a standard Swing component
other than JPanel or JLabel, we
recommend that you put the component
in a JPanel and set the border on the
JPanel.
So... you should nest your JTextField in a JPanel or JLabel, and put the border on the JPanel or JLabel. Voila!
Related
I am working with a JFrame Gui, and I have not found a way to change the background of a JLabel using the event handler actionlistener. The main problem is that I have a JPanel created with 4 JLabels inside. I am unsure why I'm not able to use the JLabel variables that are inside the JPanel container. I've tried creating a field for the JLabel, but it returns null when I try to use the .getBackground() method. I've also tried getting the components of the JPanel using a for loop, and changing the labels through that. So far nothing, hopefully this question makes sense, please help me understand this.
https://i.stack.imgur.com/Hnoj5.png
This image shows the refactored method that has my JPanel container with its 4 JLabel components.
https://i.stack.imgur.com/Gw7Xs.png
This image shows the actionlistener part of my code.
why don't you declare a jframe first? Example
JFrame frame = new JFrame();
and then you create a JPanel after that.
JPanel panel = new JPanel();
and then add your jlabel and stuff in the jpanel and then that is when you call your jframe.
example
frame.add(panel);
i have the same project before the only difference about our problem is that I forgot to use the JPanel, but I have a JFrame. Create a JFrame first.
I can't seem to find out how I can add a JScrollPane to a JLabel. The JLabel that I'm using is populated with a long formatted HTML string. Please help.
area = new JLabel();
JScrollPane scroller = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
Really not good idea to hold or display long Html formatted text in the JLabel, since is possible, better would be use JEditorPanes / JTextPanes, these JComponets support styled and html formatted text, Icons etc ... , examples for JTextPane and JEditorPane
Can you provide us your code? Are you setting the viewport view to the JLabel? Instantiate your JLabel and a JScrollPane. then set the JScrollPane viewport to the JLabel (setViewPortView(jlabel);) then add the JScrollPane to whatever component you want the scrolling JLabel to be on.
Hope this helps!
You can't add a JScrollPane to a JLabel , what you can do is to create a JScrollPane and add a JLabel.
See this: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node63.html
You need to set the JScrollPane's viewport view:
scroller.setViewPortView(area);
stick that line just before you go panel.add(scroller);
Let us know if that helps or not.
I have a JFrame containing three JPanel. The first JPanel contains a JTextField and a JButton. Once the JButton pressed, a JLabel at the second JPanel can show the text input from the JTextField. And then, the third JPanel will change its background according to the JLabel at the second JPanel.
My question is:
How to access the content of JTextField at the first JPanel and then transfer it to the other two JPanel?
you can create
Constructor
Control
please carrefully read all comments by #Hovercraft Full Of Eels to both options
So you have three panels:
JPanel panel1;
JTextField textFieldOnFirstPanel;
JButton buttonOnFirstPanel;
JLabel labelOnSecondPanel;
JPanel panel2;
JPanel panel3;
Keep a reference to all these three panels and all the components in you main object, this could be your JFrame Object itself.
Based on the events, update these components accordingly.
You will first store the data from the first text field in a variable. You could do this in the actionPerformed method when the button is pressed.
Following this you use the setText function to change your JLabel's text.
And last you change the JPanel color by calling its setBackground method.
String text = textField.getText();
label.setText(text);
myJPanel.setBackground(Color.white);
I think that the most clean way to achieev your goal is to access the getter of the field text after receiving notifications from changes as enabled by the classical Observer/Observable pattern. You may have here for details about this pattern .
http://en.wikipedia.org/wiki/Observer_pattern
My 2 pieces
Jerome
I have a JDialog and inside it I have a JPanel that uses FlowLayout
now I've created 3 labels and text fields using the Netbeans GUI Builder, and I want to add 2 more text fields using code
I've adjusted the size of the panel so that when I add a new label and a textfield with a preferred size the new set of label - textfield will be under the previous set
somewhere in the JDialog I do something like this
JLabel cores = new JLabel("Cores");
cores.setPreferredSize(new Dimension(70,15));
first = new JTextField();
first.setPreferredSize(new Dimension(140,20));
JLabel power = new JLabel("Power");
power.setPreferredSize(new Dimension(70,15));
second = new JTextField();
second.setPreferredSize(new Dimension(140,20));
panel2.add(cores);panel2.add(first);panel2.add(power);panel2.add(second);
when I compile the program, the labels don't show up and neither do the textfields
when I go down and click I have the following result
http://img684.imageshack.us/img684/13/unledlpy.png
if I type something, the text field appears
http://img5.imageshack.us/img5/6796/unledhig.png
the labels don't appear though, I don't think I made any changes to the properties, any help would be appreciated thanks
Define the no of columns while creating object.
like this
JTextField jt=new JTextField(20);
I would like to have a JButton (with a folder icon image) inside a JTextField, like over on the far right of the JTextField, so that when clicked, the button opens up a JFileChooser, and when a file is selected, the path to the file appears inside the JTextField.
I have made this code, but nothing shows up.
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
You may find the Component Border helpfull. It allows you to display a button in the text field by using the Border API.
Building on what Shakedown suggested, I think you can get the desired effect relatively easily. What you do is have a JPanel that contains both the text area and, beside it, the button. Next, set the text field to not draw any borders and give the JPanel a bevel border. Now it will look like the button is inside the text area. It might take some fine tuning, but it should work.
You can't don't want to put a button in a text field. You need to break out your intent into several components - 3, in fact.
First you're going to need a parent container, or something that will contain both your text field and also the button; a JPanel should suffice.
Then you need your real components, and by real I mean the ones that actually do something. These are your JTextField and JButton - go ahead and add these to the JPanel. In order to add them and have them appear how you want (with the button in the corner), you're going to need to specify a layout for your JPanel. This layout will define where added components go (visually) inside the JPanel.
Now that you've added those things into your JPanel, you can work only with your JPanel instead of thinking in terms of the contained JTextField and JButton.
Because Pyrite has not posted his final solution, here is mine:
my_button = new JButton("x");
JFormattedTextField my_textfield = new JFormattedTextField("Nr.");
my_textfield.setBorder(javax.swing.BorderFactory.createEmptyBorder());
JPanel textfield_with_button = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Border lowered_bevelborder = BorderFactory.createLoweredBevelBorder();
textfield_with_button.setBorder(lowered_bevelborder);
textfield_with_button.add(my_textfield);
textfield_with_button.add(my_button);
Well I probably think you got your answer, but for others who want to do this easily, cuz I think the other answers are a bit too complicated.
So, what you need to do is, when you create the JTextField you create the JButton as well. Well see the code for yourself:
JButton button = new JButton();
button.setBounds(50, 5, 50, 25);
button.setBackground(Color.black);
JTextField textField = new JTextField();
textField.setBounds(20, 60, 100, 35);
textField.setBackground(Color.white);
textField.add(button);
And its that easy, I used setBounds() on the button cuz I can place it anywhere I want, as for the textField you can use a frame/panel layout aswell, but this was for just demonstrating how it works.