How to calculate dimensions of a HTML formatted JComponent? - java

I am designing a Swing form using the GUI builder of Netbeans, and I'm using HTML to word wrap a JRadioButton. That works, but now the encapsulating JFrame does not resize the JFrame to fit the contents. I'm using the default GUI builder's LayoutManager, GroupLayout.
I tried to calculate the height of the component after it is word wrapped by HTML. That worked (see code below), but after setting the size and preferred size of the JRadioButton, the window does not get resized.
How can I fix it?
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
// This height is the real height. I tested it setting the background
// color to red.
System.out.println("Actual height: " + actualHeight);
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
Below is the whole code. Since it is generated by the GUI builder, it it hard to remove the unnecessary code.
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.text.View;
public class Test extends javax.swing.JFrame {
Test() {
initComponents();
initMoreComponents();
}
private void initComponents() {
one = new javax.swing.ButtonGroup();
two = new javax.swing.ButtonGroup();
three = new javax.swing.JTextField();
four = new javax.swing.JLabel();
five = new javax.swing.JLabel();
six = new javax.swing.JTextField();
seven = new javax.swing.JRadioButton();
eight = new javax.swing.JRadioButton();
nine = new javax.swing.JLabel();
ten = new javax.swing.JLabel();
eleven = new javax.swing.JCheckBox();
twelve = new javax.swing.JCheckBox();
thirteen = new javax.swing.JCheckBox();
fourteen = new javax.swing.JCheckBox();
fifteen = new javax.swing.JCheckBox();
sixteen = new javax.swing.JCheckBox();
seventeen = new javax.swing.JButton();
eightteen = new javax.swing.JButton();
nineteen = new javax.swing.JButton();
twenty = new javax.swing.JRadioButton();
twentyone = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(300, 420));
setResizable(false);
setSize(new java.awt.Dimension(0, 0));
four.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
four.setText("Four");
five.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
five.setText("Five");
one.add(seven);
seven.setSelected(true);
seven.setText("<html>A very long sentence, which might take up more than one line.</html>");
one.add(eight);
eight.setText("<html>Another multiline sentence.</html>");
nine.setText("Nine");
ten.setText("Ten");
eleven.setText("Eleven");
twelve.setText("Twelve");
thirteen.setText("Thirteen");
fourteen.setText("Fourteen");
fifteen.setText("Fifteen");
sixteen.setText("Sixteen");
seventeen.setText("Seventeen");
eightteen.setText("Eightteen");
nineteen.setText("Nineteen");
two.add(twenty);
twenty.setText("<html>Twenty with another line of text</html>");
two.add(twentyone);
twentyone.setText("Twenty one");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(three)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seventeen))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(six)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eightteen))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(nineteen))
.addComponent(eight, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nine)
.addComponent(four)
.addComponent(five)
.addComponent(ten)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(fourteen)
.addComponent(fifteen))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twelve)
.addComponent(thirteen))))
.addGroup(layout.createSequentialGroup()
.addComponent(eleven)
.addGap(67, 67, 67)
.addComponent(sixteen)))
.addGap(0, 101, Short.MAX_VALUE))
.addComponent(seven, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(twenty)
.addComponent(twentyone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(nine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twenty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(twentyone)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(seventeen))
.addGap(18, 18, 18)
.addComponent(five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(eightteen))
.addGap(18, 18, 18)
.addComponent(ten)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(eleven)
.addComponent(sixteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fourteen)
.addComponent(thirteen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fifteen)
.addComponent(twelve))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nineteen)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void initMoreComponents() {
recalculateSize(this.seven, 280);
}
private void recalculateSize(JComponent component, int width) {
View v = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
float preferredSpan = v.getPreferredSpan(View.Y_AXIS);
int height = component.getHeight();
float margin = height - preferredSpan;
Dimension actualSize = getActualSize(component, width);
int actualHeight = (int) (actualSize.height + margin);
Dimension d = new Dimension(component.getPreferredSize().width, actualHeight);
component.setPreferredSize(d);
component.setSize(d);
component.revalidate();
System.out.println("Actual height: " + actualHeight);
System.out.println("Dimension preferred and set size: " + d);
System.out.println("Dimension after revalidate: " + component.getPreferredSize());
pack();
}
private static Dimension getActualSize(JComponent component, int width) {
View view = (View) component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}
public static void main(String args[]) {
new Test().setVisible(true);
}
private javax.swing.JCheckBox eleven;
private javax.swing.JCheckBox fourteen;
private javax.swing.JButton eightteen;
private javax.swing.JButton seventeen;
javax.swing.JTextField six;
private javax.swing.JLabel five;
private javax.swing.JRadioButton twentyone;
private javax.swing.JCheckBox fifteen;
private javax.swing.JButton nineteen;
private javax.swing.JRadioButton seven;
private javax.swing.ButtonGroup one;
private javax.swing.JLabel nine;
private javax.swing.JRadioButton twenty;
private javax.swing.ButtonGroup two;
private javax.swing.JRadioButton eight;
javax.swing.JTextField three;
private javax.swing.JLabel four;
private javax.swing.JCheckBox sixteen;
private javax.swing.JCheckBox thirteen;
private javax.swing.JCheckBox twelve;
private javax.swing.JLabel ten;
}

Related

GridLayout in panel not showing buttons

I created a class that extends JPanel called GamePane which on creation sets the layout to a GridLayout and then adds buttons one at a time:
public class GamePane extends JPanel {
private JButton[][] buttons;
private final int sizeX, sizeY;
private Partida partida;
public GamePane(int sizeX, int sizeY, Partida p) {
super();
this.sizeX = sizeX;
this.sizeY = sizeY;
this.partida = p;
this.setLayout(new GridLayout(sizeX + 1, sizeY + 1));
buttons = new JButton[sizeX + 1][sizeY + 1];
for (int y = 0; y < sizeY + 1; y++) {
for (int x = 0; x < sizeX + 1; x++) {
JButton button = new JButton();
button.addActionListener(new ListenerGameButton(x, y, this));
this.add(button);
button.setBackground(Color.yellow);
button.setVisible(true);
buttons[x][y] = button;
}
}
The panel initialization is done on the parent JDialog:
gamePane = new GamePane(sizeX, sizeY, parentPartida);
And here's how it looks in the NetBeans editor: (The selected frame being an instance of GamePane)
Then when testing it out it doesn't show the buttons:
This is the class of the parent JDialog where the child GamePane is located, most of the code is autogenerated by Netbeans:
public class gameDialog extends javax.swing.JDialog {
private final int sizeX, sizeY;
private Partida parentPartida;
public gameDialog(java.awt.Frame parent, boolean modal, int sizeX, int sizeY, Partida parentPartida)
{
super(parent, modal);
this.parentPartida = parentPartida;
this.sizeX = sizeX;
this.sizeY = sizeY;
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
logoPane = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
gamePane = new GamePane(sizeX, sizeY, parentPartida);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Partida en curso");
setModal(true);
setResizable(false);
logoPane.setBackground(new java.awt.Color(95, 143, 191));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/logo.png"))); // NOI18N
jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout logoPaneLayout = new javax.swing.GroupLayout(logoPane);
logoPane.setLayout(logoPaneLayout);
logoPaneLayout.setHorizontalGroup(
logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
logoPaneLayout.setVerticalGroup(
logoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
);
javax.swing.GroupLayout gamePaneLayout = new javax.swing.GroupLayout(gamePane);
gamePane.setLayout(gamePaneLayout);
gamePaneLayout.setHorizontalGroup(
gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 437, Short.MAX_VALUE)
);
gamePaneLayout.setVerticalGroup(
gamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 353, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gamePane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(logoPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(gamePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private GUI.GamePane gamePane;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel logoPane;
// End of variables declaration
}
The initComponents() method is generated by NetBeans and that's where the GamePane is created and later on the frame is packed at the end of initComponents() by calling pack()
So, what am I doing wrong? Thanks in advance!
There is no problem with your code. It is perfectly working fine.
JDialog dialog = new JDialog();
dialog.getContentPane().add(new GamePane(10, 10));
dialog.pack();
dialog.setVisible(true);
And i got the below.

Need some help copying my Map into this List

I'm doing a homework assignment using HashMap with GUI. The user will enter a Country, and the Capital of that country, and press Add, this will add those two Strings to the map, and it should add the Capital to the list on the right. The search function is next. The user will be able to type the name of the country, and it will highlight that countries capital in blue on the right among the other capitals that were added. Here is what it should look like.
How can I copy the contents of my hashMap to my JList? I only need the Capitals to display so I can search them.
Any help is appreciated, thank you.
P.S. Sorry for the generated code, the variable declarations are at the bottom because of it.
import java.util.ArrayList;
import java.util.HashMap;
public class MapPanel extends javax.swing.JPanel {
HashMap<String, String> hashMap=new HashMap<String,String>();
ArrayList<String> array = new ArrayList<String>();
/**
* Creates new form MapPanel
*/
public MapPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
searchPanel = new javax.swing.JPanel();
countrySearch = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
inputPanel = new javax.swing.JPanel();
countryInput = new javax.swing.JTextField();
capitalInput = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
listPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
capitalList = new javax.swing.JList();
searchPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel3.setText("Search Country");
jButton2.setText("Search");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout searchPanelLayout = new javax.swing.GroupLayout(searchPanel);
searchPanel.setLayout(searchPanelLayout);
searchPanelLayout.setHorizontalGroup(
searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchPanelLayout.createSequentialGroup()
.addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchPanelLayout.createSequentialGroup()
.addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchPanelLayout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel3))
.addGroup(searchPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(countrySearch, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchPanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
searchPanelLayout.setVerticalGroup(
searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchPanelLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(countrySearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
countryInput.setToolTipText("");
countryInput.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
countryInputFocusGained(evt);
}
});
jLabel1.setText("Country");
jLabel2.setText("Capital");
jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout inputPanelLayout = new javax.swing.GroupLayout(inputPanel);
inputPanel.setLayout(inputPanelLayout);
inputPanelLayout.setHorizontalGroup(
inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(inputPanelLayout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(inputPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(countryInput)
.addComponent(capitalInput)
.addGroup(inputPanelLayout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inputPanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
inputPanelLayout.setVerticalGroup(
inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(inputPanelLayout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(countryInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(capitalInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addContainerGap())
);
jScrollPane1.setViewportView(capitalList);
javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
listPanel.setLayout(listPanelLayout);
listPanelLayout.setHorizontalGroup(
listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(listPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addContainerGap())
);
listPanelLayout.setVerticalGroup(
listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(searchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(inputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(searchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String text = countryInput.getText();
String text2 = capitalInput.getText();
hashMap.put(text,text2);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
hashMap.get(countrySearch.getText());
}
private void countryInputFocusGained(java.awt.event.FocusEvent evt) {
countryInput.requestFocusInWindow();
}
// Variables declaration - do not modify
private javax.swing.JTextField capitalInput;
private javax.swing.JList capitalList;
private javax.swing.JTextField countryInput;
private javax.swing.JTextField countrySearch;
private javax.swing.JPanel inputPanel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel listPanel;
private javax.swing.JPanel searchPanel;
// End of variables declaration
}
Look into the documentation of JList and it's many example use cases.
You can do something like this by modifying your adding values event using setListData() method:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String text = countryInput.getText();
String text2 = capitalInput.getText();
hashMap.put(text,text2);
capitalList.setListData(hashMap.values().toArray());
}
I've successfully tested it:
MapPanel mp = new MapPanel();
JFrame frame = new JFrame("Countries and capitals");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mp);
frame.pack();
frame.setVisible(true);
After adding a country, its capital is shown up in the right list. Problem solved ;-)

How can I set certain text to be read into a textfield in GUI?

I'm working on an assignment using Binary IO and Event Handling. It looks like this, I think I have most of the basic code down, and now its down to the hard stuff. How can I read text from my .bin file into the seperate text fields?
My fields currently are blank, but when read in they should look something like that and I should be able to parse through seperate sets of information with my two buttons on the bottom.
Here is what I have so far.
I have a RecordViewerUI JFrame form, a RecordViewerPanel JFrame panel, and a SalesAgent java class which holds all of my getters and setters/ constructor for my four variables.
Here is my panel. Sorry for the generated code.
Thanks in advance for any help anyone can offer.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RecordViewerPanel extends javax.swing.JPanel {
String filename = "sales.bin";
ArrayList<SalesAgent> customer = new ArrayList<>();
int curIndex = 0;
/**
* Creates new form RecordViewerPanel
*/
public RecordViewerPanel() throws IOException, ClassNotFoundException {
initComponents();
try {
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(filename)));
ArrayList<SalesAgent> salesForce = new ArrayList<SalesAgent>();
String first = in.readUTF();
String last = in.readUTF();
double sales = in.readDouble();
Date date = (Date) (in.readObject());
} catch (FileNotFoundException ex) {
Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void refreshUI() {
SalesAgent sale = (customer.get(curIndex));
firstTextField.setText(sale.getFirst());
lastTextField.setText(sale.getLast());
salesTextField.setText("$" + String.valueOf(sale.getSales()));
dateTextField.setText(String.valueOf(sale.getDate()));
recordPanel.repaint();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
filePanel = new javax.swing.JPanel();
fileLabel = new javax.swing.JLabel();
fileLabel1 = new javax.swing.JLabel();
recordPanel = new javax.swing.JPanel();
firstTextField = new javax.swing.JTextField();
lastTextField = new javax.swing.JTextField();
salesTextField = new javax.swing.JTextField();
dateTextField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
previousButton = new javax.swing.JButton();
nextButton = new javax.swing.JButton();
filePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
fileLabel.setBackground(new java.awt.Color(204, 204, 204));
fileLabel.setText("sales.bin");
fileLabel1.setBackground(new java.awt.Color(204, 204, 204));
fileLabel1.setText("Current file:");
javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
filePanel.setLayout(filePanelLayout);
filePanelLayout.setHorizontalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(fileLabel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(fileLabel1)
.addContainerGap(562, Short.MAX_VALUE)))
);
filePanelLayout.setVerticalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addContainerGap())
.addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addContainerGap()))
);
recordPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
firstTextField.setText("jTextField1");
lastTextField.setText("jTextField1");
salesTextField.setText("jTextField1");
dateTextField.setText("jTextField1");
jLabel1.setText("First Last Sales Date");
previousButton.setText("<<");
previousButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
previousButtonActionPerformed(evt);
}
});
nextButton.setText(">>");
nextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout recordPanelLayout = new javax.swing.GroupLayout(recordPanel);
recordPanel.setLayout(recordPanelLayout);
recordPanelLayout.setHorizontalGroup(
recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 203, Short.MAX_VALUE))
.addGroup(recordPanelLayout.createSequentialGroup()
.addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(dateTextField)))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, recordPanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
);
recordPanelLayout.setVerticalGroup(
recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(previousButton)
.addComponent(nextButton))
.addGap(27, 27, 27))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(filePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (curIndex > 0) {
curIndex--;
} else if (curIndex == 0) {
curIndex = 4;
}
customer.get(curIndex);
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (curIndex < 4) {
curIndex++;
} else if (curIndex == 4) {
curIndex = 0;
}
customer.get(curIndex);
}
// Variables declaration - do not modify
private javax.swing.JTextField dateTextField;
private javax.swing.JLabel fileLabel;
private javax.swing.JLabel fileLabel1;
private javax.swing.JPanel filePanel;
private javax.swing.JTextField firstTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField lastTextField;
private javax.swing.JButton nextButton;
private javax.swing.JButton previousButton;
private javax.swing.JPanel recordPanel;
private javax.swing.JTextField salesTextField;
// End of variables declaration
}
This is rather difficult to answer, as I have no understanding of the file format or the Customer class, but basically, you need to create a new Customer from the values you've read from the file, add it to the customer List and call refreshUI
try {
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(filename)));
String first = in.readUTF();
String last = in.readUTF();
double sales = in.readDouble();
Date date = (Date) (in.readObject());
Customer cust = ...;
customer.add(cust);
} catch (FileNotFoundException ex) {
Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
}
refreshUI();

How to open another jframe with button click in different package

i am developing a java application using swing with the help of net beans. i have to open different j frame which is in different java file in another package.. with a button click.
i also want to transfer information from the current page (j frame) to the redirected j frame.
information like content of text field, radio button drop down list etc..
help me with what to write in button click action listener....
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* CriptUI.java
*
* Created on 31 Aug, 2013, 6:05:02 PM
*/
package crypt;
/**
*
* #author Nitin
*/
public class CryptUI extends javax.swing.JFrame {
/** Creates new form CriptUI */
public CryptUI() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
jSpinner1 = new javax.swing.JSpinner();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel4 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jMenuItem5 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 628, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 28, Short.MAX_VALUE)
);
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel1.setText("Chose Algorithm:");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabel2.setText("Number of itterations:");
jSpinner1.setModel(new javax.swing.SpinnerNumberModel(1, 1, 99, 1));
jButton1.setText("Encrypt Text");
jLabel3.setText("Generated soft key to decrypt:");
jLabel4.setText("Enter key:");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, 0, 218, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(45, 45, 45)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap())
);
jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(35, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jButton2.setText("Send");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Export");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addContainerGap(236, Short.MAX_VALUE)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap())
);
jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton3});
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3)))
);
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
jMenuItem1.setText("Exit");
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setText("Cut");
jMenu2.add(jMenuItem2);
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setText("Copy");
jMenu2.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("Paste");
jMenu2.add(jMenuItem4);
jMenu2.add(jSeparator1);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0));
jMenuItem5.setText("Delete");
jMenu2.add(jMenuItem5);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CryptUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
You can do it with next code in your actionPerformed() method:
AnotherFrame f = new AnotherFrame();
f.setNeededData(/*object*/);
f.pack();
f.setVisible(true);
AnotherFrame is JFrame which you want to create and show.
setNeededData(/*object*/) is method of your frame to transfer neededData.
and last 2 lines show it.
On the click of button you can simply create the object of the JFrame which you want to open and it will load the components in it (as you must be calling initComponents() method in the default constructor).
For passing the parameters from one frame to another you can simply create a POJO and set its properties in the current frame and pass this object to the destination frame while creating the object.
I hope this helps..
On button click ActionEvent:
Create the object of class which has the frame creation function and using that object call the newFrame function :
newframe frm = new newframe(this);
frm.NewFrame();
Suppose this function is in package package org.xyz.frame;
public void NewFrame(){
JFrame frame = new JFrame("Frame2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try
{
for (LookAndFeelInfo info : UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setLocationRelativeTo(ui);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setResizable(false);
}

Allow user handwriten marking on images - Java, Swing

I am developing an application using Java, Swing. It will be mainly used in order to study oscillograms against pentagrams for medical purposes. It is wished that the user can keep notes on top of the images (presumably something like the pencil used in Window's paint).
Please, keep in mind that every time the user loads an image, the following mouse motion listener gets attached to it (in order to make the image draggable for usability reasons):
public class DragMouseListener implements MouseMotionListener {
JLabel jl;
int imageX, imageY;
public DragMouseListener(JLabel jlabel) {
this.jl = jlabel;
jl.addMouseMotionListener(this);
}
#Override
public void mouseDragged(MouseEvent e) {
updateImagePosition(e);
}
#Override
public void mouseMoved(MouseEvent e) {
}
private void updateImagePosition(MouseEvent e) {
imageX = e.getX();
imageY = e.getY();
jl.setLocation(imageX, imageY);
}
}
Until now I am unable to even come close to a way of implementing it and I cannot find any references of the issue in the Internet. Any help (even rough ideas) would be much appreciated. Thanks in advance.
Nick
I tried what you asked for DragMouseListener but found it very uncomfortable so used JInternal Frame it works fine(but you can still drag images inside JInternal Frame,as I implemented what you asked for).Insted of hand free writing,I used JEditorPane(you can add hand written thing if you want,but it will be a mess) with accessible parent JScrollPane so that you can type how much you want.
You can drag JInternal Frame easily:
Note: I have used NetBeans GUI Builder for this and suggest you too to use any GUI builder as it makes
typing work easy.
Here is the code:
import java.awt.event.MouseEvent;
public class Move extends javax.swing.JFrame {
public Move() {
initComponents();
}
#SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jInternalFrame1 = new javax.swing.JInternalFrame();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
jInternalFrame2 = new javax.swing.JInternalFrame();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jEditorPane2 = new javax.swing.JEditorPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jInternalFrame1.setVisible(true);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images.jpg"))); // NOI18N
jLabel1.setText("jLabel1");
jLabel1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentMoved(java.awt.event.ComponentEvent evt) {
jLabel1ComponentMoved(evt);
}
});
jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jLabel1MouseDragged(evt);
}
});
jScrollPane1.setViewportView(jEditorPane1);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, 0, 0, Short.MAX_VALUE))
.addContainerGap())
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
.addContainerGap())
);
jInternalFrame2.setVisible(true);
jLabel2.setIcon(new javax.swing.ImageIcon("E:\\untitled.png")); // NOI18N
jLabel2.setText("jLabel2");
jScrollPane2.setViewportView(jEditorPane2);
javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
jInternalFrame2Layout.setHorizontalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 222, Short.MAX_VALUE))
.addContainerGap())
);
jInternalFrame2Layout.setVerticalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addContainerGap(40, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(177, 177, 177)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(71, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jInternalFrame1)
.addGap(209, 209, 209))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
private void jLabel1ComponentMoved(java.awt.event.ComponentEvent evt) {
}
private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {
updateImagePosition(evt);
}
private void updateImagePosition(MouseEvent evt) {
int imageX = evt.getX();
int imageY = evt.getY();
jLabel1.setLocation(imageX, imageY);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Move().setVisible(true);
}
});
}
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JEditorPane jEditorPane2;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JInternalFrame jInternalFrame2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
}
UPDATE :
See This post answerd by #mKorbel and #trashgod to make JInternal frame minimize/maximize and resize.Great answers.

Categories

Resources