add Component to JPanel - java

I am using netBeans editor to create desktop application . and i want to add Component without using drag and drop way. I am trying code like this for adding JList to JPanel but nothing showed
JList jl = new JList();
Vector<String> v= new Vector<String>();
v.add("one");
v.add("Two");
v.add("Three");
jl.setListData(v);
JScrollPane js = new JScrollPane(jl);
js.setLocation(50, 50);
js.setSize(100, 100);
js.setVisible(true);
jPanel1.add(js);

I want to add Component without using drag and drop way.
Here's a simple JList example that doesn't use the NetBeans' GUI editor. See How to Use Lists for more.
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class JListTest {
private static final Random random = new Random();
public static final void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
final DefaultListModel dlm = new DefaultListModel();
for (int i = 0; i < 1000; i++) {
dlm.addElement("Z" + (random.nextInt(9000) + 1000));
}
final JList list = new JList(dlm);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(list), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}

The scroll list doesn't appear, or the data items in the list? Also, you're setting the position manually. Seriously, don't do that -- use a layout manager, many of which are available and you can easily use in the Netbeans GUI editor Mattise.
If the main window is under the control of a layout manager and then you add something to it that specifies its position and size, all mayhem will break loose. Namely, the layout manager will overwrite this, possibly with the result of size becoming 0, 0.
What you need to do is create a JPanel in your layout manager to hold the position of the new component and make sure it has a known field name you can reference and use to add to. Make sure that Panel also has FlowLayout or something in the properties.

you may want to call repaint() when you dynamically create GUI elements.

Related

How do I add a scrollbar to a JFrame with setLayout(null)?

I have some components which I need to use setBounds() on, hence the reason why I'm using the setLayout(null).
But some of my components are out the window(below the Y-axis). I was wondering if there is a way to add a scrollbar to navigate down the window so as to see all the remaining components. A screenshot of my window is below.
Output of my window image:
That GUI would be simple to produce using layouts. Put the component displaying the list (which looks well suited to being a JTable, given the two pieces of data per row / line) into a JScrollPane. Put the scroll pane into the CENTER of a BorderLayout. Put the red label into the PAGE_START of the border layout. Then .. oh wait, the job is done!
This is what it might look like (using a JTextArea instead of a table).
can u please post a copy of this code.
Try implementing it based on the instructions above. If there is a problem, post a minimal reproducible example of your attempt.
Since you are refering to the items in the scrolling area as components, and not as texts in a JTextArea, please have a look at the below.
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Mainframe {
private JFrame f;
Box box;
JScrollPane scrollPane;
Random rand = new Random();
public static void main(String[] args) {
new Mainframe().go();
}
private void go() {
box = new Box(BoxLayout.Y_AXIS);
JLabel label = new JLabel("Possible Paths and Total Distances");
label.setForeground(Color.RED);
for (int i = 0; i < 200; i++) {
box.add(Box.createRigidArea(new Dimension(0, 2)));// creates space between the components
box.add(new JLabel(i + " : " + rand.nextInt(10000)));
}
scrollPane = new JScrollPane(box);
Dimension dim = new Dimension(box.getComponent(0).getPreferredSize());
scrollPane.getVerticalScrollBar().setUnitIncrement(dim.height * 2); // adjusts scrolling speed
//scrollPane.getViewport().setBackground(Color.WHITE);
f = new JFrame();
f.getContentPane().add(label, BorderLayout.NORTH);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(640, 480);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

update JcomboBox after Array is updated

I have a JComboBox which displays the contents of an array in my main class, but I have a another class that has a function which changes the array based on user inputs. However the JComboBox does not update even though the array has been updated in the main class (I used a print to check that it does in fact update). Is there a way for the JComboBox to update as more items are added to the array or items are removed from the array?
This is the JComboBox in the main class, where buildingNames is the array storing information and will be updated.
private String[] buildingNames;
public mainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame("Main Window");
frame.setBounds(0, 0, 1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setBackground(Color.WHITE);
frame.setResizable(false);
buildingNames = {"Costlemark","Science","Research"} //This will get updated
DefaultComboBoxModel BuildingModel = new DefaultComboBoxModel(buildingNames);
JComboBox selectBuilding = new JComboBox(BuildingModel);
selectBuilding.setBounds(46, 82, 150, 40);
frame.getContentPane().add(selectBuilding);
}
Several solutions exist including:
Use an observer pattern to notify concerned objects when the array is updated, and then create a new model for the combo box, and load it into the combo when updates occur. This would be part of a larger Model-View-Controller program structure and is probably the way I'd go.
Create your own model class, extending the abstract combo box model class, one that uses the array itself, and one that again is notified when the array is changed.
Get rid of the array altogether and instead update the combo box model when and where needed
The details of any solution, including code would depend on the details of your current program.
Side recommendations:
Your combo box variable should not be declared locally within the initialize() method as that will make it invisible to the rest of the class, nor should any other object be assigned to a local variable that needs to have its state changed by the program. Declare the variable as a private instance field of the class.
Never set the bounds of your components or use null layouts but rather set properties (visible row count, prototype display value...) and allow the component to size itself.
If your array contents are likely going to change quite a bit during program ru, then you probably should be using a collection such as an ArrayList<String>, or even better, an ArrayList<Building> of your custom Building class.
For an example of the last recommendation where we just use the combo box model:
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class BuildingTest extends JPanel {
// model to hold all the building name strings
private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(new String[] {"Foo", "Bar", "Baz"});
private JComboBox<String> selectBuildingCombo = new JComboBox<>(comboModel);
// text field to allow user to add more strings to model
private JTextField entryField = new JTextField(10);
private JButton enterBuildingBtn = new JButton("Enter Building Name");
public BuildingTest() {
// the size of the combobox larger
selectBuildingCombo.setPrototypeDisplayValue("abcdefghijklmnopqrstuv");
add(selectBuildingCombo);
add(new JLabel("Enter new building name:"));
add(entryField);
add(enterBuildingBtn);
selectBuildingCombo.addActionListener(e -> {
String selection = (String) selectBuildingCombo.getSelectedItem();
if (selection != null) {
System.out.println("Selected Item: " + selection);
}
});
// occurs when user wants to add to combo model
ActionListener enterBuildingListener = e -> {
// get text from text field
String text = entryField.getText().trim();
if (!text.isEmpty()) {
// if not empty, add to model
comboModel.addElement(text);
entryField.selectAll();
}
};
// add this action listener to both the text field and the button
enterBuildingBtn.addActionListener(enterBuildingListener);
entryField.addActionListener(enterBuildingListener);
enterBuildingBtn.setMnemonic(KeyEvent.VK_E);
}
private static void createAndShowGui() {
BuildingTest mainPanel = new BuildingTest();
JFrame frame = new JFrame("Building Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

How can I add ScrollBar to JTextArea in Java Swing?

Anyone help me how to add a scroll bar to a JTextArea with Swing in Java?
The JTextArea just disappear when I add the scroll bar on it.
Hope somebody get me add a vertical scrollbar on it.
Additional explanation will be very thankful
public class Practice extends JFrame {
JFrame frame = new JFrame("AAA");
JTextArea textarea = new JTextArea();
JScrollPane scroll = new JScrollPane(textarea);
JPanel panelForScroll = new JPanel(null);
public Practice(){
frame.setLayout(null);
frame.setBounds(100,100,400,710);
frame.setResizable(false);
frame.setVisible(true);
textarea.setEditable(false);
textarea.setFont(new Font("arian", Font.BOLD, 16));
textarea.setBounds(20, 280, 340, 70);
panelForScroll.add(scroll);
frame.add(panelForScroll); //can't find text area....
}
public static void main(String[] args) {
new Practice();
}
}
There are several errors in your code:
You're using a null layout, this is discouraged as it produces more problems than solutions, specially when you try to use JScrollPanes, since they take the preferredSize of the Component to decide whether to add the scroll bars or not. See Why is it frowned upon to use a null layout in Swing? for more information about this. To fix this, remove this line:
frame.setLayout(null);
And instead use a layout manager or combinations of them along with borders for extra spacing between components.
While null layouts might seem like the best, easiest and faster way to design complex GUIs for Swing newbies, the more you progress in it, the more problems related to the use of them you'll find (as it's the case)
You're extending your class from JFrame and you're creating an instance of JFrame in it too, please use one or the other. When you extend JFrame you're saying your class is a JFrame and thus it cannot be placed inside another Container because JFrame is a rigid container. I recommend to forget the extends JFrame part, since anyway you're not using the JFrame that is generated by this action and stay with the object you created. See https://stackoverflow.com/questions/41252329/java-swing-using-extends-jframe-vs-calling-it-inside-of-class for a more detailed answer about this problem.
You're making your GUI visible before you have added all the elements, this could cause your GUI to not display all the elements until you hover over them, this line:
frame.setVisible(true);
Should be one of the last lines in your program
You're not placing your program on the Event Dispatch Thread (EDT) which makes your application to not be thread safe, you can fix it by writing this on your main method.
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Place your constructor here
}
});
You're setting bounds for the textArea but not for the scrollPane, but you should really not be setting the bounds manually (see point #1 again).
Now, you can make a simple GUI with a JTextArea with a JScrollPane as follows:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ScrollPaneToTextArea {
private JTextArea textArea;
private JFrame frame;
private JScrollPane scroll;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ScrollPaneToTextArea().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame("ScrollPane to TextArea");
textArea = new JTextArea(10, 20); //Rows and cols to be displayed
scroll = new JScrollPane(textArea);
// scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll); //We add the scroll, since the scroll already contains the textArea
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Which produces this output and the scroll bars are added when needed (i.e. when text goes further than the rows it can handle in the view)
If you want the vertical scroll bars to appear always you can uncomment the line:
scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Which will produce the following outputs:
You can read more about JScrollPane in the docs and JTextArea also in their own docs.
JPanel panelForScroll = new JPanel(null);
This sets the NULL Layout to this JPanel. This would require more configuration (just as you did for the frame object).
Just remove the null (also from frame.setLayout(null)!)
You have to use Jtextpane to get the scroll bar on textarea.
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
JFrame f = new JFrame();
f.getContentPane().add(sp);
you are setting the panel's layout to null,then you didn't specify the scroll bar bounds. Since you only have one component in your panel which is the scroll bar I recommend using FlowLayout

Using scrollbars with absolute layout in Swing

I am not able to use scroll bars with absolute layout in Swing.
I don't wish to use this layout but I have to display dynamic objects on my panel on click of a button and align them using setBounds which can be done using this layout only (I guess).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class clothes2 extends javax.swing.JFrame {
JTextField n=null;
JButton m=null;
public clothes2(){
initComponents();
}
public void initComponents() {
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final JPanel jp = new JPanel();
contentPane.setPreferredSize(new Dimension(320,200));
jp.setLayout(null);
m=new JButton("add");
m.setBounds(0,0,50,50);
jp.add(m);
m.addMouseListener( new MouseAdapter() {
int x=0;
int y=0;
public void mouseClicked(MouseEvent me){
x+=100;
y+=100;
jp.add(n=new JTextField("Name"));
n.setBounds(x, y, 50, 50);
jp.add(n=new JTextField("code"));
x+=100;
n.setBounds(x,y, 50, 50);
jp.revalidate();
jp.repaint();
x=0;
}
});
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp = new JScrollPane(jp, v, h);
contentPane.add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f= new clothes2();
f.setVisible(true);
f.setSize(640,320);
}
});
}
}
Set preferred size of the container.
JScrollBar uses the preferred size of the component inside it to determine how large the scroll bars should be, and if they should be displayed.
Usually, the layout manager handles this using the preferredLayoutSize method. This can be overriden by explicitly setting the preferred size of the component.
So either you have to set the preferred size, or use a custom layout manager that calculates it for you.
see also here
might help you.
display dynamic objects .. which can be done using this layout only (I guess).
You guess wrong.
See this GUI, that can not only change PLAFs at run-time, but also dynamically add new components1. Click to..
Add Another Label
This example adds the new labels to a GridLayout - but the principle is the same for any layout (or any component).
add layout
jp.setLayout(new FlowLayout());

GUI shows elements only after dragging window

frame_ref = new Frame("Login");
mainPanel_ref = new Panel();
buttonPanel_ref = new Panel();
grid_ref = new GridLayout(4,2);
frame_ref.setSize(300,120);
frame_ref.setVisible(true);
email_ref = new TextField();
password_ref = new JPasswordField();
mainPanel_ref.setLayout(grid_ref);
mainPanel_ref.add(new Label("E-Mail"));
mainPanel_ref.add(email_ref);
mainPanel_ref.add(new Label("Passwort"));
mainPanel_ref.add(password_ref);
mainPanel_ref.add(submitLogin_ref);
mainPanel_ref.add(fehlerMeldung_ref);
frame_ref.add(mainPanel_ref);
I set up a view in Java like above. The window is complete empty, but after I drag and drop its size, all the elements appear. Does somebody know how to fix this?
Call frame_ref.setVisible(true); after frame_ref.add(mainPanel_ref);.
What happens here is: You show frame by calling frame_ref.setVisible(true); and then add elements in it. So you get an empty frame. Afterwards when you drag or resize it gets repainted and you can see elements.
Call pack() on the JFrame after the components have been added. Doing so will cause the frame to assume the smallest size it needs to display the components. Finally call (setLocation()(4) &) setVisible(true).
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class FrameTest {
public void init() {
frame_ref = new JFrame("Login");
frame_ref.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel_ref = new JPanel(new GridLayout(4,2,6,3));
mainPanel_ref.setBorder(new EmptyBorder(5,5,5,5));
email_ref = new JTextField();
password_ref = new JPasswordField();
mainPanel_ref.add(new JLabel("E-Mail"));
mainPanel_ref.add(email_ref);
mainPanel_ref.add(new JLabel("Passwort"));
mainPanel_ref.add(password_ref);
mainPanel_ref.add(new JLabel(""));
mainPanel_ref.add(new JLabel(""));
mainPanel_ref.add(submitLogin_ref);
mainPanel_ref.add(fehlerMeldung_ref);
frame_ref.add(mainPanel_ref);
//frame_ref.setSize(300,120);
frame_ref.pack();
frame_ref.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FrameTest().init();
}
});
}
private JFrame frame_ref;
private JPanel mainPanel_ref;
private JTextField email_ref;
private JPasswordField password_ref;
private JButton submitLogin_ref = new JButton("Submit Login");
private JButton fehlerMeldung_ref = new JButton("Fehler Meldung");
}
Other tips:
Don't mix Swing with AWT. At least, not the components, or not before targeting Java 7+.
A log-in component is often better suited to putting in a JDialog or JOptionPane rather than a JFrame.
This might be better suited to a nested layout, or some other layout than GridLayout
setLocation() might be swapped out for:
If the log-in has a 'parent' component, setLocationRelativeTo(Component).
If the log-in is the first screen visible, setLocationByPlatform(true) (1.6+).
Check the source closely for other tips.
For better help sooner, post an SSCCE.

Categories