How to display panels with component in frame - java

Why my JFrame 'frame' is diplaying empty window, when it should give me 3 menu buttons and my own painted JComponent below ? What am I missing here ?
import java.awt.*;
import javax.swing.*;
public class Eyes extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JFrame("Eyes");
frame.setPreferredSize(new Dimension(450, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel players = new JPanel(new GridLayout(1, 3));
players.add(new JButton("Eyes color"));
players.add(new JButton("Eye pupil"));
players.add(new JButton("Background color"));
JPanel eyes = new JPanel();
eyes.add(new MyComponent());
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(players);
content.add(eyes);
frame.getContentPane();
frame.pack();
frame.setVisible(true);
}
}
class MyComponent extends JComponent {
public MyComponent(){
}
#Override
public void paint(Graphics g) {
int height = 120;
int width = 120;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
BasicStroke bs = new BasicStroke(3.0f);
g2d.setStroke(bs);
g2d.setColor(Color.yellow);
g2d.fillOval(200, 200, height, width);
g2d.setColor(Color.black);
g2d.drawOval(60, 60, height, width);
}
}

Your line:
frame.getContentPane();
doesnt do anything but access the content pane of the frame. Instead of getting the content pane, you should set your content pane, like this:
frame.setContentPane(content);
EDIT:
alternatively, as #trashgod points out, you could use the getContentPane method to access the default content pane and add your content component to that:
frame.getContentPane().add(content);

I think you are attempting to use nested JPanels. This is certainly a way to organize your components, but downside to is the fact that it gets difficult to manage in some cases. You could try this snippet of code below. In the program you will find:
1) An array of JLabel
2) An array of JTextField
3) Nested JPanels
At the end of the program I use the Container to add the final product of these object to my Graphics Window.
The most efficient way I can think of is to define these components at the top of my program so that I can reuse them later as I need to.
To achieve this you can try this snippet of code:
import javax.swing.*; //Required to use Swing Components
public class TestGUI extends JFrame
{
JLabel[] label; //Define this with an array
JTextField[] textField; //Define this with an array as well
private int nLabels; //Number of labels preferred
private int nTextFields; //Number of text fields preferred
public testGUI(int amt)
{
//Assuming that you want equal amounts of each,
//set these two variables to the "ant" input parameter
nLabels = amt;
nTextFields = amt;
//Set component attributes
label = new JLabel[2]; //Label compared text fields
textField = new JTextField[2]; //Use two of these for comparison
textField[0].setEnabled(false); //Disabled editing
//Do nothing with the second text field
JPanel labels = new JPanel(); //Place JLabels here
//Use this to align the labels vertically
labels.setLayout(new GridLayout(2, 1));
//Use this for loop to add the labels to this JPanel
for(int i = 0; i < nLabels; i++)
{
labels.add(label[i]);
//You can also define and apply additional properties
//to labels inside this loop. TIP: You can do this in
//any loop
}
JPanel txtFields = new JPanel(); //Place JTextFields here
//Use this to align the text fields vertically
txtFields.setLayout(new GridLayout(2, 1));
//Use this for loop to add the labels to this JPanel
for(int i = 0; i < nTextFields; i++)
{
textFields.add(textField[i]);
//You can also define and apply additional properties
//to text fields inside this loop. TIP: You can do
//this in any loop
}
//Now we have the two components, you asked for help with, set up
//Next, we will need another JPanel to add these to panels to.
//This JPanel will be added to the JFrame Container
//You probably know how to run this via the "main" method
JPanel window = new JPanel();
//Place the JPanel for the labels and text fields
//This requires a horizontal grid
window.setLayout(new GridLayout(1, 2));
//Add the the two JPanels: "labels" and "txtFields"
window.add(labels);
window.add(txtFields);
//Define the Container object to set up the GUI
Container container = getContentPane();
//Apply the "window" JPanel object to the container
container.add(window, BorderLayout.CENTER);
//Center this in the Graphics Window when displayed
}
//Any other methods and/or functions can be added as well
//If they are, they must placed in the constructor method above
}
This is the approach that I would use when trying to go at making and manipulating my Graphics Windows that I write. Sometimes I write applets, but only after making sure that I have everything functioning properly in a plain Graphics Window.
I hope this helps.
If you have any other questions, just let me know and I will answer the to the best of my ability, thanks.

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);
}
}

swing gui flickering white error

I have a Gui I'm making for a program that has an outer container centered to the JFrame that contains an inner container that holds 22*12 cells. When I run this program, the background just flickers white and stays like that. If you could point me out where I'm going wrong that would be awesome!
public class Gui extends JFrame
{
private JPanel outer, inner;
private JLabel[][] labels = new JLabel[22][12];
public Gui()
{
setBackground(Color.black);
setSize(1000,1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
outer = new JPanel();
outer.setLayout(new BorderLayout());
outer.setSize(620,920);
outer.setBackground(Color.white);
inner = new JPanel();
inner.setLayout(new GridLayout(22,12,10,10));
inner.setSize(600,900);
inner.setBackground(Color.white);
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 12; j++)
{
labels[i][j] = new JLabel();
JLabel label = labels[i][j];
label.setSize(50,50);
label.setBackground(Color.gray);
inner.add(label);
}
}
outer.add(inner, BorderLayout.CENTER);
add(outer, BorderLayout.CENTER);
}
}
The gui is set visible in the main class that instantiates it.
The gui is created and sized correctly. It starts out with a black background then randomly turns to white just after and stays like that.
EDIT: if this is still important:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Gui gui = new Gui();
gui.setVisible(true);
}
});
}
Use the new static variable names which follow final static naming convention. That is the variable should be upper cased:
//setBackground(Color.black);
setBackground(Color.BLACK);
Don't use setSize() for components. Instead add the components to the frame and then use the pack() method so the components are displayed at their preferred size:
//setSize(1000,1000);
add(component1);
add(anotherComponent);
pack();
Layout managers use the preferred size not the size as a layout tip:
//label.setSize(50,50);
label.setPreferredSize(new Dimension(50, 50));
This is your main problem. A JLabel is transparent by default, so the background color you set is ignored:
label.setBackground(Color.gray);
label.setOpaque(true);
By the way, my screen height is only 738, so won't event be able to see you entire frame since you want a height of 22*50. You should be aware of this and probably add your panel to a JScrollPane so people like me can actually use your application. This is why you should not hardcode a preferred size.

Drawing multiple images using paintComponent()

I would like to display more than one image on the screen in the same JPanel.
A for loop iterates over each element in the array and displays their corresponding image, but only seems to keep the last image.
The code:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
public class GameGUI extends JFrame{
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
int arrayLength;
public GameGUI() {
super("Gameplay");
//Set size of the frame.
setSize(650, 580);
//Location inside frame.
setLocation(10, 8);
SwingUtilities.isEventDispatchThread();
The methods that contain each individual panel:
createMainPanel();
createCentrePanel();
createNorthPanel();
createSouthPanel();
createWestPanel();
createEastPanel();
setVisible(true);
}
//creating panels
public void createMainPanel() {
//here is the main panel which the others will be nested in.
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.red);
add(mainPanel);
}
public boolean createCentrePanel() {
JPanel CENTRE = new JPanel(new BorderLayout());
CENTRE.setBackground(Color.WHITE);
CENTRE.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(CENTRE, BorderLayout.CENTER);
return true;
}
This is the panel which i am using to print multiple images to the panel.
As you can see I have a for loop which is going through each item in the array and passing the value to the draw component. However it only seems to keep the last image on the screen eventhough each item in the array is being passed to it.
I have tried using repaint but it doesn't seem to work:
public boolean createNorthPanel() {
int[] array = {1, 8, 9, 10};
arrayLength = array.length;
int size = 0;
JPanel NORTH = new JPanel(new BorderLayout());
NORTH.setPreferredSize(new Dimension(100, 100));
NORTH.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(NORTH, BorderLayout.NORTH);
for (int i = 0; i < arrayLength; i++) {
NORTH.add(new drawPanel(array[i], size, arrayLength));
size = size + 30;
//repaint();
}
return true;
}
public boolean createSouthPanel() {
JPanel SOUTH = new JPanel(new BorderLayout());
SOUTH.setPreferredSize(new Dimension(100, 100));
// SOUTH.add(new drawPanel(2, 0));
// SOUTH.add(new drawPanel(5, 30));
SOUTH.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(SOUTH, BorderLayout.SOUTH);
SOUTH.add(buttonPanel, BorderLayout.EAST);
return true;
}
public boolean createWestPanel() {
JPanel WEST = new JPanel(new BorderLayout());
WEST.setPreferredSize(new Dimension(150, 100));
//WEST.add(new drawPanel(8, 0));
WEST.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(WEST, BorderLayout.WEST);
return true;
}
public boolean createEastPanel() {
JPanel EAST = new JPanel(new BorderLayout());
EAST.setPreferredSize(new Dimension(150, 100));
EAST.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.add(EAST, BorderLayout.EAST);
//EAST.add(new drawPanel(2, 0));
//EAST.add(new drawPanel(7, 60));
return true;
}
public static void main(String args[]){
new GameGUI();
}
}
Here is my class which draws the images on the screen
class drawPanel extends JPanel {
Image image = null;
int xPos;
public drawPanel(int x, int y, int length) {
xPos = y;
try {
File location = new File("src/Card_images/" + x + ".png");
image = ImageIO.read(location);
} catch (IOException e) {
System.out.println("Error: " + e);
}
}
/*public Dimension getPreferredSize() {
return new Dimension(71, 96);
}*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//draws image to screen at positions displayed
g.drawImage(image, xPos, 0, this);
}
}
You only have a single drawImage() statement that is executed in you paintCompent() method so you only ever see the last image drawn.
See Custom Painting Approaches for two different ways to draw multiple objects. You will obviously need to customize for your requirements, but the basic concepts will be the same.
Edit:
The above does not apply to your question, but is still good to know when you do need to do some custom painting.
Sorry, because of the custom painting I misread your question. You are making the code too complex.
The first problem is that you changed the layout manager of the "north" panel to a BorderLayout. You can only add a single component to any location of a BorderLayout. So that is why the last component added gets painted. Just use the default FlowLayout for the panel. Although your code still won't work because your components don't have a preferred size.
So the solution to your problem is:
a) create a panel using a FlowLayout
b) Use a JLabel to display your images. There is no need to do custom painting!. Add the labels to the panel, then add this panel to your frame.
Now the layout manager can do its job and you don't need to worry about the details.
Also, use standard Java naming conventions. Your code is too hard to read because you don't follow the standards.
NORTH is not a proper variable name. It should be "north". An upper cased name indicates a final static variable.
use proper class names. Classes should start with an upper case character. "drawPanel" should be "DrawPanel".

How to embed a grid layout inside a border layout in java

I have a border layout and I want to add a grid layout to the center section. However, I can't declare a grid and then add my center border. How can I do this?
public Liability_Calculator(String s)
{
super(s);
setSize(325,200);
c = getContentPane();
c.setLayout(new BorderLayout());
//the top label
total = new JLabel("Total monthly liabilities ", JLabel.CENTER);
c.add(total, BorderLayout.NORTH);
//the grid
GridLayout grid = new GridLayout(2,2);
text_field1 = new JTextField(7);
//I GET AN ERROR HERE!!!!!!!
grid.add(text_field1);
//AND ERROR HERE!!!!!!!!!!!!!
c.add(grid, BorderLayout.CENTER);
setVisible(true);
}
You're trying to add a component to a layout, and that simply cannot be done. Instead use a JPanel, give it a GridLayout, and then add the component to the JPanel (acting as the "container" here).
In general, you will want to nest JPanels with each using the best layout for the GUI, here the inner JPanel using GridLayout and the outer one using BorderLayout. Then you simply add the inner JPanel to the outer one (here your contentPane) in the BorderLayout.CENTER position.
Providing code visualization derived from Hovercraft's answer:
Display class:
public class Display extends JFrame {
JPanel gridHolder = new JPanel(); // panel to store the grid
private GridLayout buttonsGrid; // space containing a buttons
private JButton myButtons[]; // grid is to be filled with these buttons
private BorderLayout mainGUILayout; // main gui layout
private Container mainGuiContainer;
public Display() {
mainGUILayout = new BorderLayout(5,5); // Border layout option
mainGuiContainer = getContentPane(); // getting content pane
mainGuiContainer.setLayout(mainFrameLayout); // setting main layout
buttonsGrid = new GridLayout(4, 1, 5, 5); // 4 buttons one over the other
myButtons = new JButton[4]; // player's hand represented with buttons
gridHolder.setLayout(buttonsGrid);
for (int x = 0; x < 4; x++) {
myButtons[x] = new JButton (" ");
gridHolder.add(myButtons[x]); }
add(gridHolder, BorderLayout.WEST);
setVisible(true); } }
MainGUILaunch class:
public class MainGUILaunch {
public static void main (String args[]) {
Display myApplication = new Display();
myApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myApplication.setSize(1024, 1024);
myApplication.setVisible(true); // displaying application }
} // End of MainGUILaunch

How to adjust the grid size after resizing the frame?

I was wondering the following: I have a MainWindow component (which contains a frame (JFrame)) and several other JPanels. Where one JPanel, let's say gridPanel uses the gridLayout as LayoutManager. Now my problem is that I want to adjust (set the size of rows / set the size of columns) after the window has been resized. Can someone tell me how I can achieve actions that can be triggered after resizing the frame as I am not familiar with the listeners involved.
It should be the done on the most "standard" coding practices. Thank you for your response and answers!
If you want your grid to "fill up" a container, or fill up the JFrame, then key is to use proper layout managers to hold the GridLayout-using container. For instance if you add the GridLayout-using container to another container that uses FlowLayout, then the GridLayout-using container will not change size if its holding container changes size. However if you add the GridLayout-using container to another container that uses BorderLayout and to its BorderLayout.CENTER position, then the GridLayout-using container will resize as the BorderLayout-using parent container resizes.
Example:
import java.awt.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class ExpandingGrid extends JPanel {
private static final int GAP = 5;
public ExpandingGrid() {
// create a BorderLayout-using JPanel
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout Panel"));
borderLayoutPanel.add(createGridPanel(), BorderLayout.CENTER); // add a Grid to it
// create a FlowLayout-using JPanel
JPanel flowLayoutPanel = new JPanel(new FlowLayout());
flowLayoutPanel.setBorder(BorderFactory.createTitledBorder("FlowLayout Panel"));
flowLayoutPanel.add(createGridPanel()); // add a grid to it
// set up the main JPanel
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new GridLayout(1, 0, GAP, 0)); // grid with 1 row
// and add the borderlayout and flowlayout using JPanels to it
add(borderLayoutPanel);
add(flowLayoutPanel);
}
// create a JPanel that holds a bunch of JLabels in a GridLayout
private JPanel createGridPanel() {
int rows = 5;
int cols = 5;
JPanel gridPanel = new JPanel(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// create the JLabel that simply shows the row and column number
JLabel label = new JLabel(String.format("[%d, %d]", i, j),
SwingConstants.CENTER);
// give the JLabel a border
label.setBorder(BorderFactory.createEtchedBorder());
gridPanel.add(label); // add to the GridLayout using JPanel
}
}
return gridPanel;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("ExpandingGrid");
frame.getContentPane().add(new ExpandingGrid());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
Also, if this is not helpful, then you may wish to elaborate more on your problem and post code, preferably an SSCCE.
That's why I wanted to only adjust the columns.
Maybe the Wrap Layout is what you are looking for.

Categories