Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with Swing dialogbox  RSS feed

 
Håkan Axheim
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the dialogbox I want:

<Label1:> <txtField1> <button1>
<Label2:> <txtField2> <button2>
<Label3:> <checkbox> <button3>

Of course, I want txtField1 and txtField2 the sam size. How can I do that?
I want button1, 2 and 3 at the same position start position.

This is my current code:

package p;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.Border;


import v.Validator;

public class PrognosValidatorDialog extends JFrame implements ActionListener {
private JFrame jFrame = new JFrame("Kontrollera värderegler och styrdata");

private JPanel panOuter = new JPanel(new BorderLayout());

/* All buttons implemented in user dialog */
private JButton btnCurrentWorkbook = new JButton("Bläddra");
private JButton btnPreviousWorkbook = new JButton("Bläddra");
private JButton btnValidate = new JButton("Kontrollera");

private JTextField txtPreviousWorkbook = new JTextField(10);
private JTextField txtCurrentWorkbook = new JTextField(10);

public PrognosValidatorDialog()
{

jFrame.setSize(220, 200);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);

JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiClose = new JMenuItem("Close");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);

JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);

jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);

jFrame.setJMenuBar(jmb);

jFrame.pack();

//take up the default look and feel specified by windows themes
jFrame.setDefaultLookAndFeelDecorated(true);

jFrame.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnCurrentWorkbook) {
JFileChooser chooser = new JFileChooser();
int returnVal;
returnVal = chooser.showOpenDialog(PrognosValidatorDialog.this);

if (returnVal == chooser.APPROVE_OPTION) {
if (chooser.getSelectedFile() != null) {
txtCurrentWorkbook.setText(chooser.getSelectedFile().getAbsoluteFile().toString());
}
} else if (returnVal == JFileChooser.CANCEL_OPTION) {
txtPreviousWorkbook.setText("You canceled.");
} else {
// log.append("Open command cancelled by user." + newline);
}
}
if (ae.getSource() == btnPreviousWorkbook) {
JFileChooser chooser = new JFileChooser();
int returnVal;
returnVal = chooser.showOpenDialog(PrognosValidatorDialog.this);

if (returnVal == chooser.APPROVE_OPTION) {
if (chooser.getSelectedFile() != null) {
txtPreviousWorkbook.setText(chooser.getSelectedFile().getAbsoluteFile().toString());
}
} else if (returnVal == JFileChooser.CANCEL_OPTION) {
txtPreviousWorkbook.setText("You canceled.");
} else {
// log.append("Open command cancelled by user." + newline);
}
}
if (ae.getSource() == btnValidate) {
if (txtPreviousWorkbook.getText().length() >= 1 && txtCurrentWorkbook.getText().length()>=1) {
Validator validator = new Validator();
validator.validate(txtPreviousWorkbook.getText(), txtCurrentWorkbook.getText());
} else {
JOptionPane.showMessageDialog(null, "Du måste ange 2 kalkyler för att validera dessa!", "Error message", JOptionPane.ERROR_MESSAGE);
}

}
}


public void test()
{
JPanel panTop = new JPanel((new BorderLayout()));
panTop.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel panBottom = new JPanel(new BorderLayout()); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JPanel panConsole = new JPanel(new BorderLayout());

Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("Resultat");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);

panInput.add(panTop,BorderLayout.NORTH);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);

panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);

panOuter.setPreferredSize(new Dimension(800,400));

JLabel lblLeft = new JLabel("Föregående kalkyl : ", JLabel.LEFT);
JLabel lblRight = new JLabel("Inskickad kalkyl : ", JLabel.LEFT);
JLabel lblDatabase = new JLabel("Databastest : ",JLabel.LEFT);
JCheckBox chkDatabase = new JCheckBox();

JTextArea txtConsole = new JTextArea(5, 10);

JScrollPane srcPane = new JScrollPane(txtConsole,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

panTop.add(lblLeft, BorderLayout.WEST);
panTop.add(txtPreviousWorkbook, BorderLayout.CENTER);
panTop.add(btnPreviousWorkbook,BorderLayout.EAST);

panLeft.add(lblRight, BorderLayout.WEST);
panLeft.add(txtCurrentWorkbook, BorderLayout.CENTER);
panLeft.add(btnCurrentWorkbook,BorderLayout.EAST);

/* Add all buttons to Panel */
panBottom.add(lblDatabase,BorderLayout.WEST);
panBottom.add(chkDatabase,BorderLayout.CENTER);
panBottom.add(btnValidate,BorderLayout.EAST);

panConsole.add(srcPane, BorderLayout.CENTER);

/* Add listener to buttons */
btnCurrentWorkbook.addActionListener(this);
btnPreviousWorkbook.addActionListener(this);
btnValidate.addActionListener(this);

jFrame.setContentPane(panOuter);

jFrame.pack();
jFrame.setVisible(true);

}


}

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Håkan, welcome to the Ranch!

Håkan Axheim wrote:Of course, I want txtField1 and txtField2 the sam size. How can I do that?
I want button1, 2 and 3 at the same position start position.

Use an appropriate layout manager.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!