I'm having a problem when i use .updateUI() in my JPanels, The bounds i set return to default.
//Set my Frame
frame = new JFrame();
frame.setSize(1600,900);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Set Panel
JPanel menuInput = new JPanel();
menuInput.setBounds(100,100,500,500);
//Set Button
JButton buttonSolve = new JButton();
buttonSolve.setBounds(30,30,400,200);
menuInput .add(buttonSolve);
frame.add(menuInput);
//If i updateUI, the bound returns to default.
menuInput.updateUI();
However if i place my menuInput.updateUI before frame.add(menuInput) it will not affect the bounds, but i need to refresh my Panel every time new data comes in.
I use MigLayout, and i have no problem with setting the positions of each object now.
Related
im trying to make a program to add an admin to a ms access database
I researched many times, figured out all the components need to be in a panel, and only the same type of J stuff can be in a panel, so i made many panels and combined them in a big panel
//frame details
final int FRAME_WIDTH = 1000;
final int FRAME_HEIGHT = 1000;
JFrame aFrame = new JFrame("Add admin");
aFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel declaration
JPanel BigPanel = new JPanel();
JPanel adminnameenter = new JPanel();
JPanel typeadminname = new JPanel();
JPanel adminlastnameenter = new JPanel();
JPanel typeadminlastname = new JPanel();
JPanel buttonaddadmin = new JPanel();
//labels, textfields, and buttons
JLabel newAdminName = new JLabel("Enter admin name");
JTextField adminName = new JTextField(7);
JLabel newadminlastname = new JLabel("Enter admin last name");
JTextField adminlastname = new JTextField(7);
JButton addadmin = new JButton("Add Admin");
//add things to panel
adminnameenter.add(newAdminName);
typeadminname.add(adminName);
adminlastnameenter.add(newadminlastname);
typeadminlastname.add(adminlastname);
buttonaddadmin.add(addadmin);
//add things to big jPanel
BigPanel.add(adminnameenter);
BigPanel.add(typeadminname);
BigPanel.add(adminlastnameenter);
BigPanel.add(typeadminlastname);
BigPanel.add(buttonaddadmin);
//add things to frame
aFrame.add(BigPanel);
the only thing that popped up was a frame that said add admin
Add this code to the end of your function:
aFrame.setVisible(false);
aFrame.setVisible(true);
Or alternatively put
aFrame.setVisible(true);
at the end of your function instead of the beginning.
And all the components will appear. This is because whenever you change anything to your JFrame, it will only change on the user side once it is told to resize or refresh the frame. Also you don't need to put every single component in it's own JPanel, you can simply insert them directly in your BigPanel (small nitpick, but the b in bigPanel, shouldn't be capitalized, seeing as variables start with non-capitalized letters).
Also look into LayoutManagers, they will probably be useful for your application.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I've spent hours online and I'm afraid I can't quite figure out how to make my JTable show up next to my JButton on a JFrame, if anyone has a simple or comprehensive way to explain why and how to fix the problem Id really appreciate it.
Extensive online research including downloading samples and applying various suggestions; also reached out to my teacher for help but she doesn't have much experience with java.
public class canvas extends JTable{
static void displayJFrame(){
//set variables
int i = 0;
String[][] strArray = new String[3][i];
String[] labelArray = new String[3];
labelArray[0] = "Name: ";
labelArray[1] = "Subject: ";
labelArray[2] = "Average: ";
//create JFrame
JFrame f = new JFrame("Test Average");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.WHITE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
//create JButton
JButton button=new JButton("Enter New Grade");
button.setBounds(450,15,140, 40);
button.addActionListener(e -> average(strArray, i));//gets info from user and adds to strArray
button.addActionListener(e -> count(i));//increases counter
f.add(button);
//create JTable
JTable j = new JTable(strArray, labelArray);
JScrollPane scrollPane = new JScrollPane(j);
j.setBounds(30, 40, 200, 300);
j.setSize(500, 200);
j.setVisible(true);
}
}
all of my code runs as expected except there is no table, I've also tried so many things that didn't work so this basically where I started so that its not crowded by tons of incorrect stuff
You have several problems. First, by default a JFrame uses a BorderLayout. Using the default add(component) method places that component in the center. Using the same add() method again just replaces the new component at the center.
Second, do not pack or set the frame visible until AFTER you have created the entire GUI.
You would do better to create a JPanel and add the components to that panel, then add the panel to the frame. The default layout manager for JPanel is a FlowLayout.
I mean the following:
JFrame frame = new JFrame("Hello swing");
JPanel panel = new JPanel();
panel.add(new JButton("A"));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
But this code displays this
Is there a way to display something like this right after starting up?
I mean so that the whole title can be seen.
Use the length of the title and apply it to the new dimension used. However I haven't tested it, you have to play with the numbers yourself. Just giving you the idea.
JFrame frame = new JFrame("Hello swing");
JPanel panel = new JPanel();
int len = frame.getTitle().length();
Dimension dimension = new Dimension(200 + 7*len, 200);
panel.setPreferredSize(dimension);
I guess using the number 7 as the constant would be the best. However be aware of using wide (wwww..) or narrow letters (iiii...) mostly, because they appear in different widths. To get the exact length, you have to use method of such as Graphics.getFontMetrics and FontMetrics.stringWidth are.
Take a look at the example acording my code on the top:
Use:
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
to expand your frame
Trying to add 3 panels i created to a frame in Java with the JSplitPane. Have tried this with 2 panels, and that worked great, but with 3 it still does not do what i want.
Have read something about making 2 JSplitPanes and put the one in the other, but that does not actually work what i would like it to do.
My code shows that there are 3 panels, but the size are all wrong.. it should be filled out.
My code:
frame = new JFrame(); // Create a new frame
frame.setVisible(true); // Makes it visible
frame.setSize(900, 500); // Sets size
frame.setTitle(""); // Sets title
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Sets the window on the center of the screen
temp_panel = new JPanel(); // Creates new JPanel
water_panel = new JPanel(); // Creates new JPanel
power_panel = new JPanel(); // Creates new JPanel
temp_panel.setBackground(Color.decode("#2ecc71")); // Sets color
water_panel.setBackground(Color.decode("#3498db")); // Sets color
power_panel.setBackground(Color.decode("#f1c40f")); // Sets color
temp_label = new JLabel("This is Temperature");
water_label = new JLabel("This is Water consumption");
power_label = new JLabel("This is Power consumption");
// Add labels on panel
temp_panel.add(temp_label);
water_panel.add(water_label);
power_panel.add(power_label);
JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JSplitPane splitPaneRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneLeft.setLeftComponent( temp_panel );
splitPaneLeft.setRightComponent( water_panel );
splitPaneRight.setLeftComponent( splitPaneLeft );
splitPaneRight.setRightComponent( power_panel );
splitPaneLeft.setEnabled(false);
splitPaneLeft.setDividerSize(0);
splitPaneRight.setEnabled(false);
splitPaneRight.setDividerSize(0);
// put splitPaneRight onto a single panel
JPanel panelSplit = new JPanel();
panelSplit.add( splitPaneRight );
frame.add(panelSplit, BorderLayout.CENTER);
It should look like this, but just with 3 panels with 3 different colors instead of 2!
Hope someone can help
If you don't need to change the relative sizes of the components during runtime, don't use a JSplitPane. Instead create a container JPanel that uses GridLayout, say new GridLayout(1, 0) for 1 row and variable number of columns, add your three colored JPanels to the GridLayout-using JPanel, and add this then to the JFrame.
you could make one of the panels another JSplitPane, unfortunately there is no other solution for this.
I want to create a UI in java using swings and awt such that :
**** BUTTON1 , BUTTON2,BUTTON3, BUTTON4, BUTTON5 **** (in a row )
***** GRID OF SIZE 20 X 20 ****** (next)
frame.setSize(1000,700);
frame.setLayout(new BorderLayout());
smile = new ImageIcon("C:/Users/RISHABH/Desktop/PP/MineSweeper/sm.png");
sad = new ImageIcon("C:/Users/RISHABH/Desktop/PP/MineSweeper/smb.png");
mine = new ImageIcon("C:/Users/RISHABH/Desktop/PP/MineSweeper/mine.png");
panel1.setLayout(new BorderLayout());
panel2.setLayout(new GridLayout());
smiley.setIcon(smile);
Dimension d = new Dimension(30,20);
smiley.setPreferredSize(d);//.setSize(30, 20);
panel1.add(timer);
panel1.add(score);
panel1.add(smiley);
panel1.validate();
//frame.add(smiley,BorderLayout.NORTH);
//smiley.addActionListener(this);
//Button grid
buttons = new JButton[nrows][ncols];
grid.setLayout(new GridLayout(nrows,ncols));
for(int i=0;i<nrows;i++){
for(int j=0;j<ncols;j++){
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
grid.add(buttons[i][j]);
}
}
panel2.add(grid,BorderLayout.CENTER);
//frame.add(grid,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
i created two panels ..
1st panel was added with all 3 buttons with layout set as BorderLayout
2nd panel contained the grid ..
and then added the two panels to the frame which has the layout border layout
but buttons are not added and the grid appears small ... when I click that grid button it expands ..
Can anybody help me to create this UI ? I am new to java . Please help.
but buttons are not added
frame.setLayout(new BorderLayout());
...
frame.add(panel1);
frame.add(panel2);
You are using a BorderLayout. You can't add to components to the CENTER, only the last component added will be displayed. Try:
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2.CENTER);
and the grid appears small
frame.setSize(1000,700);
Don't hardcode the size. Let the layout manager do its job by invoking the pack() method:
frame.pack();
frame.setVisible(true);
Don't har