LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-21-2006, 02:15 PM   #1
hazeposse
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Rep: Reputation: 0
reading data from textfile in java?


Hi i'm in progress to make a little program but i'm not that good and have a problem: Now i read all new data in by typing it myself but i want to read it in from a texfile( *.dat). Some one who can help??

this is my code: ( i put it in big letters what has to come in a data-file)

TestFrame.java

package meting;

import meting.model.WeersMetingen;
import meting.model.WeersMeting;
import meting.view.JTableFrame;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class TestFrame extends JFrame {
private JButton verslagButton;
private JButton tabelButton;
private JButton grafiekButton;
private JPanel keuzePanel;
private Font font;
private Border border;
private JFrame newFrame;

public TestFrame(String titel) {
super(titel);
init();
maakKeuzePanel();
maakFrame();
voegListenersToe();

Label tekstLabel = new Label();
tekstLabel.setAlignment(Label.CENTER);
tekstLabel.setFont(font);
tekstLabel.setText("ONT8");
add(tekstLabel);

toonFrame();
}

public void init() {
font = new Font("Sans Serif",Font.PLAIN,14);
border = BorderFactory.createEtchedBorder(
EtchedBorder.RAISED, Color.blue, Color.black);
}

private void maakKeuzePanel() {
keuzePanel = new JPanel();
keuzePanel.setBorder(border);
keuzePanel.setBorder(BorderFactory.createTitledBorder(border, "MAAK UW KEUZE"));

ImageIcon icon = new ImageIcon("grafiek.gif");
ImageIcon iconOver = new ImageIcon("images.gif");

verslagButton = new JButton("Verslag");
verslagButton.setHorizontalAlignment(SwingConstants.CENTER);
verslagButton.setHorizontalTextPosition(SwingConstants.CENTER);
verslagButton.setVerticalTextPosition(SwingConstants.BOTTOM);
verslagButton.setFont(font);
verslagButton.setMnemonic(KeyEvent.VK_V); //ALT-V
verslagButton.setActionCommand("toonVerslag");
//verslagButton.setEnabled(false);
tabelButton = new JButton("Tabellen");
tabelButton.setHorizontalAlignment(SwingConstants.CENTER);
tabelButton.setHorizontalTextPosition(SwingConstants.CENTER);
tabelButton.setVerticalTextPosition(SwingConstants.BOTTOM);
tabelButton.setFont(font);
tabelButton.setMnemonic(KeyEvent.VK_T); //ALT-T
tabelButton.setActionCommand("toonTabel");
//tabelButton.setEnabled(false);
grafiekButton = new JButton("Grafiek", icon);
grafiekButton.setRolloverIcon(iconOver);
grafiekButton.setHorizontalAlignment(SwingConstants.CENTER);
grafiekButton.setHorizontalTextPosition(SwingConstants.CENTER);
grafiekButton.setVerticalTextPosition(SwingConstants.BOTTOM);
grafiekButton.setFont(font);
grafiekButton.setMnemonic(KeyEvent.VK_G); //ALT-T
grafiekButton.setActionCommand("toonGrafiek");
//grafiekButton.setEnabled(false);
keuzePanel.add(verslagButton);
keuzePanel.add(tabelButton);
keuzePanel.add(grafiekButton);

}

private void maakFrame() {
Container pane = getContentPane();
pane.add(keuzePanel, BorderLayout.SOUTH);
}

private void voegListenersToe() {
verslagButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "toonVerslag") {
new VerslagFrame("tabel");
}
}
});
tabelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "toonTabel") {

WeersMetingen data = new WeersMetingen();
data.add(new WeersMeting("10/12/2006", "Lanaken", "+31"));
data.add(new WeersMeting("12/12/2006", "Lanaken", "+30"));
data.add(new WeersMeting("3/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("4/2/2006", "Lanaken", "+1"));
data.add(new WeersMeting("5/2/2006", "Lanaken", "+15"));
data.add(new WeersMeting("6/2/2006", "Lanaken", "+45"));
data.add(new WeersMeting("7/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("8/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("9/2/2006", "Lanaken", "+12"));
data.add(new WeersMeting("10/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("11/2/2006", "Lanaken", "+10"));
data.add(new WeersMeting("1/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("2/2/2006", "Lanaken", "+30"));
data.add(new WeersMeting("3/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("4/2/2006", "Lanaken", "+1"));
data.add(new WeersMeting("5/2/2006", "Lanaken", "+15"));
data.add(new WeersMeting("6/2/2006", "Lanaken", "+45"));
data.add(new WeersMeting("7/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("8/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("9/2/2006", "Lanaken", "+12"));
data.add(new WeersMeting("10/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("11/2/2006", "Lanaken", "+10"));
data.add(new WeersMeting("1/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("2/2/2006", "Lanaken", "+30"));
data.add(new WeersMeting("3/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("4/2/2006", "Lanaken", "+1"));
data.add(new WeersMeting("5/2/2006", "Lanaken", "+15"));
data.add(new WeersMeting("6/2/2006", "Lanaken", "+45"));
data.add(new WeersMeting("7/2/2006", "Lanaken", "+29"));
data.add(new WeersMeting("8/2/2006", "Lanaken", "+31"));
data.add(new WeersMeting("9/2/2006", "Lanaken", "+12"));
data.add(new WeersMeting("10/12/2006", "Lanaken", "+31"));
data.add(new WeersMeting("11/2/2006", "Lanaken", "+10"));

new JTableFrame(data);
}
}
});
grafiekButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "toonGrafiek") {
new GrafiekFrame("grafiek");
}
}
});
}

private void toonFrame() {
setSize(450,280);
Dimension d = getToolkit().getScreenSize();
setLocation((d.width-getSize().width)/2,(d.height-getSize().height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}

main.java

package meting;

import meting.model.WeersMetingen;
import meting.model.WeersMeting;
import meting.view.JTableFrame;



/**
* AWT Sample application
*
* @author
* @version 1.00 06/07/20
*/
public class Main {

public static void main(String[] args) {
// Create application frame.
new TestFrame("OPDRACHT ONT8");
}
}

grafiekFrame.java

package meting;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class GrafiekFrame extends JFrame {
private Font font;
private Border border;

public GrafiekFrame(String titel) {
super(titel);
//init();

Label tekstLabel = new Label();
tekstLabel.setAlignment(Label.CENTER);
tekstLabel.setFont(font);
tekstLabel.setText("grafiek");
add(tekstLabel);

toonFrame();
}

//private void init() {
// font = new Font("Sans Serif",Font.PLAIN,14);
// border = BorderFactory.createEtchedBorder(
// EtchedBorder.RAISED, Color.blue, Color.black);
//}

private void toonFrame() {
setSize(450,280);
Dimension d = getToolkit().getScreenSize();
setLocation((d.width-getSize().width)/2,(d.height-getSize().height)/2);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
}

VerslagFrame.java

package meting;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class VerslagFrame extends JFrame {
private Font font;
private Border border;

public VerslagFrame(String titel) {
super(titel);
//init();

Label tekstLabel = new Label();
tekstLabel.setAlignment(Label.CENTER);
tekstLabel.setFont(font);
tekstLabel.setText("Verslag");
add(tekstLabel);

toonFrame();
}

//private void init() {
// font = new Font("Sans Serif",Font.PLAIN,14);
// border = BorderFactory.createEtchedBorder(
// EtchedBorder.RAISED, Color.blue, Color.black);
//}

private void toonFrame() {
setSize(450,280);
Dimension d = getToolkit().getScreenSize();
setLocation((d.width-getSize().width)/2,(d.height-getSize().height)/2);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
}

WeersMeting.java

package meting.model;

public class WeersMeting {
private String datum;
private String plaats;
private String temp;

public WeersMeting (String datum, String plaats, String temp) {
this.datum = datum;
this.plaats = plaats;
this.temp = temp;
}

public String getDatum() {
return datum;
}

public String getPlaats() {
return plaats;
}

public String getTemp() {
return temp;
}
}

JTableFrame.java

package meting.view;

import meting.model.WeersMeting;
import meting.model.WeersMetingen;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.List;
import java.util.Vector;
import java.io.Serializable;

public class JTableFrame extends JFrame {
private static final String[] koppen = {"Datum", "Plaats", "Temp"};

private DefaultTableModel model;
private JTable table;
private DefaultTableModel model1;
private JTable table1;
private JButton toevoegButton;
private JButton verwijderButton;
private JButton temp_ifv_tijdButton;

public JTableFrame(WeersMetingen weersmetingen) {
super("Metingen");
maakComponenten();
vulTable(weersmetingen.getData());
stelKolomBreedtesIn();
voegListenersToe();
maakLayout();
toonFrame();
}

private void maakComponenten() {
model = new DefaultTableModel();
table = new JTable(model) {
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
};

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

toevoegButton = new JButton("Voeg een meting toe");
verwijderButton = new JButton("Verwijder meting");
temp_ifv_tijdButton = new JButton("temp ifv tijd");
}

private void vulTable(List<WeersMeting> data) {
for (int i= 0; i < koppen.length; i++) {
model.addColumn(koppen[i]);
}


Vector<Serializable> rijData;

for(WeersMeting weersmeting : data) {
rijData = new Vector<Serializable>();
rijData.add(weersmeting.getDatum());
rijData.add(weersmeting.getPlaats());
rijData.add(weersmeting.getTemp());
model.addRow(rijData);

}

}

private void stelKolomBreedtesIn() {
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setMinWidth(70);
columnModel.getColumn(0).setMaxWidth(70);
columnModel.getColumn(1).setMinWidth(70);
columnModel.getColumn(1).setMaxWidth(100);
columnModel.getColumn(2).setMinWidth(40);
columnModel.getColumn(2).setMaxWidth(40);

}

private void voegListenersToe() {
toevoegButton.addActionListener( new ToevoegListener(table, model));
verwijderButton.addActionListener( new VerwijderListener(table, model));
temp_ifv_tijdButton.addActionListener(new Temp_Ifv_TijdListener());
}

private void maakLayout() {
JPanel panel1 = new JPanel();
panel1.add(toevoegButton);
panel1.add(verwijderButton);
JPanel panel2 = new JPanel();
panel2.add(temp_ifv_tijdButton);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

Container content = getContentPane();
content.add(scrollPane, BorderLayout.PAGE_START);
content.add(panel1, BorderLayout.CENTER);
content.add(panel2, BorderLayout.PAGE_END);

}

private void toonFrame() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(0,0);
pack();
setVisible(true);
}

}

WeersMetingen.java

package meting.model;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class WeersMetingen {
private List<WeersMeting> data = new ArrayList<WeersMeting>();

public WeersMetingen() {

}

public WeersMetingen(List<WeersMeting> data) {
this.data = data;
}

public List<WeersMeting> getData() {
return Collections.unmodifiableList(data);
}

public WeersMeting getElementAt(int index) {
return data.get(index);
}

public void add(WeersMeting weersmeting) {
data.add(weersmeting);
}

public boolean remove(WeersMeting weersmeting) {
return data.remove(weersmeting);
}
}

ToevoegListener.java
package meting.view;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.io.Serializable;

public class ToevoegListener implements ActionListener {
private JTable table;
private DefaultTableModel model;

public ToevoegListener(JTable table, DefaultTableModel model) {
this.table = table;
this.model = model;
}

public void actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
Vector<Serializable> vector = new Vector<Serializable>();
vector.add("Datum");
vector.add("Plaats");
vector.add("Temp");
if (rowIndex != -1) {
model.insertRow(rowIndex, vector);
} else {
model.addRow(vector);
}

}
}

VerwijderListener.java
package meting.view;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class VerwijderListener implements ActionListener {
private JTable table;
private DefaultTableModel model;

public VerwijderListener(JTable table, DefaultTableModel model) {
this.table = table;
this.model = model;
}

public void actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
if (rowIndex != -1) {
model.removeRow(rowIndex);
}

}
}

Temp_Ifv_TijdListener.java

package meting.view;

import meting.model.WeersMetingen;
import meting.model.WeersMeting;
import meting.view.JTableFrameTemp_Datum;
import meting.TestFrame;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Temp_Ifv_TijdListener implements ActionListener {

public Temp_Ifv_TijdListener() {

}

public void actionPerformed(ActionEvent e) {


}
}

my data-file should look like: (data.dat)
12/06/2006 Lanaken +31
13/06/2006 Lanaken +16
14/06/2006 Lanaken +0
15/06/2006 Lanaken -5
16/06/2006 Lanaken +16
17/06/2006 Lanaken +14
 
Old 07-21-2006, 07:41 PM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 52
how do you write/read the data?
 
Old 07-22-2006, 06:00 AM   #3
hazeposse
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Original Poster
Rep: Reputation: 0
i've solved the problem. Thanks anyway
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading in data in columns from a file (C++) Nylex Programming 4 03-08-2006 05:55 PM
reading raw data in to memory whizbit Programming 77 07-07-2005 10:20 AM
Insert data into a textfile template! joda Programming 3 03-08-2004 09:45 AM
reading data from a file afrm Programming 2 12-16-2003 07:43 PM
Reading COM Port Data croakofonix Programming 1 06-11-2003 04:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration