JPanel/JFrame Automatically resizing. JAVA - java

I so have made a GUI for a Chat that im currently working on. Its aaaaalmost done but the only thing is the automatically resizing when dragging the window. I can't find out why its happend and this was my last chance. So I really need help from you guys! Im kinda out of idea
The code is here:
ClientGUI(String host, int port) {
super("Chat Client");
defaultPort = port;
defaultHost = host;
// The CenterPanel which is the chat room
MessageText = new JTextArea("Welcome to the Chat room\n");
JPanel centerPanel = new JPanel();
MessageText.setWrapStyleWord(true);
MessageText.setLineWrap(true);
centerPanel.setLayout(null);
JScrollPane scrollPane = new JScrollPane(MessageText);
scrollPane.setBounds(0, 0, 584, 486);
getContentPane().add(scrollPane);
MessageText.setEditable(false);
getContentPane().add(centerPanel, BorderLayout.CENTER);
WriteMessage = new JTextField("Write your username here!");
WriteMessage.setBounds(0, 492, 584, 35);
centerPanel.add(WriteMessage);
WriteMessage.setColumns(234);
WriteMessage.setBackground(Color.WHITE);
// the 3 buttons
Login = new JButton("Login");
Login.setBounds(594, 338, 125, 35);
centerPanel.add(Login);
Logout = new JButton("Logout");
Logout.setBounds(594, 469, 125, 35);
centerPanel.add(Logout);
Logout.addActionListener(this);
Logout.setEnabled(false); // you have to login before being able to logout
Online = new JButton("Online");
Online.setBounds(594, 403, 125, 35);
centerPanel.add(Online);
Online.addActionListener(this);
Online.setEnabled(false); // you have to login before being able to Who is in
JLabel PortNumberText = new JLabel("Port Number: ");
PortNumberText.setBounds(594, 83, 144, 20);
centerPanel.add(PortNumberText);
PortNumber = new JTextField("" + port);
PortNumber.setBounds(594, 114, 129, 20);
centerPanel.add(PortNumber);
PortNumber.setHorizontalAlignment(SwingConstants.RIGHT);
JLabel ServerText = new JLabel("Server Address: ");
ServerText.setBounds(594, 21, 240, 20);
centerPanel.add(ServerText);
// the two JTextField with default value for server address and port number
ServerAddress = new JTextField(host);
ServerAddress.setBounds(594, 52, 129, 20);
centerPanel.add(ServerAddress);
Login.addActionListener(this);
WriteMessage.requestFocus();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(760, 570);
setVisible(true);
}
A picture what I mean

Don't use a null layout. Don't use setBounds(). Swing was designed to be used with layout managers.
Resizing of components can only be done when you use a layout manager. Read the section from the Swing tutorial on Layout Manager for more information and examples. You can always use multiple panels each with a different layout manager to get your desired results.
Also follow Java naming conventions. Variable names should NOT start with an upper case character. Half the time your names are correct and half the time they are not. Be consistent!

You are using a Null-Layout (absolute positioning), and this means you have to take care of everything related to layout yourself. Better use a LayoutManager.

Alright so I just made a border layout all from start so I guess its all good, Thanks everyone for helping! Doesnt look that professional. So yeah, Might be some more updates later on :)

Related

Java how do I access text field

I'm relatively new to programming. Hoping someone can help. I have a button that creates a series of labels and text boxes within a frame once the user enters a number. I'm having trouble access the text boxes once they are created. Can someone point me in the right direction. Code snips below. Thanks.
method to create a label and text box.
public JTextField createPrizePanels(){
JLabel prizePanel = new JLabel("Enter Prize Here", SwingConstants.CENTER);
prizePanel.setVerticalAlignment(SwingConstants.TOP);
prizePanel.setFont(new Font("Arial", Font.BOLD, 14));
prizePanel.setForeground(Color.BLUE);
Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
prizePanel.setBorder(border);
prizePanel.setOpaque(true);
prizePanel.setBackground(Color.LIGHT_GRAY);
prizePanel.setBounds(setBoundsX, setBoundsY, 120, 60);
prizeTextBox = new JTextField(50);
prizeTextBox.setBounds(setBoundsX + 5, setBoundsY + 20, 110, 30);
prizeTextBox.setFont(new Font("Arial", Font.BOLD, 12));
prizeTextBox.setOpaque(true);
prizeTextBox.setBackground(Color.WHITE);
prizeTextBox.setForeground(Color.BLACK);
prizeTextBox.setText("No Prize");
prizeTextBox.setHorizontalAlignment(JTextField.CENTER);
lp.add(prizePanel);
lp.add(prizeTextBox);
return prizeTextBox;
}
code that creates multiple text boxes bases on input from user.
JButton numberOfBallonsButton = new JButton("Set");
numberOfBallonsButton.setBounds(360,160,95, 0x1e);
numberOfBallonsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numberOfBallons = Integer.parseInt(numberOfBallonsTextBox.getText());
System.out.println(numberOfBallons);
lp.remove(numberOfBallonsButton);
for(int i = 0; i < numberOfBallons; i++ ){
createPrizePanels();
setBoundsX = setBoundsX +125;
if(setBoundsX > 450){
setBoundsX = 120;
setBoundsY = setBoundsY + 65;
}
}
lp.add(startGameButton);
}
});
Up to this point it works fine. However, the user needs to enter text into each text box and press another button. I'm not sure how to access each text field. Thanks in advance.
You are using local fields.you can create global field and the return value of createPrizePanels() is never used and for your case you don't know number of created textfield so you can use List as global field and when you create textfield add it to the list so you access it later from anywhere

how to set JtextArea focus in right position?

when i enable or request focus in text area the focus or caret anything u name it, it located at left. but when i start typing, it change to right.
please help me to make it on right when request focus(true);
this is the properties of textarea:
textManualIn = new JTextArea();
textManualIn.setLocale(new Locale("ar", "AE"));
textManualIn.setMinimumSize(new Dimension(5, 22));
textManualIn.setPreferredSize(new Dimension(5, 22));
textManualIn.setFocusCycleRoot(true);
textManualIn.setFocusTraversalPolicyProvider(true);
textManualIn.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
textManualIn.setRequestFocusEnabled(true);
textManualIn.setFont(new Font("Calibri", Font.PLAIN, 14));
textManualIn.setSelectionColor(new Color(153, 255, 255));
textManualIn.setMargin(new Insets(2, 5, 2, 5));
textManualIn.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textManualIn.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textManualIn.setCaretColor(Color.BLACK);
textManualIn.setBackground(new Color(255, 255, 255));
mainmanual.add(status);//add to Jpanel
This is not perfect, but you can try this code:
textManualIn.setText(" "); // one space
// And use one of the following
textManualIn.setCaretPosition(0);
// -or-
textManualIn.select(0, 1);

Why i cant see my buttons?

why if i have an image background i cant see all my buttons? only if my arrow go on button i see it..
this is my jlabel with background:
JLabel sfondo = new JLabel("");
sfondo.setBounds(0, 0, 832, 527);
contentPane.add(sfondo);
sfondo.setIcon(new ImageIcon(
"C:\\Users\\bocci\\workspace\\it's time of the series\\src\\immagini\\icone\\sfondo.jpg"));
sfondo.setIcon(icona3.newicona(patchsfondo, sfondo));
and i have 5 jbuttons
JButton btnOpzioni = new JButton("Opzioni");
btnOpzioni.setBounds(497, 5, 67, 23);
contentPane.add(btnOpzioni);
i see only first button.

how to make JTable display all records form database

My problem is when i run the program the data displayed at the JTable but not all records are displayed although i have a scroll Pane and aJtable inside it,when i run it provides a scroll but for the firs few records ,the jtable have a textfield to provide searching in table using rowSorter ,although jtable doesn't show all records when i search for a record that is not viewed i gets it.this is an image describing the problem.please how can i fix that.
this is the link i have no permission to attache an image here
http://www.megafileupload.com/en/file/561971/Untitled-jpg.html
this is piece of code where i initialize these component
table2 = new JTable(tableModel);
table2.setFont(new Font("Tahoma", Font.PLAIN, 16));
table2.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));
scrollPane_1.setColumnHeaderView(table2.getTableHeader());
table2.setBackground(new Color(210, 180, 140));
table2.setBorder(new LineBorder(new Color(105, 105, 105)));
table2.setForeground(new Color(0, 0, 0));
sorter2 = new TableRowSorter<TableModel>(table2.getModel());
table2.setRowSorter(sorter2);
JViewport vp2 = new JViewport();
vp2.setBackground(new Color(210, 105, 30));
vp2.setForeground(new Color(0, 0, 0));
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
this is the method that fill the table
table2Data = new Vector<>();
Iterator<SellBean> buyIt = boughtedList.iterator();
while (buyIt.hasNext()) {
SellBean buyBean = buyIt.next();
Vector rec = new Vector<>();
rec.add(buyBean.getPRODUCT_ID());
rec.add(buyBean.getPRODUCT_NAME());
// System.out.println(buyBean.getPRODUCT_NAME());
rec.add(buyBean.getQUANTITY());
rec.add(buyBean.getBUY_PRICE());
rec.add(buyBean.getSELL_PRICE());
rec.add(buyBean.getPROFIT());
rec.add(buyBean.getBUY_DATE());
rec.add(buyBean.getRETURNED());
table2Data.add(rec);
}
tm2.setDataVector(table2Data, table2ColNames);
for (int i = 0; i < table2.getColumnCount(); i++) {
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table2.getColumnModel().getColumn(i)
.setCellRenderer(centerRenderer);
}
for (int i = 0; i < table2.getRowCount(); i++) {
table2.setRowHeight(i, 28);
}
table2.getColumnModel().getColumn(0).setPreferredWidth(25);
}
i hope it is clear now
Maybe you have problems with the scrollpane (like: no scrollbars visible).
Are your sure you are using it correct?
this looks strange to me:
You create a ViewPort and put this as View?!
JViewport vp2 = new JViewport();
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
why not just set the Table as ViewportView (as the name implies?)
just:
scrollPane_1.setViewportView(table2);
Edit: Info about the ViewPort:
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html

a retractable JTextArea with scrollpane

Goodevening
how can have a JTextArea like in netbeans (see the pic)
(source: hostingpics.net)
my code of the JTextArea:
JTextArea infoArea = new JTextArea(10,10);
infoArea.setLineWrap(true);
infoArea.setFont(police);
infoArea.setForeground(Color.YELLOW);
infoArea.setBackground(Color.BLACK);
infoArea.setEditable(false);
JScrollPane scroll = new JScrollPane(infoArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 0));
scroll.setPreferredSize(new Dimension(sousFrame.getWidth(),90));
scroll.setFont( new Font("Arial", Font.BOLD, 13));
scroll.setBorder(BorderFactory.createTitledBorder("Output"));
thank you
I'm not sure what NetBeans uses, but we used flexdock at my last company to create dockable windows in a Java Swing application (assuming this is what you meant by "retractable").

Categories

Resources