Put panel and text box in the same frame - java

How do i put Jlabel and Jtext in the same frame?
if i add the text last, then only the text are showen, thes is my code:
public MatrixFrame(String framname, int width, int height) {
width =7;
height = 6;
JFrame fram = new JFrame(framname);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(500,500);
JTextArea text = new JTextArea("Here come Text");
valMatrixPanel = new ValMatrixPanel(height,width,Color.GRAY, Color.black);
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fram.add(pan);
fram.add(text);
fram.setVisible(true);
}
}

The key to solving this is your understanding how to use layout managers, because this is how Swing decides where to put what and how to size things. First off for a quick easy fix, put all your components into a JPanel, which uses a FlowLayout by default, and then add the JPanel to your JFrame. Don't set the JPanel's or the JFrame's size, do call pack() on the JFrame after adding everything and then finally call setVisible(true).
The better long term answer: read the layout manager tutorials which you can find, among the other Swing tutorials: here.

Try this
you will have to add a import for grid layout
check that
all you need to do is add a grid layout because the textbox overlaps the panel.
so add the line
fame.getContentPane().setLayout(new GridLayout(1,1));
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fame.getContentPane().setLayout(new GridLayout(1,1));
fram.add(pan);
fram.add(text);
fram.setVisible(true);

use BorderLayout in fram.add()
like this
public MatrixFrame(String framname, int width, int height) {
width =7;
height = 6;
JFrame fram = new JFrame(framname);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(500,500);
JTextArea text = new JTextArea("Here come Text");
valMatrixPanel = new ValMatrixPanel(height,width,Color.GRAY, Color.black);
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fram.add(pan,BorderLayout.WEST);
fram.add(text,BorderLayout.NORTH);
fram.setVisible(true);
}

Related

Java swing GUI absolute positioning

I know that absolute positioning is not recommended, but I need to show my labels randomly scattered as well as randomly changing their positions.
I have researched how to use setBounds but it doesn't seem to work. The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
public class GUI extends JFrame{
device mobiles[];
device station;
JPanel pane= new JPanel();
public GUI()
{
setTitle("communication is Key");
setSize(1000, 1000);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane.setBackground(Color.WHITE);
int x=0; int y=0;
mobiles= new device[10];
for (int i = 0; i < 10; i++) {
x=randInt();
y=randInt();
mobiles[i]= new device(1,x,y);
pane.add(mobiles[i]);
}
x=randInt();
y=randInt();
station = new device(0,x,y);
pane.add(station);
this.add(pane);
}
and this is class "devices" that extends JLabel
public class device extends JLabel{
ImageIcon mob = new ImageIcon("mob.png");
ImageIcon tow = new ImageIcon("tower.png");
public device(int num, int x, int y)
{ if(num==1)
this.setIcon(mob);
else this.setIcon(tow);
this.setBounds(x, y, 3, 7);
}
}
any help in finding out what the problem is, would be be appreciated.
The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
The layout manager sets the size and location of the component.
If you don't use the layout manager, then you are responsible for set the size and location of each component.
Typically I would set the size to equal to the components preferred size.
Also, did you display the x/y value that are randomly generated? Maybe the values are larger than the size of the panel.
and when I use setLayout(null) it shows a blank frame.
What layout is set to null? The panel of the frame. Your code doesn't use the above method. Post the code that you use to cause the problem. We don't want to guess what you may or may not be doing.
thanks to #CasparNoree ... the answer suggested was to initialize the Japnel from the start:
JPanel pane = new JPanel(null);
When you set the layout to null you can set the bounds manually with coordinates.
JFrame jf = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton();
// this is where you make it so setting the bounds actually does something
p.setLayout(null);
jb.setBounds(100,100,100,100);
p.add(jb);
jf.add(p);
jf.setVisible(true);

How can i show the jpanel?

I make a movie theater system. And i keep the movies and movie theaters in a jTable. Also I'm trying to show the movie theater. Movie theater seats made from JButton and I keep these seats in a jpanel.
How can i show the seats when the movie theater selected?
And this is my code.
final ArrayList<JPanel> panels = new ArrayList<>();
for(int k=0;k<theater.size();k++){
JPanel panel = new JPanel();
panel.setBounds(500, 0, 500, 500);
contentPane.add(panel);
int y = theater.get(k).getCapacity();
int x = 500/y;
for(int i=0;i<y;i++){
for(int j=0;j<y;j++){
JButton button = new JButton(letters[i]+numbers[j]);
button.setBounds(500 + x*j, 0 + x*i, x-5, x-5);
panel.add(button);
}
}
repaint();
panels.add(panel);
}
Don't use setBounds() method instead leave it for Layout Manger to set the size and position of the components that why it's made for.
Use proper layout for this design such as GridLayout, GridBagLayout etc.
Read more about layout How to Use Various Layout Managers
Don't directly add the components in the JFrame's content pane instead first add the components in another container such as JPanel then finally add it in the JFrame's content pane.

Java Mouse Position couting area that was scrolled

I didnt know how to make a good title for this.. but here we go:
I am trying to get the accurate mouse position when I click on a label, using a MouseAdapter I added to my the JViewPort of the JScrollPane
My panel itself is added inside the JScrollPane.
And when I try to get the coordinates of the mouse position is always relative to the area of the panel that is visible.. its not counting the area that is not visible, because of the scroll..
I dont know If I made myself clear, I hope so..
Already tryed using getMousePosition from JViewPort and from JScrollPane and also did not work..
Thanks alot in advance!!
Here is some code:
The construction of the frame that has the panel inside it..
public GraphViewer(ArrayList<TimeSlot> graph) throws HeadlessException {
final MyCustomPanel panel = new MyCustomPanel(graph);
panel.setPreferredSize(panel.getLargestSize());
scroll = new JScrollPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
JViewport v = scroll.getViewport();
ViewportDragScrollListener l = new ViewportDragScrollListener(panel);
v.addMouseMotionListener(l);
v.addMouseListener(l);
}
What about adding JViewport.getViewPosition() to the coordinates obtained from getMousePosition()?
evento mouseClicked...
x= event.getX();
luego:
Point pos = scrollImagen.getViewport().getViewPosition();
pos.translate(x, y);
...
double nuevaPosx = pos.getX();

How to increase the slow scroll speed on a JScrollPane?

I am adding a JPanel in a JScrollPane in my project.
All is working fine, but there is one problem about mouse scroll using the mouse-Wheel in JPanel. It's speed is very slow on scrolling. How to make it faster?
My code is :
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
objCheckBoxList = new CheckBoxList();
BaseTreeExplorer node = (BaseTreeExplorer)projectMain.objCommon.tree.getLastSelectedPathComponent();
if (node.getObject() != null) {
cmbList.setSelectedItem(node.getParent().toString());
} else {
if (node.toString().equalsIgnoreCase("List of attributes")) {
cmbList.setSelectedIndex(0);
} else {
cmbList.setSelectedItem(node.toString());
}
}
panel.add(objCheckBoxList);
JScrollPane myScrollPanel = new JScrollPane(panel);
myScrollPanel.setPreferredSize(new Dimension(200, 200));
myScrollPanel.setBorder(BorderFactory.createTitledBorder("Attribute List"));
You can set your scrolling speed with this line of code myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);
Here is details.
This bug seems to occur because swing interprets the scroll speed in pixels instead of lines of text. If you are looking for a more accessible alternative to the accepted solution, you can use the following function to calculate and set the actual desired scroll speed in pixels:
public static void fixScrolling(JScrollPane scrollpane) {
JLabel systemLabel = new JLabel();
FontMetrics metrics = systemLabel.getFontMetrics(systemLabel.getFont());
int lineHeight = metrics.getHeight();
int charWidth = metrics.getMaxAdvance();
JScrollBar systemVBar = new JScrollBar(JScrollBar.VERTICAL);
JScrollBar systemHBar = new JScrollBar(JScrollBar.HORIZONTAL);
int verticalIncrement = systemVBar.getUnitIncrement();
int horizontalIncrement = systemHBar.getUnitIncrement();
scrollpane.getVerticalScrollBar().setUnitIncrement(lineHeight * verticalIncrement);
scrollpane.getHorizontalScrollBar().setUnitIncrement(charWidth * horizontalIncrement);
}
Note that swing does calculate the scroll speed correctly when it contains a single component like a JTable or JTextArea. This fix is specifically for when your scroll pane contains a JPanel.

How to setVisible a JinternalFrame in the center of the JdesktopPane?

Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work :
Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame
jDesktopPane1.add(ex);
ex.setLocationRelativeTo(this);
ex.setVisible(true);
So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane.
Thank you
Try something like :
JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();
public void centerJIF(JInternalFrame jif) {
Dimension desktopSize = mainPanel.getSize();
Dimension jInternalFrameSize = jif.getSize();
int width = (desktopSize.width - jInternalFrameSize.width) / 2;
int height = (desktopSize.height - jInternalFrameSize.height) / 2;
jif.setLocation(width, height);
jif.setVisible(true);
}
And
centerJIF(jif_test);
A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window, but you can use a similar approach. Just subtract half the width and height of the internal frame from the corresponding center coordinates of the desktop pane. Use the new coordinates in your call to setLocation().
You'll also want to adjust the internal frame's dimensions if necessary.

Categories