I have a Jwindow, and when I added a Jtextfield to it, the textfield became uneditable.
JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);
window.getContentPane().add(text);
But when I tried to use Jframe as Jwindow's owner, the textfield was now editable, but the frame showed up together with the jwindow :
JFrame frame = new JFrame();
frame.setVisible(true);
JWindow window = new JWindow();
window.setBounds(400, 100, 700,500);
window.setVisible(true);
window.setLayout(null);
JTextField text = new JTextField();
text.setBounds(300, 300, 150, 30);
text.setEditable(true);
window.getContentPane().add(text);
So, I have 2 questions :
Why JTextField is uneditable in JWindow and how could I make it editable?
What is the main purpose of using JFrame as JWindow's border?
EDIT,
contents of JWindow is accesible only if its parent is displayed on the screen
for editable and accesible contents use un_decorated JDialog instead of JWindow, jDialog doesn't caused non_accesible contents,
reason why ..., I can't explain, not undestand why, no way in this moment, the API says me nothing about caused accesible, editable ...
.
.
.
1. Why JTextField is uneditable in JWindow and how could i let it able to edit?
really don't know
import java.awt.*;
import javax.swing.*;
public class WindowTest {
private JFrame frame;
public JPanel createContentPane() {
JTextField text = new JTextField("Whatewer");
JPanel panel = new JPanel();
panel.add(text);
createAndShowWindow();
return panel;
}
void createAndShowGUI() {
frame = new JFrame("Window Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(createContentPane());
frame.setLocation(50, 50);
frame.pack();
frame.setVisible(true);
}
private void createAndShowWindow() {
JTextField text = new JTextField("Whatewer");
JWindow win = new JWindow(frame);
win.setLayout(new GridLayout(0, 1));
win.add(text);
win.pack();
win.setLocation(150, 50);
win.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new WindowTest().createAndShowGUI();
}
});
}
}
EDIT
Yes, both are editable, and i wannt only JWindow to be displayed. Thanks!!
by default JWindow required JFrame for correct workaround
nobody tell that this JFrame must be visible (valid for GUI), then remove these code lines from frame.setDefaultClose.... including frame.setVisible(true); from my example
in this form current JVM instance never gone from RAM, untill your PC restarted or swith off, you have to add separated exit JButton with code line System.exit(0) inside ActionListener
The JWindow should be focusable. Use public void setFocusable(boolean focusable) method.
Related
I'm trying to learn Java Swing. Right now, I'm making a simple program and I need to make a button. I have two classes: driver and swing.
I create the button and import the javax.swing.JButton and added the button. Finally, the button added to the panel but Idk why I just get the panel?
Can anyone help me, please? Here's my code:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Swing extends JFrame {
private JFrame f;
private JButton button;
private JLabel label;
private JPanel panel;
public Swing() {
}
public Swing(String titleName) {
creatButton();
creatFrame(titleName);
}
public void creatButton() {
JButton btn = new JButton("click me");
JPanel panel = new JPanel();
panel.add(btn);
btn.setBounds(50, 100, 95, 30);
add(panel);
}
private void creatFrame(String title) {
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 500);
f.setLayout(null);
}
}
public class Driver {
public static void main (String [] args) {
new Swing ("calculator");
}
}
Okay,lets start with...
JButton btn = new JButton("click me");
JPanel panel = new JPanel();
panel.add(btn);
btn.setBounds(50, 100, 95, 30);
add(panel);
You:
Create a button
Create a panel
You add the button to panel
You add the panel to the frame
And then...
JFrame f = new JFrame("calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new
f.setVisible(true);
You create a brand new instance of JFrame and show it, but it has nothing on to it?! 😱!
Instead, you should avoid extending from JFrame and maybe use JPanel instead, something like...
public class Swing extends JPanel {
private JButton button;
private JLabel label;
public Swing() {
creatButton();
add(button);
}
public void creatButton() {
JButton btn = new JButton("click me");
}
}
Then you can just create a window (or other container) and add it to it
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Swing());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
As a general rule, JFrame is a really poor extension point, it's a complex, compound component and locks you into a single use case. It's generally a better idea to start with something JPanel which provides you with a lot more flexibility and a lot less complexity and is easily reusable.
You really, really, really need to avoid null layouts
creatFrame is creating a new JFrame different than the frame itself (your Swing class extending JFrame).
Remove the line:
JFrame f = new JFrame(title);
and call the methods over this instead of f.
I am trying to create a frame, and when I am adding some components they don't listen to the sizes I give them, or locations - whenever I resize the frame, the components stick together, one aside another. Also, I have a scrollable text area, which takes the length and width of the text written in it. Plus, if I don't resize the frame the components don't show.
My code:
public static void main(String[] args){
new Main();
}
private void loadLabel(){
label.setBounds(0,0,269,20);
//Setting the icon, not relevant to the code.
panel.add(label);
}
private void loadInput(){
input.setBounds(0,20,300,60);
JScrollPane scroll = new JScrollPane (input);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
scroll.setBounds(50,20,300,60);
panel.add(scroll);
}
private JPanel panel = new JPanel();
private JLabel label = new JLabel();
private JTextArea input = new JTextArea("Enter message ");
public Main() {
super("Frame");
setLocationRelativeTo(null);
setSize(300, 400);
setContentPane(panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadLabel();
loadInput();
}
Thanks in advance!
You shouldn't arrange your components using .setBounds(,,,) but instead arrange your
components using layout ( http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html ).
Plus you haven't set your label a text or icon so it's hard to see those component correctly. Here i'm using BoxLayout to manage your components vertically and put them on the EAST side of your frame by replacing setContentPane(panel); to getContentPane().add(panel,BorderLayout.EAST); to help us see your components correctly.
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
public static void main(String[] args){
new Main();
}
private void loadLabel(){
label.setBounds(0,0,269,20);
//Setting the icon, not relevant to the code.
panel.add(label);
}
private void loadInput(){
input.setBounds(0,20,300,60);
JScrollPane scroll = new JScrollPane (input);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
scroll.setBounds(50,20,300,60);
panel.add(scroll);
}
private JPanel panel = new JPanel();
private JLabel label = new JLabel("Your Label");
private JTextArea input = new JTextArea("Enter message ");
public Main() {
super("Frame");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
setLocationRelativeTo(null);
setSize(300, 400);
getContentPane().add(panel,BorderLayout.EAST);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadLabel();
loadInput();
}
}
Write like this
loadLabel();
loadInput();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Load the content then make it visible true
I am creating a JFrame and I call the method setSize(500, 500). Now the desired behaviour is that JFrame should not be resized by user in any condition. Either by maximizing or by dragging the borders. It should be 500x500. How can I do it? I have also attached the code in case you can guide me better.
package com.techpapa;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow extends JFrame{
private JTextField
write;
private JRadioButton
rb1,
rb2,
rb3;
private ButtonGroup
bg;
private ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e){
write.setText("JRadioButton : " + ((JRadioButton)e.getSource()).getText());
}
};
public MainWindow(){
//Frame Initialization
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setTitle(".:JRadioButton:.");
setVisible(true);
//Components Initialization
write = new JTextField(20);
write.setEditable(false);
rb1 = new JRadioButton("Male", false);
rb1.addActionListener(al);
rb2 = new JRadioButton("Female", false);
rb2.addActionListener(al);
rb3 = new JRadioButton("I don't want to specify", true);
rb3.addActionListener(al);
bg = new ButtonGroup();
//Add radio buttons to buttongroup
bg.add(rb1); bg.add(rb2); bg.add(rb3);
//Add to window
add(write);
write.setBounds(140, 100, 150, 20);
write.setDragEnabled(true);
add(rb1);
rb1.setBounds(180, 200, 100, 30);
add(rb2);
rb2.setBounds(180, 225, 100, 30);
add(rb3);
rb3.setBounds(180, 250, 130, 30);
}
public static void main(String[] args) {
new MainWindow();
}
}
You can use a simple call in the constructor under "frame initialization":
setResizable(false);
After this call, the window will not be resizable.
Use setResizable on your JFrame
yourFrame.setResizable(false);
But extending JFrame is generally a bad idea.
Simply write one line in the constructor:
setResizable(false);
This will make it impossible to resize the frame.
This Code May be Help you : [ Both maximizing and preventing resizing on a JFrame ]
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);
Just in case somebody didn't understand the 6th time around:
setResizable(false);
it's easy to use:
frame.setResizable(false);
You can use this.setResizable(false); or frameObject.setResizable(false);
If you are defining class like this
className extend JFrame{}
Use this code
this.setResizable(false);
or
setResizable(false);
I was trying to make my JTextField fill the width and set a height for it but still failed. I tried adding the code setPreferredSize(new Dimension(320,200)); but still failed. Is there any way I can make my JTextField fill the width and set the height to 200 or something?
You should not play with the height. Let the text field determine the height based on the font used.
If you want to control the width of the text field then you can use
textField.setColumns(...);
to let the text field determine the preferred width.
Or if you want the width to be the entire width of the parent panel then you need to use an appropriate layout. Maybe the NORTH of a BorderLayout.
See the Swing tutorial on Layout Managers for more information.
set the height to 200
Set the Font to a large variant (150+ px). As already mentioned, control the width using columns, and use a layout manager (or constraint) that will respect the preferred width & height.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BigTextField {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new FlowLayout(5));
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
// Create big text fields & add them to the GUI
String s = "Hello!";
JTextField tf1 = new JTextField(s, 1);
Font bigFont = tf1.getFont().deriveFont(Font.PLAIN, 150f);
tf1.setFont(bigFont);
gui.add(tf1);
JTextField tf2 = new JTextField(s, 2);
tf2.setFont(bigFont);
gui.add(tf2);
JTextField tf3 = new JTextField(s, 3);
tf3.setFont(bigFont);
gui.add(tf3);
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Big Text Fields");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
There's a way which maybe not perfect, but can meet your requirement. The main point here is use a special dimension to restrict the height. But at the same time, width actually is free, as the max width is big enough.
package test;
import java.awt.*;
import javax.swing.*;
public final class TestFrame extends Frame{
public TestFrame(){
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setPreferredSize(new Dimension(500, 200));
p.setMaximumSize(new Dimension(10000, 200));
p.add(new JLabel("TEST: "));
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
p1.setMaximumSize(new Dimension(10000, 200));
p1.add(new JTextField(50));
p.add(p1);
this.setLayout(new BorderLayout());
this.add(p, BorderLayout.CENTER);
}
//TODO: GUI CREATE
}
xyz.setColumns() method is control the width of TextField.
import java.awt.*;
import javax.swing.*;
class miniproj extends JFrame {
public static void main(String[] args)
{
JFrame frame=new JFrame();
JPanel panel=new JPanel();
frame.setSize(400,400);
frame.setTitle("Registration");
JLabel lablename=new JLabel("Enter your name");
TextField tname=new TextField(30);
tname.setColumns(45);
JLabel lableemail=new JLabel("Enter your Email");
TextField email=new TextField(30);
email.setColumns(45);
JLabel lableaddress=new JLabel("Enter your address");
TextField address=new TextField(30);
address.setColumns(45);
address.setFont(Font.getFont(Font.SERIF));
JLabel lablepass=new JLabel("Enter your password");
TextField pass=new TextField(30);
pass.setColumns(45);
JButton login=new JButton();
JButton create=new JButton();
login.setPreferredSize(new Dimension(90,30));
login.setText("Login");
create.setPreferredSize(new Dimension(90,30));
create.setText("Create");
panel.add(lablename);
panel.add(tname);
panel.add(lableemail);
panel.add(email);
panel.add(lableaddress);
panel.add(address);
panel.add(lablepass);
panel.add(pass);
panel.add(create);
panel.add(login);
frame.add(panel);
frame.setVisible(true);
}
}
What type of LayoutManager are you using for the panel you're adding the JTextField to?
Different layout managers approach sizing elements on them in different ways, some respect SetPreferredSize(), while others will scale the compoenents to fit their container.
See: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
ps. this has nothing to do with eclipse, its java.
f.setLayout(null);
add the above lines ( f is a JFrame or a Container where you have added the JTestField )
But try to learn 'LayoutManager' in java ; refer to other answers for the links of the tutorials .Or try This http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
maybe applying an EmptyBorder to your JTextField would be the simplest solution to simulate a height, and for the width use the setColumns() method
JTextField input=new JTextField();
input.setColumns(10);
input.setBorder(new EmptyBorder(15,0,15,0);
the 4 arguments of the EmptyBorder constructor are: top, left, bottom and right respectively.
Or if you want something more technical you can override the getPreferredSize, getMinimumSize and getMaximumSize methods so that it returns the values ​​you want to apply as width and height.
JTexField input=new JTextField(){
#Override
public Dimension getPreferredSize(){
return new Dimension(200,40);
};
public Dimension getMinimumSize(){
return new Dimension(200,40);
};
public Dimension getMaximumSize(){
return new Dimension(200,40);
};
};
package myguo;
import javax.swing.*;
public class MyGuo {
JFrame f;
JButton bt1 , bt2 ;
JTextField t1,t2;
JLabel l1,l2;
MyGuo(){
f=new JFrame("LOG IN FORM");
f.setLocation(500,300);
f.setSize(600,500);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("NAME");
l1.setBounds(50,70,80,30);
l2=new JLabel("PASSWORD");
l2.setBounds(50,100,80,30);
t1=new JTextField();
t1.setBounds(140, 70, 200,30);
t2=new JTextField();
t2.setBounds(140, 110, 200,30);
bt1 =new JButton("LOG IN");
bt1.setBounds(150,150,80,30);
bt2 =new JButton("CLEAR");
bt2.setBounds(235,150,80,30);
f.add(l1);
f.add(l2);
f.add(t1);
f.add(t2);
f.add(bt1);
f.add(bt2);
f.setVisible(true);
}
public static void main(String[] args) {
MyGuo myGuo = new MyGuo();
}
}
setBounds is working only in BorderLayout use BorderLayout for frame or container or panel and use setBounds to set the width and height of text field.
see this code simple code
import java.awt.*;
import javax.swing.*;
class uni1
{
public static void main(String[] args)
{
JFrame frm = new JFrame();
TextField txt = new TextField();
txt.setBounds(0, 0, 1200, 400);
frm.add(txt,BorderLayout.NORTH);
frm.setLayout(new BorderLayout());
frm.setVisible(true);
frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE);
}
}
on button click, new window(internal frame) should open, what's wrong with my code?
can somebody explain the relationship between desktopane and internalframe and just
regular contentpane?
import javax.swing.*;
import java.awt.event.*;
public class tuna extends JFrame{
private JButton button1;
JDesktopPane desktop;
JInternalFrame internalFrame;
public tuna(){
super("iLyrics");
desktop = new JDesktopPane();
add(desktop);
button1 = new JButton("Open Internal Frame");
add(button1);
button1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JInternalFrame internalFrame = new JInternalFrame("Internal Frame", true, true, true, true );
internalFrame.setBounds(110, 130, 105, 70);
desktop.add(internalFrame, JLayeredPane.DEFAULT_LAYER);
//desktop.add(internalFrame);
internalFrame.setVisible(true);
}
});
}
}
}
It looks like you're adding the desktop and the button to the CENTER of the content pane, making the button replace the desktop pane, so you'll never see it.
// put the desktop in the center
desktop = new JDesktopPane();
getContentPane().add(desktop, BorderLayout.CENTER);
// but the button at the top
button1 = new JButton("Open Internal Frame");
getContentPane().add((button1, BorderLayout.NORTH);
I don't believe you can add a frame to a pane. If you look at the hierarchy of swing containers. It would go Label -> Pane -> Frame. I think the problem with your code is when your doing
desktop.add(internalFrame);
I would change desktop to be a new JFrame
desktop = new JFrame();
http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
This post talks about your relationship with top-level containers.
add this code after you create jinternaframe:
internalFrame.setBounds(110, 130, 105, 70);
desktopPane.add(internalFrame, JLayeredPane.DEFAULT_LAYER);