trying to make program where after clicking on buttom it should open new JFrame inside it should have different labels and TextFields....It all compile and work how it should beside History JTextField and hisScrollPAne these ScrollPAne not appeared at all TextField Appeared in completly wrong place....if change bounds armor JTextField and historyJTextField then it history appearing in right place but armor in wrong..not sure what I did wrong.....dimenssions of JFrame(800,900)
This is whole code related to this JFrame...
private void object704JButtonActonPerformed(ActionEvent event)
{
JFrame object704 = new JFrame("object704");
object704.setLayout(new BorderLayout());
JLabel titleJLabel704 = new JLabel();
titleJLabel704.setBounds(0,20,800,36);
titleJLabel704.setFont(new Font("Serif",Font.BOLD,30));
titleJLabel704.setText("Object704");
titleJLabel704.setHorizontalAlignment(JLabel.CENTER);
object704.add(titleJLabel704);
JLabel obPicJLabel = new JLabel();
obPicJLabel.setBounds(190,90,400,215);
obPicJLabel.setIcon(new ImageIcon("gr/objPic.jpg"));
object704.add(obPicJLabel);
JLabel weaponJLabel = new JLabel();
weaponJLabel.setBounds(113,345,70,30);
weaponJLabel.setFont(new Font("Serif",Font.BOLD,20));
weaponJLabel.setText("Weapon");
object704.add(weaponJLabel);
JLabel armorJLabel = new JLabel();
armorJLabel.setBounds(345,345,70,30);
armorJLabel.setFont(new Font("Serif",Font.BOLD,20));
armorJLabel.setText("Armor");
object704.add(armorJLabel);
JLabel historyJLabel = new JLabel();
historyJLabel.setBounds(590,345,70,30);
historyJLabel.setFont(new Font("Serif",Font.BOLD,20));
historyJLabel.setText("History");
object704.add(historyJLabel);
JTextArea weaponJTextArea = new JTextArea("Test2");
weaponJTextArea.setEditable(false);
weaponJTextArea.setBounds(40,380,225,400);
object704.add(weaponJTextArea);
JScrollPane scrollJScrollPane = new JScrollPane(weaponJTextArea);
scrollJScrollPane.setBounds(40,380,225,400);
object704.add(scrollJScrollPane);
JTextArea armorJTextArea = new JTextArea("Test1");
//armorJTextArea.setEditable(false);
armorJTextArea.setVerticalAlignment(JTextArea.CENTER);
armorJTextArea.setBounds(287,380,225,400);
object704.add(armorJTextArea);
JScrollPane armScrollPane = new JScrollPane(armorJTextArea);
armScrollPane.setBounds(287,380,225,400);
object704.add(armScrollPane);
JTextField historyJtextField = new JTextField("test");
//historyJtextField.setEditable(false);
historyJtextField.setBounds(530,380,200,400);
object704.add(historyJtextField);
JTextArea historyJTextArea = new JTextArea();
historyJTextArea.setEditable(false);
historyJTextArea.setBounds(530,380,200,400);
object704.add(historyJTextArea);
JScrollPane hisScrollPane = new JScrollPane(historyJTextArea);
hisScrollPane.setBounds(530,380,200,400);
object704.add(hisScrollPane);
object704.setSize(850,900);
object704.setVisible(true);
Related
Currently, I'm working on java Frame. I have an issue with making the size of the textfield bigger since I can't really see what I typed. I used command+a, then copied and pasted what I typed in another textarea, and it showed as I typed. However, I want to make my textfield bigger. Please let me know what I can do.
This is main class.
public static void main(String[]args)
{
Frame f = new Frame("This is practice title bar!");
f.addWindowListener(new ExitListener());
f.setLayout(new GridLayout(2,1,20,20));
Panel myPanel = new Panel();
myPanel.setLayout(new GridLayout(3,3,20,20));
Label lName = new Label("Name");
lName.setAlignment(Label.CENTER);
myPanel.add(lName);
Label lAge = new Label("Age");
lAge.setAlignment(Label.CENTER);
myPanel.add(lAge);
Label lWeight = new Label("Weight");
lWeight.setAlignment(Label.CENTER);
myPanel.add(lWeight);
TextField tfName = new TextField();
myPanel.add(tfName);
TextField tfAge = new TextField();
myPanel.add(tfAge);
kgPanel kp = new kgPanel();
myPanel.add(kp);
Label empty = new Label("");
myPanel.add(empty);
Button addBtn = new Button("Add");
addBtn.addActionListener(new UserActionListener());
myPanel.add(addBtn);
Label empty2 = new Label("");
myPanel.add(empty2);
f.add(myPanel);
TextArea ta = new TextArea();
f.add(ta);
f.pack();
f.setSize(500,500);
f.setVisible(true);
}
This is kgPanel class.
import java.awt.*;
public class kgPanel extends Panel
{
public TextField weightTf = null;
public Label measurement = null;
public kgPanel()
{
tfAndKg();
}
public kgPanel(LayoutManager layout)
{
super(layout);
tfAndKg();
}
public void tfAndKg()
{
weightTf = new TextField();
add(weightTf);
measurement = new Label("kg");
add(measurement);
}
}
And the image is a result. Please let me know which part should I edit to make my textfield bigger.
Thank you
I tried to use
weightTf.setSize()
and
weightTf.setBounds()
but didn't work.
Please help me.
I have written a java gui code for many options available on it. the gui is also set visible true but it doesn't show until I pick its border and drag them to resize the gui window. After manually resizing it, it shows everything. Also, the textlabels and the textfields and buttons are not in new lines, they are placed one after one. Please tell me whats wrong with that: here is a part of code:
public static void initGUI(){
JFrame fr = new JFrame();
Container cont = fr.getContentPane();
cont.setLayout( new FlowLayout( ) );
FlowLayout layout = new FlowLayout();
cont.setLayout(layout);
frame.setSize(200,300) ;
frame.setVisible(true) ;
JTextField tName = new JTextField(30);
JTextField tCNIC = new JTextField(15);
JLabel nameLabel = new JLabel("Name:");
JLabel cnicLabel = new JLabel("CNIC #:");
cont.add(nameLabel);
cont.add(tName);
cont.add(cnicLabel);
cont.add(tCNIC);
JButton Cancel = new JButton ("Canel" );
JButton OK = new JButton ("OK" );
savebtn.setPreferredSize(new Dimension(150, 50));
retbtn.setPreferredSize(new Dimension(150, 50));
cont.add(savebtn);
cont.add(retbtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
frame.setVisible(true) ;
The above statement should be invoked AFTER all the components have been added to the frame. So it should be the last statement in your method.
Also, you should be invoking:
frame.pack();
instead of setSize(), before making the frame visible so all the components are displayed at their preferred size.
frame.setVisible(true);
This Statement should be invoked in the last of adding other components to the Frame.
I am learning Java and I though as good practice I could make a simple GUI program.
I'm hoping my program will be able to search read and write a text file.
I got all my components looking how I want them too.
I have 4 panels searchPanel, readPanel, writePanel, and the mainMenuPanel they are displayed on the mainframe JFrame.
Now what I want to do (this is where I could really use your guys' help) is how to make this program multi windowed?
i.e if I press the read button in the mainMenuPanel it will display the readPanel. and so on with the other buttons.
I hope my question makes senses and that you guys can help me.
This is my code:
package MyGUIStuff;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class multiWinDemo {
public static void main (String args []) {
/* Search Panel Ingredients
* Label : File Name
* TextField : look for ...
* Button - Search
* Button - Back
* TextArea - Results found ...
*/
JLabel fileNameStorage1 = new JLabel ("File Name");
JTextField searchFor = new JTextField (20);
JButton searchBtnPanel = new JButton ("Search:");
JButton backButton1 = new JButton ("Back");
JTextArea searchedArea = new JTextArea ("Text Area Here!\nName : Misael\nIf this worked\nthen i should\nbe able to\nuse JScrollPane\ncorrectly", 5,20);
searchedArea.setEditable(false);
//searchedArea.setPreferredSize(new Dimension(100,100) );
JScrollPane searchedAreaScoller = new JScrollPane (searchedArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
/* Write Panel Ingredients
* Label : File Name
* TextField : write this text to file ...
* Button : Write
* Button : Back
*/
JLabel fileNameStorage2 = new JLabel ("File Name:");
JTextField writeThis = new JTextField (20);
JButton writeButton = new JButton ("Write");
JButton backButton2 = new JButton ("Back");
/* Read Panel Ingredients
* Label : File Name
* Button - Back
* TextArea - File That I Chose
*/
JLabel fileNameStorage3 = new JLabel ("File Name Here");
JButton backButton3 = new JButton ("Back");
JTextArea readTextArea = new JTextArea ("Text Area Here" ,5 , 20);
readTextArea.setEditable(false);
JScrollPane readScrollPane = new JScrollPane (readTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
/* Main Menu Panel Ingredients
* Label : "File Name"
* TextField : File Absolute Path
* Button - Browse
* Button - Search
* Button - Write
* Button - Read
* Button - Exit
*/
JLabel lbl1 = new JLabel ("File Name:");
JTextArea howTo = new JTextArea ("Search: Search a text file.\nWrite: Write to a text file.\nRead: Read a text file.", 3, 20);
howTo.setEditable(false);
JTextField filePath = new JTextField (20);
JButton browseBtn = new JButton ("Browse");
JButton searchBtn = new JButton ("Search");
JButton writeBtn = new JButton ("Write");
JButton readBtn = new JButton ("Read");
JButton exitBtn = new JButton ("Exit");
//Search Panel
JPanel searchPanel = new JPanel ();
//searchPanel.setLayout(new GridLayout (3,9,5,5) );
searchPanel.setVisible(false);
searchPanel.add(fileNameStorage1);
searchPanel.add(searchFor);
searchPanel.add(backButton1);
searchPanel.add(searchBtnPanel);
searchPanel.add(searchedAreaScoller);
//Write Panel
JPanel writePanel = new JPanel ();
//writePanel.setLayout(new GridLayout (3,9,5,5) );
writePanel.setVisible(false);
writePanel.add(fileNameStorage2);
writePanel.add(writeThis);
writePanel.add(backButton2);
writePanel.add(writeButton);
//Read Panel
JPanel readPanel = new JPanel ();
//readPanel.setLayout(new GridLayout (3,9,5,5) );
readPanel.setVisible(false);
readPanel.add(fileNameStorage3);
//readPanel.add(readTextArea);
readPanel.add(readScrollPane);
readPanel.add(backButton3);
//Main Menu Panel
JPanel blank1 = new JPanel ();
JPanel mainMenuPanel = new JPanel ();
mainMenuPanel.setVisible(true);
//mainMenuPanel.setLayout(new GridLayout (3,9,5,5) );
mainMenuPanel.add(lbl1);
mainMenuPanel.add(filePath);
mainMenuPanel.add(browseBtn);
mainMenuPanel.add(searchBtn);
mainMenuPanel.add(writeBtn);
mainMenuPanel.add(readBtn);
mainMenuPanel.add(howTo);
mainMenuPanel.add(exitBtn);
//Program Frame
JFrame mainFrame = new JFrame ("File Control");
mainFrame.setSize(300,235);
mainFrame.setLayout(new CardLayout() );
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.add(mainMenuPanel);
mainMenuPanel.add(searchPanel);
mainMenuPanel.add(writePanel);
mainMenuPanel.add(readPanel);
}
}
Note: what I've tried so far is: since all 4 panels are being displayed on the frame (frame has a card layout). what I did was try and add action listeners to the searchBtn and set the mainMenuPanel Visible false and searchPanel true, but nothing happened. How can I make it multi windowed?
You can add an ActionListener to the JButton and then an ActionPerformed that opens the other GUI window.
If you need documentation you can find it here --> event handler
From what I'm reading what you are trying to do is when you press a button you want another panel to start up. There are various ways to do this, in my experience you'd want to use an actionListener with your button. Here is some code that I have written based on a simple finance calculator that you can use as a reference. Notice it's using a Combobox and calling the method getselecteditems() to get each selection from a combo box on my main panel.
I then use the add() to add the panel and removeall() and remove() to remove it.
private class financeBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) financeBox.getSelectedItem();
if(selection.equals("Present value")){
//financeFinderPanel.removeAll();
presentValuePanel();
add(presentValuePanel, BorderLayout.NORTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Simple interest")){
simpleInterestPanel();
add(simpleInterestPanel, BorderLayout.SOUTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Doubling time")){
doublingTimePanel();
add(doublingTimePanel, BorderLayout.NORTH);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Compound interest")){
compoundInterestPanel();
add(compoundInterestPanel, BorderLayout.EAST);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Future value"))
{
futureValuePanel();
add(futureValuePanel, BorderLayout.WEST);
financeFinderPanel.removeAll();
remove(financeFinderPanel);
pack();
}
else if(selection.equals("Choose a formula")){
setSize(200, 125);
financeFinderPanel();
add(financeFinderPanel, BorderLayout.NORTH);
NONEMessage = new JLabel("PICK A SELECTION");
}
}
}
There are many other ways to do this, and this might not be the best way in your case. There is something called actionCommand and source which get the actually command that has been called, "you can use this with buttons."
like this...
if(actionCommand.equals("Calc Simple Interest")){
try{
double principal = Double.parseDouble(principalText.getText());
double yearlyRate = Double.parseDouble(yearlyRateText.getText());
double termYears = Double.parseDouble(termYearsText.getText());
double interestRate = financeFormula.simpleInterest(principal, yearlyRate, termYears);
//double interestRate = (principal * yearlyRate * termYears);
String msg = "Simple interest is: $" + dc.format(interestRate);
JOptionPane.showMessageDialog(null, msg);
simpleInterestPanel.removeAll();
remove(simpleInterestPanel);
financeFinderPanel();
add(financeFinderPanel, BorderLayout.NORTH);
pack();
}
In this example the name of the button I clicked is Calc Simple Interest.
I want to add a JComboBox and a JTextField to a panel when there is an item selected in the combobox.
I have this code for the panel.
aantallenGebruikt.add(new JTextField("", 5));
onderdelenGebruikt.add(new JComboBox(onderdelenBox()));
onderdelenGebruikt.get(0).addActionListener(MyFrame.this);
panelAfronden = new JPanel();
panelAfronden.setLayout(new FlowLayout());
panelAfronden.add(new JLabel("Selecteer onderdeelNr en Vul gebruikte aantallen in"));
panelAfronden2 = new JPanel();
panelAfronden2.setLayout(new FlowLayout());
panelAfronden2.add(onderdelenGebruikt.get(0));
panelAfronden2.add(aantallenGebruikt.get(0));
JScrollPane sPane = new JScrollPane(panelAfronden2);
sPane.setPreferredSize(new Dimension(220, 230));
panelAfronden.add(sPane);
panelAfronden.add(new JLabel("Opmerkingen"));
opmerkingenAfronden = new JTextArea(5, 20);
panelAfronden.add(opmerkingenAfronden);
rondAf = new JButton("Rond Werkzaamheid Af");
rondAf.addActionListener(MyFrame.this);
panelAfronden.add(rondAf);
annuleer = new JButton("Annuleer");
annuleer.addActionListener(MyFrame.this);
panelAfronden.add(annuleer);
I have this in the ActionListener
if( eventSource == onderdelenGebruikt){
System.out.println("test");
}
I know how to add the combobox and textfield to the panel but At the moment it doesn't even print out the test to the console
Your Question:
I know how to add the combobox and textfield to the panel but At the moment it doesn't even print out the test to the console.
Answer:
Store the reference of JcomboBox some where and then check the source in ActionListener.
do in this way:
final JComboBox comboBox = new JComboBox(onderdelenBox());
onderdelenGebruikt.add(comboBox);
comboBox.addActionListener(MyFrame.this);
Your ActionListener will look like this.
if( eventSource == comboBox ){
System.out.println("test");
}
I want to repeatedly take input from the user(probably using a button) via a JOptionPane(already done) and store the details in something(how about a dynamic object array) and display this information as a list in a scrollable JList.
MY CODE
import java.awt.GridLayout;
import javax.swing.*;
class Flight {
public static void main(String[] args) {
//Panel
JPanel panel = new JPanel(new GridLayout(7, 2,20, 20));
//Add textfields here
JTextField txtflightno = new JTextField(8);
JTextField txtmechanicalstatus = new JTextField(8);
JTextField txtmedicalstatus = new JTextField(8);
JTextField txtfuellevel = new JTextField(8);
JTextField txtweathercondition = new JTextField(8);
JTextField txtfrequency = new JTextField(8);
JTextField txtflightpath = new JTextField(8);
//Add labels here
JLabel lblflightno = new JLabel("Flight No : ");
JLabel lblmechanicalstatus = new JLabel("Mechanical Status:");
JLabel lblmedicalstatus = new JLabel("Medical Status:");
JLabel lblfuellevel = new JLabel("Fuel Level:");
JLabel lblweathercondition = new JLabel("Weather Condition:");
JLabel lblfrequency = new JLabel("Frequency:");
JLabel lblflightpath = new JLabel("Flight Path:");
//Adding flightno to panel
panel.add(lblflightno);
panel.add(txtflightno);
//Adding mechanicalstatus to the panel
panel.add(lblmechanicalstatus);
panel.add(txtmechanicalstatus);
//Adding medicalstatus to the panel
panel.add(lblmedicalstatus);
panel.add(txtmedicalstatus);
//Adding fuellevel to the panel
panel.add(lblfuellevel);
panel.add(txtfuellevel);
//Adding weathercondition to the panel
panel.add(lblweathercondition);
panel.add(txtweathercondition);
//Adding frequency to the panel
panel.add(lblfrequency);
panel.add(txtfrequency);
//Adding flightpath to the panel
panel.add(lblflightpath);
panel.add(txtflightpath);
panel.setBounds(0, 0, 800, 600);
int result = JOptionPane.showConfirmDialog(null, panel, "Flight Details",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
}
}
}
How must I do the storing of the plane details ? How must I implement a scrollable JList ? Any suggestions.
Many Thanks
As discussed in How to Use Lists, JList uses a list model as the source of data it displays. Just add your data to a DefaultListModel, and use it to construct your list.
DefaultListModel dlm = new DefaultListModel();
// add data
JList list = new JList(dlm);
panel.add(new JScrollPane(list));
To make the JList scrollable, simply embed it in a JScrollPane. Instead of
add(myList, constraints);
do
add(new JScrollPane(myList), constraints);
To extend the list, just get the JList's ListModel (using getListModel) and use add to add objects.
More on using ListModels in Sun's tutorial.
More on ScrollPane in the Tutorial, too.
You've to use the ActionListener of Button, that you've missed in the code snippet.
In the OK Option :
JList jlist = ...;
jlist.add(txtflightno.getText());
jlist.add(txtmechanicalstatus.getText());
jlist.add(txtmedicalstatus.getText());
....
....
&
add(new JScrollPanel(myList), constraints);
After this use validate method of Component to update the list with this new item.
But one thing you should remember is that list displays each item row-wise.
I suggest you to use JTable with which you can display your items in a meaningful way...